Rose::DB::Object::MakeMethods::Date - Create date-related methods for Rose::DB::Object-derived objects.
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Date ( date => [ 'start_date', 'end_date' => { default => '2005-01-30' } ], datetime => [ 'date_created', 'other_date' => { type => 'datetime year to minute' }, ], timestamp => [ 'last_modified' => { default => '2005-01-30 12:34:56.123' } ], ); ... $o->start_date('2/3/2004 8am'); $dt = $o->start_date(truncate => 'day'); print $o->end_date(format => '%m/%d/%Y'); # 2005-01-30 $o->date_created('now'); $o->other_date('2001-02-20 12:34:56'); # 02/20/2001 12:34:00 print $o->other_date(format => '%m/%d/%Y %H:%M:%S'); print $o->last_modified(format => '%S.%5N'); # 56.12300
Rose::DB::Object::MakeMethods::Date creates methods that deal with dates, and inherits from Rose::Object::MakeMethods. See the Rose::Object::MakeMethods documentation to learn about the interface. The method types provided by this module are described below.
Rose::DB::Object::MakeMethods::Date
Rose::Object::MakeMethods
All method types defined by this module are designed to work with objects that are subclasses of (or otherwise conform to the interface of) Rose::DB::Object. In particular, the object is expected to have a db method that returns a Rose::DB-derived object. See the Rose::DB::Object documentation for more details.
Rose::DB::Object
db
Rose::DB
Create get/set methods for date (year, month, day) attributes.
default
Determines the default value of the attribute.
hash_key
The key inside the hash-based object to use for the storage of this attribute. Defaults to the name of the method.
interface
Choose the interface. The only current interface is get_set, which is the default.
get_set
Creates a get/set accessor method for a date (year, month, day) attribute. When setting the attribute, the value is passed through the parse_date() method of the object's db attribute. If that fails, the value is passed to Rose::DateTime::Util's parse_date() function. If that fails, a fatal error will occur.
parse_date()
Rose::DateTime::Util
The time zone of the DateTime object that results from a successful parse is set to the server_time_zone value of the object's db attribute using DateTime's set_time_zone() method.
DateTime
server_time_zone
set_time_zone()
When saving to the database, the method will pass the attribute value through the format_date() method of the object's db attribute before returning it. Otherwise, the value is returned as-is.
format_date()
This method is designed to allow date values to make a round trip from and back into the database without ever being "inflated" into DateTime objects. Any use of the attribute (get or set) outside the context of loading from or saving to the database will cause the value to be "inflated" using the parse_date() method of the object's db attribute. If that fails, Rose::DateTime::Util's parse_date() function is tried. If that fails, a fatal error will occur.
If passed two arguments and the first argument is "format", then the second argument is taken as a format string and passed to Rose::DateTime::Util's format_date() function along with the current value of the date attribute. Example:
$o->start_date('2004-05-22'); print $o->start_date(format => '%A'); # "Saturday"
If passed two arguments and the first argument is "truncate", then the second argument is taken as the value of the to argument to DateTime's truncate() method, which is applied to a clone of the current value of the date attribute, which is then returned. Example:
to
truncate()
$o->start_date('2004-05-22'); # Equivalent to: # $d = $o->start_date->clone->truncate(to => 'month') $d = $o->start_date(truncate => 'month');
If the date attribute is undefined, then undef is returned (i.e., no clone or call to truncate() is made).
If a valid date keyword is passed as an argument, the value will never be "inflated" but rather passed to the database and returned to other code unmodified. That means that the "truncate" and "format" calls described above will also return the date keyword unmodified. See the Rose::DB documentation for more information on date keywords.
Example:
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Date ( date => [ 'start_date', 'end_date' => { default => '2005-01-30' } ], ); ... $o->start_date('2/3/2004'); $dt = $o->start_date(truncate => 'week'); print $o->end_date(format => '%m/%d/%Y'); # 01/30/2005
Create get/set methods for "datetime" (year, month, day, hour, minute, second) attributes.
type
The datetime variant as a string. Each space in the string is replaced with an underscore "_", then the string is appended to "format_" and "parse_" in order to form the names of the methods called on the object's db attribute to format and parse datetime values. The default is "datetime", which means that the format_datetime() and parse_datetime() methods will be used.
format_datetime()
parse_datetime()
Any string that results in a set of method names that are supported by the object's db attribute is acceptable. Check the documentation for the class of the object's db attribute for a list of valid method names.
Creates a get/set accessor method for a "datetime" attribute. The exact granularity of the "datetime" value is determined by the value of the type option (see above).
When setting the attribute, the value is passed through the parse_TYPE() method of the object's db attribute, where TYPE is the value of the type option. If that fails, the value is passed to Rose::DateTime::Util's parse_date() function. If that fails, a fatal error will occur.
parse_TYPE()
TYPE
When saving to the database, the method will pass the attribute value through the format_TYPE() method of the object's db attribute before returning it, where TYPE is the value of the type option. Otherwise, the value is returned as-is.
format_TYPE()
This method is designed to allow datetime values to make a round trip from and back into the database without ever being "inflated" into DateTime objects. Any use of the attribute (get or set) outside the context of loading from or saving to the database will cause the value to be "inflated" using the parse_TYPE() method of the object's db attribute, where TYPE is the value of the type option. If that fails, Rose::DateTime::Util's parse_date() function is tried. If that fails, a fatal error will occur.
If passed two arguments and the first argument is "format", then the second argument is taken as a format string and passed to Rose::DateTime::Util's format_datetime() function along with the current value of the datetime attribute. Example:
$o->start_date('2004-05-22 12:34:56'); print $o->start_date(format => '%A'); # "Saturday"
If passed two arguments and the first argument is "truncate", then the second argument is taken as the value of the to argument to DateTime's truncate() method, which is applied to a clone of the current value of the datetime attribute, which is then returned. Example:
$o->start_date('2004-05-22 04:32:01'); # Equivalent to: # $d = $o->start_date->clone->truncate(to => 'month') $d = $o->start_date(truncate => 'month');
If the datetime attribute is undefined, then undef is returned (i.e., no clone or call to truncate() is made).
If a valid datetime keyword is passed as an argument, the value will never be "inflated" but rather passed to the database and returned to other code unmodified. That means that the "truncate" and "format" calls described above will also return the datetime keyword unmodified. See the Rose::DB documentation for more information on datetime keywords.
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Date ( datetime => [ 'start_date', 'end_date' => { default => '2005-01-30 12:34:56' } 'other_date' => { type => 'datetime year to minute' }, ], ); ... $o->start_date('2/3/2004 8am'); $dt = $o->start_date(truncate => 'day'); # 01/30/2005 12:34:56 print $o->end_date(format => '%m/%d/%Y %H:%M:%S'); $o->other_date('2001-02-20 12:34:56'); # 02/20/2001 12:34:00 print $o->other_date(format => '%m/%d/%Y %H:%M:%S');
Create get/set methods for "timestamp" (year, month, day, hour, minute, second, fractional seconds) attributes.
Creates a get/set accessor method for a "timestamp" (year, month, day, hour, minute, second, fractional seconds) attribute. When setting the attribute, the value is passed through the parse_timestamp() method of the object's db attribute. If that fails, the value is passed to Rose::DateTime::Util's parse_date() function. If that fails, a fatal error will occur.
parse_timestamp()
When saving to the database, the method will pass the attribute value through the format_timestamp() method of the object's db attribute before returning it. Otherwise, the value is returned as-is.
format_timestamp()
This method is designed to allow timestamp values to make a round trip from and back into the database without ever being "inflated" into DateTime objects. Any use of the attribute (get or set) outside the context of loading from or saving to the database will cause the value to be "inflated" using the parse_timestamp() method of the object's db attribute. If that fails, Rose::DateTime::Util's parse_date() function is tried. If that fails, a fatal error will occur.
If passed two arguments and the first argument is "format", then the second argument is taken as a format string and passed to Rose::DateTime::Util's format_timestamp() function along with the current value of the timestamp attribute. Example:
$o->start_date('2004-05-22 12:34:56.123'); print $o->start_date(format => '%A'); # "Saturday"
If passed two arguments and the first argument is "truncate", then the second argument is taken as the value of the to argument to DateTime's truncate() method, which is applied to a clone of the current value of the timestamp attribute, which is then returned. Example:
$o->start_date('2004-05-22 04:32:01.456'); # Equivalent to: # $d = $o->start_date->clone->truncate(to => 'month') $d = $o->start_date(truncate => 'month');
If the timestamp attribute is undefined, then undef is returned (i.e., no clone or call to truncate() is made).
If a valid timestamp keyword is passed as an argument, the value will never be "inflated" but rather passed to the database and returned to other code unmodified. That means that the "truncate" and "format" calls described above will also return the timestamp keyword unmodified. See the Rose::DB documentation for more information on timestamp keywords.
package MyDBObject; our @ISA = qw(Rose::DB::Object); use Rose::DB::Object::MakeMethods::Date ( timestamp => [ 'start_date', 'end_date' => { default => '2005-01-30 12:34:56.123' } ], ); ... $o->start_date('2/3/2004 8am'); $dt = $o->start_date(truncate => 'day'); # 01/30/2005 12:34:56.12300 print $o->end_date(format => '%m/%d/%Y %H:%M:%S.%5N');
John C. Siracusa (siracusa@mindspring.com)
Copyright (c) 2005 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Rose::DB::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Rose::DB::Object
CPAN shell
perl -MCPAN -e shell install Rose::DB::Object
For more information on module installation, please visit the detailed CPAN module installation guide.