Rose::...ateTime(3pm)
π Rose::Object::MakeMethods::DateTime β the whole vibe π
okay bestie, gather round, because this is the Rose::Object::MakeMethods::DateTime page from the User Contributed Perl Documentation, and it's genuinely iconic. it's a method maker that spins up methods which store DateTime objects for you. no cap. π°οΈβ¨
β¨ NAME β¨
Rose::Object::MakeMethods::DateTime β create methods that store DateTime objects. that's it. that's the whole job. and it slays at it. π
πΈ SYNOPSIS πΈ
here's the lil taste test, verbatim, don't touch a character:
package MyObject;
use Rose::Object::MakeMethods::DateTime
(
datetime =>
[
'birthday',
'arrival' => { tz => 'UTC' }
],
);
...
$obj = MyObject->new(birthday => '1/24/1984 1am');
$dt = $obj->birthday; # DateTime object
$bday = $obj->birthday(format => '%B %E'); # 'January 24th'
# Shortcut for $obj->birthday->clone->truncate(to => 'month');
$month = $obj->birthday(truncate => 'month');
$obj->birthday('blah'); # croaks - invalid date!
$obj->birthday('1999-04-31'); # croaks - invalid date!
see how $obj->birthday just hands you a whole DateTime object like it's nothing? main character energy fr fr. π
π¦ DESCRIPTION π¦
so Rose::Object::MakeMethods::DateTime is a method maker that inherits from Rose::Object::MakeMethods. if you wanna learn about the interface itself, go read the Rose::Object::MakeMethods documentation β that's where the whole system lives. this page is just here to describe the method types this module provides. π
β οΈ and this part matters: all methods work only with hash-based objects. hash-based or it's not happening. π
π METHODS TYPES π
π datetime
creates get/set methods for scalar attributes that store DateTime objects. so cute, so useful.
π Options
hash_keyβ the key inside the hash-based object where this attribute actually gets stored. defaults to the name of the method. (so if your method isbirthday, the key isbirthdayunless you say otherwise.)init_methodβ the name of the method to call when initializing the value of an undefined attribute. heads up: this option is only applicable when using theget_set_initinterface. defaults to the method name with the prefixinit_added.
this method should return a value that can be parsed by Rose::DateTime::Util's the parse_date() function. and if the return value is a DateTime object, it will have its time zone set (see the tz option below) using DateTime's set_time_zone() method. π
interfaceβ picks one of the two possible interfaces. defaults toget_set. choose your fighter. π₯tzβ the time zone of theDateTimeobject to be stored. if it's present, this value gets passed as the second argument toRose::DateTime::Util's theparse_date()function when creatingDateTimeobjects for storage. if it's absent,DateTimeobjects will use the default time zone of theRose::DateTime::Utilclass, which is set byRose::DateTime::Util'stime_zone()class method. see theRose::DateTime::Utildocumentation for more info. π
π Interfaces
get_set
creates a get/set accessor method for an object attribute that stores a DateTime object. the classic. the blueprint. π«Ά
when called with a single argument, that argument gets passed through Rose::DateTime::Util's parse_date() function in order to create the DateTime object that is stored. the current value of the attribute is returned. passing a value that Rose::DateTime::Util's parse_date() function doesn't understand causes a fatal error. so don't do that. π
when called with two arguments and the first argument is the string 'format', then the second argument is taken as a format specifier, which gets passed to Rose::DateTime::Util's format_date() function. the formatted string is returned. in other words, this:
$obj->birthday(format => '%m/%d/%Y');
is just a shortcut for this:
Rose::DateTime::Util::format_date($obj->birthday,
'%m/%d/%Y');
when called with two arguments and the first argument is the string 'truncate', then the second argument is taken as a truncation specifier, which gets passed to DateTime's truncate() method called on a clone of the existing DateTime object. the cloned, truncated DateTime object is returned. in other words, this:
$obj->birthday(truncate => 'month');
is just a shortcut for this:
$obj->birthday->clone->truncate(to => 'month');
π¨ passing more than two arguments, or passing two arguments where the first argument is not'format'or'truncate', will cause a fatal error. periodt.
get_set_init
behaves like the get_set interface unless the value of the attribute is undefined. in that case, the method specified by the init_method option gets called, the return value gets passed through Rose::DateTime::Util's parse_date() function, and the attribute is set to the return value. an init method that returns a value that Rose::DateTime::Util's parse_date() function doesn't understand will cause a fatal error. no notes. π
π Example:
okay here's the full flex, copy it exactly:
package MyObject;
use Rose::Object::MakeMethods::DateTime
(
datetime =>
[
'birthday',
'arrival' => { tz => 'UTC' }
],
'datetime --get_set_init' =>
[
'departure' => { tz => 'UTC' }
],
);
sub init_departure
{
DateTime->new(month => 1,
day => 10,
year => 2000,
time_zone => 'America/Chicago');
}
...
$obj = MyObject->new(birthday => '1/24/1984 1am');
$dt = $obj->birthday; # DateTime object
$bday = $obj->birthday(format => '%B %E'); # 'January 24th'
# Shortcut for $obj->birthday->clone->truncate(to => 'month');
$month = $obj->birthday(truncate => 'month');
$obj->birthday('blah'); # croaks - invalid date!
$obj->birthday('1999-04-31'); # croaks - invalid date!
# DateTime object with time zone set to UTC
$dt = $obj->arrival('2005-21-01 4pm');
# DateTime object with time zone set to UTC, not America/Chicago!
# Start with 2000-01-10T00:00:00 America/Chicago,
# then set_time_zone('UTC'),
# which results in: 2000-01-10T06:00:00 UTC
$dt = $obj->departure;
print $dt; # "2000-01-10T06:00:00"
the way that departure starts as 2000-01-10T00:00:00 in America/Chicago and then gets set_time_zone('UTC')'d into 2000-01-10T06:00:00 UTC... that's the plot twist, bestie. π§β¨
π AUTHOR π
John C. Siracusa (siracusa@gmail.com) β the icon who built this. π
π LICENSE π
Copyright (c) 2010 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.
and that's the whole page, babe. π«Ά
this was Rose::...ateTime(3pm) from perl v5.34.0, dated 2022-05-28 β shoutout to the version and the date, they're doing great. now go make some DateTime objects, you absolute legend. slay π
β¨π
π¦ where this page comes from
- debian bookworm: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: librose-object-perl 0.860-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms