NAME
Rose::DB::Object::MakeMethods::Date - Create date-related methods for Rose::DB::Object-derived objects.
SYNOPSIS
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
DESCRIPTION
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.
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.
METHODS TYPES
- date
-
Create get/set methods for date (year, month, day) attributes.
- Options
-
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.
- Interfaces
-
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'sdb
attribute. If that fails, the value is passed toRose::DateTime::Util
'sparse_date()
function. If that fails, a fatal error will occur.The time zone of the
DateTime
object that results from a successful parse is set to theserver_time_zone
value of the object'sdb
attribute usingDateTime
'sset_time_zone()
method.When saving to the database, the method will pass the attribute value through the
format_date()
method of the object'sdb
attribute before returning it. Otherwise, the value is returned as-is.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 theparse_date()
method of the object'sdb
attribute. If that fails,Rose::DateTime::Util
'sparse_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
'sformat_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 toDateTime
'struncate()
method, which is applied to a clone of the current value of the date attribute, which is then returned. Example:$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
- datetime
-
Create get/set methods for "datetime" (year, month, day, hour, minute, second) attributes.
- Options
-
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. 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 theformat_datetime()
andparse_datetime()
methods will be used.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'sdb
attribute for a list of valid method names.
- Interfaces
-
get_set
-
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'sdb
attribute, whereTYPE
is the value of thetype
option. If that fails, the value is passed toRose::DateTime::Util
'sparse_date()
function. If that fails, a fatal error will occur.The time zone of the
DateTime
object that results from a successful parse is set to theserver_time_zone
value of the object'sdb
attribute usingDateTime
'sset_time_zone()
method.When saving to the database, the method will pass the attribute value through the
format_TYPE()
method of the object'sdb
attribute before returning it, whereTYPE
is the value of thetype
option. Otherwise, the value is returned as-is.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 theparse_TYPE()
method of the object'sdb
attribute, whereTYPE
is the value of thetype
option. If that fails,Rose::DateTime::Util
'sparse_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
'sformat_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 toDateTime
'struncate()
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.
Example:
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');
- timestamp
-
Create get/set methods for "timestamp" (year, month, day, hour, minute, second, fractional seconds) attributes.
- Options
-
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.
- Interfaces
-
get_set
-
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'sdb
attribute. If that fails, the value is passed toRose::DateTime::Util
'sparse_date()
function. If that fails, a fatal error will occur.The time zone of the
DateTime
object that results from a successful parse is set to theserver_time_zone
value of the object'sdb
attribute usingDateTime
'sset_time_zone()
method.When saving to the database, the method will pass the attribute value through the
format_timestamp()
method of the object'sdb
attribute before returning it. Otherwise, the value is returned as-is.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 theparse_timestamp()
method of the object'sdb
attribute. If that fails,Rose::DateTime::Util
'sparse_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
'sformat_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 toDateTime
'struncate()
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.
Example:
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');
AUTHOR
John C. Siracusa (siracusa@mindspring.com)
COPYRIGHT
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.