.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. | will give a
.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.\"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "DateTime 3"
.TH DateTime 3 "2007-09-10" "perl v5.8.8" "User Contributed Perl Documentation"
.SH "NAME"
DateTime \- A date and time object
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& use DateTime;
.Ve
.PP
.Vb 9
\& $dt = DateTime->new( year => 1964,
\& month => 10,
\& day => 16,
\& hour => 16,
\& minute => 12,
\& second => 47,
\& nanosecond => 500000000,
\& time_zone => 'Asia/Taipei',
\& );
.Ve
.PP
.Vb 2
\& $dt = DateTime->from_epoch( epoch => $epoch );
\& $dt = DateTime->now; # same as ( epoch => time() )
.Ve
.PP
.Vb 2
\& $year = $dt->year;
\& $month = $dt->month; # 1-12 - also mon
.Ve
.PP
.Vb 1
\& $day = $dt->day; # 1-31 - also day_of_month, mday
.Ve
.PP
.Vb 1
\& $dow = $dt->day_of_week; # 1-7 (Monday is 1) - also dow, wday
.Ve
.PP
.Vb 2
\& $hour = $dt->hour; # 0-23
\& $minute = $dt->minute; # 0-59 - also min
.Ve
.PP
.Vb 1
\& $second = $dt->second; # 0-61 (leap seconds!) - also sec
.Ve
.PP
.Vb 1
\& $doy = $dt->day_of_year; # 1-366 (leap years) - also doy
.Ve
.PP
.Vb 1
\& $doq = $dt->day_of_quarter; # 1.. - also doq
.Ve
.PP
.Vb 1
\& $qtr = $dt->quarter; # 1-4
.Ve
.PP
.Vb 2
\& # all of the start-at-1 methods above have correponding start-at-0
\& # methods, such as $dt->day_of_month_0, $dt->month_0 and so on
.Ve
.PP
.Vb 2
\& $ymd = $dt->ymd; # 2002-12-06
\& $ymd = $dt->ymd('/'); # 2002/12/06 - also date
.Ve
.PP
.Vb 2
\& $mdy = $dt->mdy; # 12-06-2002
\& $mdy = $dt->mdy('/'); # 12/06/2002
.Ve
.PP
.Vb 2
\& $dmy = $dt->dmy; # 06-12-2002
\& $dmy = $dt->dmy('/'); # 06/12/2002
.Ve
.PP
.Vb 2
\& $hms = $dt->hms; # 14:02:29
\& $hms = $dt->hms('!'); # 14!02!29 - also time
.Ve
.PP
.Vb 1
\& $is_leap = $dt->is_leap_year;
.Ve
.PP
.Vb 5
\& # these are localizable, see Locales section
\& $month_name = $dt->month_name; # January, February, ...
\& $month_abbr = $dt->month_abbr; # Jan, Feb, ...
\& $day_name = $dt->day_name; # Monday, Tuesday, ...
\& $day_abbr = $dt->day_abbr; # Mon, Tue, ...
.Ve
.PP
.Vb 3
\& $epoch_time = $dt->epoch;
\& # may return undef if the datetime is outside the range that is
\& # representable by your OS's epoch system.
.Ve
.PP
.Vb 1
\& $dt2 = $dt + $duration_object;
.Ve
.PP
.Vb 1
\& $dt3 = $dt - $duration_object;
.Ve
.PP
.Vb 1
\& $duration_object = $dt - $dt2;
.Ve
.PP
.Vb 1
\& $dt->set( year => 1882 );
.Ve
.PP
.Vb 1
\& $dt->set_time_zone( 'America/Chicago' );
.Ve
.PP
.Vb 1
\& $dt->set_formatter( $formatter );
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
DateTime is a class for the representation of date/time combinations,
and is part of the Perl DateTime project. For details on this project
please see . The DateTime site has a \s-1FAQ\s0
which may help answer many \*(L"how do I do X?\*(R" questions. The \s-1FAQ\s0 is at
.
.PP
It represents the Gregorian calendar, extended backwards in time
before its creation (in 1582). This is sometimes known as the
\&\*(L"proleptic Gregorian calendar\*(R". In this calendar, the first day of
the calendar (the epoch), is the first day of year 1, which
corresponds to the date which was (incorrectly) believed to be the
birth of Jesus Christ.
.PP
The calendar represented does have a year 0, and in that way differs
from how dates are often written using \*(L"\s-1BCE/CE\s0\*(R" or \*(L"\s-1BC/AD\s0\*(R".
.PP
For infinite datetimes, please see the
DateTime::Infinite module.
.SH "USAGE"
.IX Header "USAGE"
.Sh "0\-based Versus 1\-based Numbers"
.IX Subsection "0-based Versus 1-based Numbers"
The DateTime.pm module follows a simple consistent logic for
determining whether or not a given number is 0\-based or 1\-based.
.PP
Month, day of month, day of week, and day of year are 1\-based. Any
method that is 1\-based also has an equivalent 0\-based method ending in
\&\*(L"_0\*(R". So for example, this class provides both \f(CW\*(C`day_of_week()\*(C'\fR and
\&\f(CW\*(C`day_of_week_0()\*(C'\fR methods.
.PP
The \f(CW\*(C`day_of_week_0()\*(C'\fR method still treats Monday as the first day of
the week.
.PP
All \fItime\fR\-related numbers such as hour, minute, and second are
0\-based.
.PP
Years are neither, as they can be both positive or negative, unlike
any other datetime component. There \fIis\fR a year 0.
.PP
There is no \f(CW\*(C`quarter_0()\*(C'\fR method.
.Sh "Error Handling"
.IX Subsection "Error Handling"
Some errors may cause this module to die with an error string. This
can only happen when calling constructor methods, methods that change
the object, such as \f(CW\*(C`set()\*(C'\fR, or methods that take parameters.
Methods that retrieve information about the object, such as \f(CW\*(C`year()\*(C'\fR
or \f(CW\*(C`epoch()\*(C'\fR, will never die.
.Sh "Locales"
.IX Subsection "Locales"
All the object methods which return names or abbreviations return data
based on a locale. This is done by setting the locale when
constructing a DateTime object. There is also a \f(CW\*(C`DefaultLocale()\*(C'\fR
class method which may be used to set the default locale for all
DateTime objects created. If this is not set, then \*(L"en_US\*(R" is used.
.PP
Some locales may return data as Unicode. When using Perl 5.6.0 or
greater, this will be a native Perl Unicode string. When using older
Perls, this will be a sequence of bytes representing the Unicode
character.
.Sh "Floating DateTimes"
.IX Subsection "Floating DateTimes"
The default time zone for new DateTime objects, except where stated
otherwise, is the \*(L"floating\*(R" time zone. This concept comes from the
iCal standard. A floating datetime is one which is not anchored to
any particular time zone. In addition, floating datetimes do not
include leap seconds, since we cannot apply them without knowing the
datetime's time zone.
.PP
The results of date math and comparison between a floating datetime
and one with a real time zone are not really valid, because one
includes leap seconds and the other does not. Similarly, the results
of datetime math between two floating datetimes and two datetimes with
time zones are not really comparable.
.PP
If you are planning to use any objects with a real time zone, it is
strongly recommended that you \fBdo not\fR mix these with floating
datetimes.
.Sh "Math"
.IX Subsection "Math"
If you are going to be using doing date math, please read the section
\&\*(L"How Datetime Math is Done\*(R".
.Sh "Time Zone Warning"
.IX Subsection "Time Zone Warning"
Do not try to use named time zones (like \*(L"America/Chicago\*(R") with dates
very far in the future (thousands of years). The current
implementation of \f(CW\*(C`DateTime::TimeZone\*(C'\fR will use a huge amount of
memory calculating all the \s-1DST\s0 changes from now until the future
date. Use \s-1UTC\s0 or the floating time zone and you will be safe.
.Sh "Methods"
.IX Subsection "Methods"
\fIConstructors\fR
.IX Subsection "Constructors"
.PP
All constructors can die when invalid parameters are given.
.IP "* new( ... )" 4
.IX Item "new( ... )"
This class method accepts parameters for each date and time component:
\&\*(L"year\*(R", \*(L"month\*(R", \*(L"day\*(R", \*(L"hour\*(R", \*(L"minute\*(R", \*(L"second\*(R", \*(L"nanosecond\*(R".
It also accepts \*(L"locale\*(R", \*(L"time_zone\*(R", and \*(L"formatter\*(R" parameters.
.Sp
.Vb 9
\& my $dt = DateTime->new( year => 1066,
\& month => 10,
\& day => 25,
\& hour => 7,
\& minute => 15,
\& second => 47,
\& nanosecond => 500000000,
\& time_zone => 'America/Chicago',
\& );
.Ve
.Sp
DateTime validates the \*(L"month\*(R", \*(L"day\*(R", \*(L"hour\*(R", \*(L"minute\*(R", and \*(L"second\*(R",
and \*(L"nanosecond\*(R" parameters. The valid values for these parameters are:
.RS 4
.IP "* month" 8
.IX Item "month"
1\-12
.IP "* day" 8
.IX Item "day"
1\-31, and it must be within the valid range of days for the specified
month
.IP "* hour" 8
.IX Item "hour"
0\-23
.IP "* minute" 8
.IX Item "minute"
0\-59
.IP "* second" 8
.IX Item "second"
0\-61 (to allow for leap seconds). Values of 60 or 61 are only allowed
when they match actual leap seconds.
.IP "* nanosecond" 8
.IX Item "nanosecond"
>= 0
.RE
.RS 4
.RE
.PP
Invalid parameter types (like an array reference) will cause the
constructor to die.
.PP
The value for seconds may be from 0 to 61, to account for leap
seconds. If you give a value greater than 59, DateTime does check to
see that it really matches a valid leap second.
.PP
All of the parameters are optional except for \*(L"year\*(R". The \*(L"month\*(R" and
\&\*(L"day\*(R" parameters both default to 1, while the \*(L"hour\*(R", \*(L"minute\*(R",
\&\*(L"second\*(R", and \*(L"nanosecond\*(R" parameters all default to 0.
.PP
The \*(L"locale\*(R" parameter should be a string matching one of the valid
locales, or a \f(CW\*(C`DateTime::Locale\*(C'\fR object. See the
DateTime::Locale documentation for details.
.PP
The time_zone parameter can be either a scalar or a
\&\f(CW\*(C`DateTime::TimeZone\*(C'\fR object. A string will simply be passed to the
\&\f(CW\*(C`DateTime::TimeZone\->new\*(C'\fR method as its \*(L"name\*(R" parameter. This
string may be an Olson \s-1DB\s0 time zone name (\*(L"America/Chicago\*(R"), an
offset string (\*(L"+0630\*(R"), or the words \*(L"floating\*(R" or \*(L"local\*(R". See the
\&\f(CW\*(C`DateTime::TimeZone\*(C'\fR documentation for more details.
.PP
The default time zone is \*(L"floating\*(R".
.PP
The \*(L"formatter\*(R" can be either a scalar or an object, but the class
specified by the scalar or the object must implement a
\&\f(CW\*(C`format_datetime()\*(C'\fR method.
.PP
Ambiguous Local Times
.IX Subsection "Ambiguous Local Times"
.PP
Because of Daylight Saving Time, it is possible to specify a local
time that is ambiguous. For example, in the \s-1US\s0 in 2003, the
transition from to saving to standard time occurred on October 26, at
02:00:00 local time. The local clock changed from 01:59:59 (saving
time) to 01:00:00 (standard time). This means that the hour from
01:00:00 through 01:59:59 actually occurs twice, though the \s-1UTC\s0 time
continues to move forward.
.PP
If you specify an ambiguous time, then the latest \s-1UTC\s0 time is always
used, in effect always choosing standard time. In this case, you can
simply subtract an hour to the object in order to move to saving time,
for example:
.PP
.Vb 9
\& # This object represent 01:30:00 standard time
\& my $dt = DateTime->new( year => 2003,
\& month => 10,
\& day => 26,
\& hour => 1,
\& minute => 30,
\& second => 0,
\& time_zone => 'America/Chicago',
\& );
.Ve
.PP
.Vb 1
\& print $dt->hms; # prints 01:30:00
.Ve
.PP
.Vb 2
\& # Now the object represent 01:30:00 saving time
\& $dt->subtract( hours => 1 );
.Ve
.PP
.Vb 1
\& print $dt->hms; # still prints 01:30:00
.Ve
.PP
Alternately, you could create the object with the \s-1UTC\s0 time zone, and
then call the \f(CW\*(C`set_time_zone()\*(C'\fR method to change the time zone. This
is a good way to ensure that the time is not ambiguous.
.PP
Invalid Local Times
.IX Subsection "Invalid Local Times"
.PP
Another problem introduced by Daylight Saving Time is that certain
local times just do not exist. For example, in the \s-1US\s0 in 2003, the
transition from standard to saving time occurred on April 6, at the
change to 2:00:00 local time. The local clock changes from 01:59:59
(standard time) to 03:00:00 (saving time). This means that there is
no 02:00:00 through 02:59:59 on April 6!
.PP
Attempting to create an invalid time currently causes a fatal error.
This may change in future version of this module.
.ie n .IP "* from_epoch( epoch => $epoch, ... )" 4
.el .IP "* from_epoch( epoch => \f(CW$epoch\fR, ... )" 4
.IX Item "from_epoch( epoch => $epoch, ... )"
This class method can be used to construct a new DateTime object from
an epoch time instead of components. Just as with the \f(CW\*(C`new()\*(C'\fR
method, it accepts \*(L"time_zone\*(R", \*(L"locale\*(R", and \*(L"formatter\*(R" parameters.
.Sp
If the epoch value is not an integer, the part after the decimal will
be converted to nanoseconds. This is done in order to be compatible
with \f(CW\*(C`Time::HiRes\*(C'\fR. If the floating portion extends past 9 decimal
places, it will be truncated to nine, so that 1.1234567891 will become
1 second and 123,456,789 nanoseconds.
.Sp
By default, the returned object will be in the \s-1UTC\s0 time zone.
.IP "* now( ... )" 4
.IX Item "now( ... )"
This class method is equivalent to calling \f(CW\*(C`from_epoch()\*(C'\fR with the
value returned from Perl's \f(CW\*(C`time()\*(C'\fR function. Just as with the
\&\f(CW\*(C`new()\*(C'\fR method, it accepts \*(L"time_zone\*(R" and \*(L"locale\*(R" parameters.
.Sp
By default, the returned object will be in the \s-1UTC\s0 time zone.
.IP "* today( ... )" 4
.IX Item "today( ... )"
This class method is equivalent to:
.Sp
.Vb 1
\& DateTime->now->truncate( to => 'day' );
.Ve
.ie n .IP "* from_object( object => $object, ... )" 4
.el .IP "* from_object( object => \f(CW$object\fR, ... )" 4
.IX Item "from_object( object => $object, ... )"
This class method can be used to construct a new DateTime object from
any object that implements the \f(CW\*(C`utc_rd_values()\*(C'\fR method. All
\&\f(CW\*(C`DateTime::Calendar\*(C'\fR modules must implement this method in order to
provide cross-calendar compatibility. This method accepts a
\&\*(L"locale\*(R" and \*(L"formatter\*(R" parameter
.Sp
If the object passed to this method has a \f(CW\*(C`time_zone()\*(C'\fR method, that
is used to set the time zone of the newly created \f(CW\*(C`DateTime.pm\*(C'\fR
object.
.Sp
Otherwise, the returned object will be in the floating time zone.
.IP "* last_day_of_month( ... )" 4
.IX Item "last_day_of_month( ... )"
This constructor takes the same arguments as can be given to the
\&\f(CW\*(C`new()\*(C'\fR method, except for \*(L"day\*(R". Additionally, both \*(L"year\*(R" and
\&\*(L"month\*(R" are required.
.IP "* from_day_of_year( ... )" 4
.IX Item "from_day_of_year( ... )"
This constructor takes the same arguments as can be given to the
\&\f(CW\*(C`new()\*(C'\fR method, except that it does not accept a \*(L"month\*(R" or \*(L"day\*(R"
argument. Instead, it requires both \*(L"year\*(R" and \*(L"day_of_year\*(R". The
day of year must be between 1 and 366, and 366 is only allowed for
leap years.
.IP "* clone" 4
.IX Item "clone"
This object method returns a new object that is replica of the object
upon which the method is called.
.PP
\fI\*(L"Get\*(R" Methods\fR
.IX Subsection "Get Methods"
.PP
This class has many methods for retrieving information about an
object.
.IP "* year" 4
.IX Item "year"
Returns the year.
.IP "* ce_year" 4
.IX Item "ce_year"
Returns the year according to the \s-1BCE/CE\s0 numbering system. The year
before year 1 in this system is year \-1, aka \*(L"1 \s-1BCE\s0\*(R".
.IP "* era_name" 4
.IX Item "era_name"
Returns the long name of the current era, something like \*(L"Before
Christ\*(R". See the Locales section for more details.
.IP "* era_abbr" 4
.IX Item "era_abbr"
Returns the abbreviated name of the current era, something like \*(L"\s-1BC\s0\*(R".
See the Locales section for more details.
.IP "* christian_era" 4
.IX Item "christian_era"
Returns a string, either \*(L"\s-1BC\s0\*(R" or \*(L"\s-1AD\s0\*(R", according to the year.
.IP "* secular_era" 4
.IX Item "secular_era"
Returns a string, either \*(L"\s-1BCE\s0\*(R" or \*(L"\s-1CE\s0\*(R", according to the year.
.IP "* year_with_era" 4
.IX Item "year_with_era"
Returns a string containing the year immediately followed by its era
abbreviation. The year is the absolute value of \f(CW\*(C`ce_year()\*(C'\fR, so that
year 1 is \*(L"1BC\*(R" and year 0 is \*(L"1AD\*(R".
.IP "* year_with_christian_era" 4
.IX Item "year_with_christian_era"
Like \f(CW\*(C`year_with_era()\*(C'\fR, but uses the \fIchristian_era()\fR to get the era
name.
.IP "* year_with_secular_era" 4
.IX Item "year_with_secular_era"
Like \f(CW\*(C`year_with_era()\*(C'\fR, but uses the \fIsecular_era()\fR method to get the
era name.
.IP "* month, mon" 4
.IX Item "month, mon"
Returns the month of the year, from 1..12.
.IP "* month_name" 4
.IX Item "month_name"
Returns the name of the current month. See the
Locales section for more details.
.IP "* month_abbr" 4
.IX Item "month_abbr"
Returns the abbreviated name of the current month. See the
Locales section for more details.
.IP "* day_of_month, day, mday" 4
.IX Item "day_of_month, day, mday"
Returns the day of the month, from 1..31.
.IP "* day_of_week, wday, dow" 4
.IX Item "day_of_week, wday, dow"
Returns the day of the week as a number, from 1..7, with 1 being
Monday and 7 being Sunday.
.IP "* day_name" 4
.IX Item "day_name"
Returns the name of the current day of the week. See the
Locales section for more details.
.IP "* day_abbr" 4
.IX Item "day_abbr"
Returns the abbreviated name of the current day of the week. See the
Locales section for more details.
.IP "* day_of_year, doy" 4
.IX Item "day_of_year, doy"
Returns the day of the year.
.IP "* quarter" 4
.IX Item "quarter"
Returns the quarter of the year, from 1..4.
.IP "* quarter_name" 4
.IX Item "quarter_name"
Returns the name of the current quarter. See the
Locales section for more details.
.IP "* quarter_abbr" 4
.IX Item "quarter_abbr"
Returns the abbreviated name of the current quarter. See the
Locales section for more details.
.IP "* day_of_quarter, doq" 4
.IX Item "day_of_quarter, doq"
Returns the day of the quarter.
.IP "* weekday_of_month" 4
.IX Item "weekday_of_month"
Returns a number from 1..5 indicating which week day of the month this
is. For example, June 9, 2003 is the second Monday of the month, and
so this method returns 2 for that day.
.ie n .IP "* ymd( $optional_separator ), date" 4
.el .IP "* ymd( \f(CW$optional_separator\fR ), date" 4
.IX Item "ymd( $optional_separator ), date"
.PD 0
.ie n .IP "* mdy( $optional_separator )" 4
.el .IP "* mdy( \f(CW$optional_separator\fR )" 4
.IX Item "mdy( $optional_separator )"
.ie n .IP "* dmy( $optional_separator )" 4
.el .IP "* dmy( \f(CW$optional_separator\fR )" 4
.IX Item "dmy( $optional_separator )"
.PD
Each method returns the year, month, and day, in the order indicated
by the method name. Years are zero-padded to four digits. Months and
days are 0\-padded to two digits.
.Sp
By default, the values are separated by a dash (\-), but this can be
overridden by passing a value to the method.
.IP "* hour" 4
.IX Item "hour"
Returns the hour of the day, from 0..23.
.IP "* hour_1" 4
.IX Item "hour_1"
Returns the hour of the day, from 1..24.
.IP "* hour_12" 4
.IX Item "hour_12"
Returns the hour of the day, from 1..12.
.IP "* hour_12_0" 4
.IX Item "hour_12_0"
Returns the hour of the day, from 0..11.
.IP "* minute, min" 4
.IX Item "minute, min"
Returns the minute of the hour, from 0..59.
.IP "* second, sec" 4
.IX Item "second, sec"
Returns the second, from 0..61. The values 60 and 61 are used for
leap seconds.
.IP "* fractional_second" 4
.IX Item "fractional_second"
Returns the second, as a real number from 0.0 until 61.999999999
.Sp
The values 60 and 61 are used for leap seconds.
.IP "* millisecond" 4
.IX Item "millisecond"
Returns the fractional part of the second as milliseconds (1E\-3 seconds).
.Sp
Half a second is 500 milliseconds.
.IP "* microsecond" 4
.IX Item "microsecond"
Returns the fractional part of the second as microseconds (1E\-6
seconds). This value will be rounded to an integer.
.Sp
Half a second is 500_000 microseconds. This value will be rounded to
an integer.
.IP "* nanosecond" 4
.IX Item "nanosecond"
Returns the fractional part of the second as nanoseconds (1E\-9 seconds).
.Sp
Half a second is 500_000_000 nanoseconds.
.ie n .IP "* hms( $optional_separator ), time" 4
.el .IP "* hms( \f(CW$optional_separator\fR ), time" 4
.IX Item "hms( $optional_separator ), time"
Returns the hour, minute, and second, all zero-padded to two digits.
If no separator is specified, a colon (:) is used by default.
.IP "* datetime, iso8601" 4
.IX Item "datetime, iso8601"
This method is equivalent to:
.Sp
.Vb 1
\& $dt->ymd('-') . 'T' . $dt->hms(':')
.Ve
.IP "* is_leap_year" 4
.IX Item "is_leap_year"
This method returns a true or false indicating whether or not the
datetime object is in a leap year.
.IP "* week" 4
.IX Item "week"
.Vb 1
\& ($week_year, $week_number) = $dt->week;
.Ve
.Sp
Returns information about the calendar week which contains this
datetime object. The values returned by this method are also available
separately through the week_year and week_number methods.
.Sp
The first week of the year is defined by \s-1ISO\s0 as the one which contains
the fourth day of January, which is equivalent to saying that it's the
first week to overlap the new year by at least four days.
.Sp
Typically the week year will be the same as the year that the object
is in, but dates at the very beginning of a calendar year often end up
in the last week of the prior year, and similarly, the final few days
of the year may be placed in the first week of the next year.
.IP "* week_year" 4
.IX Item "week_year"
Returns the year of the week.
.IP "* week_number" 4
.IX Item "week_number"
Returns the week of the year, from 1..53.
.IP "* week_of_month" 4
.IX Item "week_of_month"
The week of the month, from 0..5. The first week of the month is the
first week that contains a Thursday. This is based on the \s-1ICU\s0
definition of week of month, and correlates to the \s-1ISO8601\s0 week of
year definition. A day in the week \fIbefore\fR the week with the first
Thursday will be week 0.
.IP "* jd, mjd" 4
.IX Item "jd, mjd"
These return the Julian Day and Modified Julian Day, respectively.
The value returned is a floating point number. The fractional portion
of the number represents the time portion of the datetime.
.IP "* time_zone" 4
.IX Item "time_zone"
This returns the \f(CW\*(C`DateTime::TimeZone\*(C'\fR object for the datetime object.
.IP "* offset" 4
.IX Item "offset"
This returns the offset from \s-1UTC\s0, in seconds, of the datetime object
according to the time zone.
.IP "* is_dst" 4
.IX Item "is_dst"
Returns a boolean indicating whether or not the datetime object is
currently in Daylight Saving Time or not.
.IP "* time_zone_long_name" 4
.IX Item "time_zone_long_name"
This is a shortcut for \f(CW\*(C`$dt\->time_zone\->name\*(C'\fR. It's provided so
that one can use \*(L"%{time_zone_long_name}\*(R" as a strftime format
specifier.
.IP "* time_zone_short_name" 4
.IX Item "time_zone_short_name"
This method returns the time zone abbreviation for the current time
zone, such as \*(L"\s-1PST\s0\*(R" or \*(L"\s-1GMT\s0\*(R". These names are \fBnot\fR definitive, and
should not be used in any application intended for general use by
users around the world.
.ie n .IP "* strftime( $format, ... )" 4
.el .IP "* strftime( \f(CW$format\fR, ... )" 4
.IX Item "strftime( $format, ... )"
This method implements functionality similar to the \f(CW\*(C`strftime()\*(C'\fR
method in C. However, if given multiple format strings, then it will
return multiple scalars, one for each format string.
.Sp
See the strftime Specifiers section for a list
of all possible format specifiers.
.Sp
If you give a format specifier that doesn't exist, then it is simply
treated as text.
.IP "* epoch" 4
.IX Item "epoch"
Return the \s-1UTC\s0 epoch value for the datetime object. Internally, this
is implemented using \f(CW\*(C`Time::Local\*(C'\fR, which uses the Unix epoch even on
machines with a different epoch (such as MacOS). Datetimes before the
start of the epoch will be returned as a negative number.
.Sp
This return value from this method is always an integer.
.Sp
Since the epoch does not account for leap seconds, the epoch time for
1972\-12\-31T23:59:60 (\s-1UTC\s0) is exactly the same as that for
1973\-01\-01T00:00:00.
.Sp
Epoch times cannot represent many dates on most platforms, and this
method may simply return undef in some cases.
.Sp
Using your system's epoch time may be error\-prone, since epoch times
have such a limited range on 32\-bit machines. Additionally, the fact
that different operating systems have different epoch beginnings is
another source of possible bugs.
.IP "* hires_epoch" 4
.IX Item "hires_epoch"
Returns the epoch as a floating point number. The floating point
portion of the value represents the nanosecond value of the object.
This method is provided for compatibility with the \f(CW\*(C`Time::HiRes\*(C'\fR
module.
.IP "* is_finite, is_infinite" 4
.IX Item "is_finite, is_infinite"
These methods allow you to distinguish normal datetime objects from
infinite ones. Infinite datetime objects are documented in
DateTime::Infinite.
.IP "* utc_rd_values" 4
.IX Item "utc_rd_values"
Returns the current \s-1UTC\s0 Rata Die days, seconds, and nanoseconds as a
three element list. This exists primarily to allow other calendar
modules to create objects based on the values provided by this object.
.IP "* local_rd_values" 4
.IX Item "local_rd_values"
Returns the current local Rata Die days, seconds, and nanoseconds as a
three element list. This exists for the benefit of other modules
which might want to use this information for date math, such as
\&\f(CW\*(C`DateTime::Event::Recurrence\*(C'\fR.
.IP "* leap_seconds" 4
.IX Item "leap_seconds"
Returns the number of leap seconds that have happened up to the
datetime represented by the object. For floating datetimes, this
always returns 0.
.IP "* utc_rd_as_seconds" 4
.IX Item "utc_rd_as_seconds"
Returns the current \s-1UTC\s0 Rata Die days and seconds purely as seconds.
This number ignores any fractional seconds stored in the object,
as well as leap seconds.
.IP "* local_rd_as_seconds \- deprecated" 4
.IX Item "local_rd_as_seconds - deprecated"
Returns the current local Rata Die days and seconds purely as seconds.
This number ignores any fractional seconds stored in the object,
as well as leap seconds.
.IP "* locale" 4
.IX Item "locale"
Returns the current locale object.
.IP "* formatter" 4
.IX Item "formatter"
Returns current formatter object or class. See \*(L"Formatters And Stringification\*(R" for details.
.PP
\fI\*(L"Set\*(R" Methods\fR
.IX Subsection "Set Methods"
.PP
The remaining methods provided by \f(CW\*(C`DateTime.pm\*(C'\fR, except where otherwise
specified, return the object itself, thus making method chaining
possible. For example:
.PP
.Vb 1
\& my $dt = DateTime->now->set_time_zone( 'Australia/Sydney' );
.Ve
.PP
.Vb 4
\& my $first = DateTime
\& ->last_day_of_month( year => 2003, month => 3 )
\& ->add( days => 1 )
\& ->subtract( seconds => 1 );
.Ve
.IP "* set( .. )" 4
.IX Item "set( .. )"
This method can be used to change the local components of a date time,
or its locale. This method accepts any parameter allowed by the
\&\f(CW\*(C`new()\*(C'\fR method except for \*(L"time_zone\*(R". Time zones may be set using
the \f(CW\*(C`set_time_zone()\*(C'\fR method.
.Sp
This method performs parameters validation just as is done in the
\&\f(CW\*(C`new()\*(C'\fR method.
.IP "* \fIset_year()\fR, \fIset_month()\fR, \fIset_day()\fR, \fIset_hour()\fR, \fIset_minute()\fR, \fIset_second()\fR, \fIset_nanosecond()\fR, \fIset_locale()\fR" 4
.IX Item "set_year(), set_month(), set_day(), set_hour(), set_minute(), set_second(), set_nanosecond(), set_locale()"
These are shortcuts to calling \f(CW\*(C`set()\*(C'\fR with a single key. They all
take a single parameter.
.IP "* truncate( to => ... )" 4
.IX Item "truncate( to => ... )"
This method allows you to reset some of the local time components in
the object to their \*(L"zero\*(R" values. The \*(L"to\*(R" parameter is used to
specify which values to truncate, and it may be one of \*(L"year\*(R",
\&\*(L"month\*(R", \*(L"week\*(R", \*(L"day\*(R", \*(L"hour\*(R", \*(L"minute\*(R", or \*(L"second\*(R". For example,
if \*(L"month\*(R" is specified, then the local day becomes 1, and the hour,
minute, and second all become 0.
.Sp
If \*(L"week\*(R" is given, then the datetime is set to the beginning of the
week in which it occurs, and the time components are all set to 0.
.ie n .IP "* set_time_zone( $tz )" 4
.el .IP "* set_time_zone( \f(CW$tz\fR )" 4
.IX Item "set_time_zone( $tz )"
This method accepts either a time zone object or a string that can be
passed as the \*(L"name\*(R" parameter to \f(CW\*(C`DateTime::TimeZone\->new()\*(C'\fR.
If the new time zone's offset is different from the old time zone,
then the \fIlocal\fR time is adjusted accordingly.
.Sp
For example:
.Sp
.Vb 3
\& my $dt = DateTime->new( year => 2000, month => 5, day => 10,
\& hour => 15, minute => 15,
\& time_zone => 'America/Los_Angeles', );
.Ve
.Sp
.Vb 1
\& print $dt->hour; # prints 15
.Ve
.Sp
.Vb 1
\& $dt->set_time_zone( 'America/Chicago' );
.Ve
.Sp
.Vb 1
\& print $dt->hour; # prints 17
.Ve
.Sp
If the old time zone was a floating time zone, then no adjustments to
the local time are made, except to account for leap seconds. If the
new time zone is floating, then the \fI\s-1UTC\s0\fR time is adjusted in order
to leave the local time untouched.
.Sp
Fans of Tsai Ming\-Liang's films will be happy to know that this does
work:
.Sp
.Vb 1
\& my $dt = DateTime->now( time_zone => 'Asia/Taipei' );
.Ve
.Sp
.Vb 1
\& $dt->set_time_zone( 'Europe/Paris' );
.Ve
.Sp
Yes, now we can know \*(L"ni3 na4 bian1 ji2dian3?\*(R"
.ie n .IP "* set_formatter( $formatter )" 4
.el .IP "* set_formatter( \f(CW$formatter\fR )" 4
.IX Item "set_formatter( $formatter )"
Set the formatter for the object. See \*(L"Formatters And Stringification\*(R" for details.
.ie n .IP "* add_duration( $duration_object )" 4
.el .IP "* add_duration( \f(CW$duration_object\fR )" 4
.IX Item "add_duration( $duration_object )"
This method adds a \f(CW\*(C`DateTime::Duration\*(C'\fR to the current datetime. See
the DateTime::Duration docs for more details.
.IP "* add( DateTime::Duration\->new parameters )" 4
.IX Item "add( DateTime::Duration->new parameters )"
This method is syntactic sugar around the \f(CW\*(C`add_duration()\*(C'\fR method. It
simply creates a new \f(CW\*(C`DateTime::Duration\*(C'\fR object using the parameters
given, and then calls the \f(CW\*(C`add_duration()\*(C'\fR method.
.ie n .IP "* subtract_duration( $duration_object )" 4
.el .IP "* subtract_duration( \f(CW$duration_object\fR )" 4
.IX Item "subtract_duration( $duration_object )"
When given a \f(CW\*(C`DateTime::Duration\*(C'\fR object, this method simply calls
\&\f(CW\*(C`invert()\*(C'\fR on that object and passes that new duration to the
\&\f(CW\*(C`add_duration\*(C'\fR method.
.IP "* subtract( DateTime::Duration\->new parameters )" 4
.IX Item "subtract( DateTime::Duration->new parameters )"
Like \f(CW\*(C`add()\*(C'\fR, this is syntactic sugar for the \f(CW\*(C`subtract_duration()\*(C'\fR
method.
.ie n .IP "* subtract_datetime( $datetime )" 4
.el .IP "* subtract_datetime( \f(CW$datetime\fR )" 4
.IX Item "subtract_datetime( $datetime )"
This method returns a new \f(CW\*(C`DateTime::Duration\*(C'\fR object representing
the difference between the two dates. The duration is \fBrelative\fR to
the object from which \f(CW$datetime\fR is subtracted. For example:
.Sp
.Vb 2
\& 2003-03-15 00:00:00.00000000
\& - 2003-02-15 00:00:00.00000000
.Ve
.Sp
.Vb 1
\& -------------------------------
.Ve
.Sp
.Vb 1
\& = 1 month
.Ve
.Sp
Note that this duration is not an absolute measure of the amount of
time between the two datetimes, because the length of a month varies,,
as well as due to the presence of leap seconds.
.Sp
The returned duration may have deltas for months, days, minutes,
seconds, and nanoseconds.
.ie n .IP "* delta_md( $datetime )" 4
.el .IP "* delta_md( \f(CW$datetime\fR )" 4
.IX Item "delta_md( $datetime )"
.PD 0
.ie n .IP "* delta_days( $datetime )" 4
.el .IP "* delta_days( \f(CW$datetime\fR )" 4
.IX Item "delta_days( $datetime )"
.PD
Each of these methods returns a new \f(CW\*(C`DateTime::Duration\*(C'\fR object
representing some portion of the difference between two datetimes.
The \f(CW\*(C`delta_md()\*(C'\fR method returns a duration which contains only the
month and day portions of the duration is represented. The
\&\f(CW\*(C`delta_days()\*(C'\fR method returns a duration which contains only days.
.Sp
The \f(CW\*(C`delta_md\*(C'\fR and \f(CW\*(C`delta_days\*(C'\fR methods truncate the duration so
that any fractional portion of a day is ignored. Both of these
methods operate on the date portion of a datetime only, and so
effectively ignore the time zone.
.Sp
Unlike the subtraction methods, \fBthese methods always return a
positive (or zero) duration\fR.
.ie n .IP "* delta_ms( $datetime )" 4
.el .IP "* delta_ms( \f(CW$datetime\fR )" 4
.IX Item "delta_ms( $datetime )"
Returns a duration which contains only minutes and seconds. Any day
and month differences to minutes are converted to minutes and seconds.
.Sp
\&\fBAlways return a positive (or zero) duration\fR.
.ie n .IP "* subtract_datetime_absolute( $datetime )" 4
.el .IP "* subtract_datetime_absolute( \f(CW$datetime\fR )" 4
.IX Item "subtract_datetime_absolute( $datetime )"
This method returns a new \f(CW\*(C`DateTime::Duration\*(C'\fR object representing
the difference between the two dates in seconds and nanoseconds. This
is the only way to accurately measure the absolute amount of time
between two datetimes, since units larger than a second do not
represent a fixed number of seconds.
.PP
\fIClass Methods\fR
.IX Subsection "Class Methods"
.ie n .IP "* DefaultLocale( $locale )" 4
.el .IP "* DefaultLocale( \f(CW$locale\fR )" 4
.IX Item "DefaultLocale( $locale )"
This can be used to specify the default locale to be used when
creating DateTime objects. If unset, then \*(L"en_US\*(R" is used.
.IP "* compare" 4
.IX Item "compare"
.PD 0
.IP "* compare_ignore_floating" 4
.IX Item "compare_ignore_floating"
.PD
.Vb 1
\& $cmp = DateTime->compare( $dt1, $dt2 );
.Ve
.Sp
.Vb 1
\& $cmp = DateTime->compare_ignore_floating( $dt1, $dt2 );
.Ve
.Sp
Compare two DateTime objects. The semantics are compatible with
Perl's \f(CW\*(C`sort()\*(C'\fR function; it returns \-1 if \f(CW$a\fR < \f(CW$b\fR, 0 if \f(CW$a\fR == \f(CW$b\fR, 1
if \f(CW$a\fR > \f(CW$b\fR.
.Sp
If one of the two DateTime objects has a floating time zone, it will
first be converted to the time zone of the other object. This is what
you want most of the time, but it can lead to inconsistent results
when you compare a number of DateTime objects, some of which are
floating, and some of which are in other time zones.
.Sp
If you want to have consistent results (because you want to sort a
number of objects, for example), you can use the
\&\f(CW\*(C`compare_ignore_floating()\*(C'\fR method:
.Sp
.Vb 1
\& @dates = sort { DateTime->compare_ignore_floating($a, $b) } @dates;
.Ve
.Sp
In this case, objects with a floating time zone will be sorted as if
they were \s-1UTC\s0 times.
.Sp
Since DateTime objects overload comparison operators, this:
.Sp
.Vb 1
\& @dates = sort @dates;
.Ve
.Sp
is equivalent to this:
.Sp
.Vb 1
\& @dates = sort { DateTime->compare($a, $b) } @dates;
.Ve
.Sp
DateTime objects can be compared to any other calendar class that
implements the \f(CW\*(C`utc_rd_values()\*(C'\fR method.
.Sh "How Datetime Math is Done"
.IX Subsection "How Datetime Math is Done"
It's important to have some understanding of how datetime math is
implemented in order to effectively use this module and
\&\f(CW\*(C`DateTime::Duration\*(C'\fR.
.PP
\fIMaking Things Simple\fR
.IX Subsection "Making Things Simple"
.PP
If you want to simplify your life and not have to think too hard about
the nitty-gritty of datetime math, I have several recommendations:
.IP "* use the floating time zone" 4
.IX Item "use the floating time zone"
If you do not care about time zones or leap seconds, use the
\&\*(L"floating\*(R" timezone:
.Sp
.Vb 1
\& my $dt = DateTime->now( time_zone => 'floating' );
.Ve
.Sp
Math done on two objects in the floating time zone produces very
predictable results.
.IP "* use \s-1UTC\s0 for all calculations" 4
.IX Item "use UTC for all calculations"
If you do care about time zones (particularly \s-1DST\s0) or leap seconds,
try to use non-UTC time zones for presentation and user input only.
Convert to \s-1UTC\s0 immediately and convert back to the local time zone for
presentation:
.Sp
.Vb 2
\& my $dt = DateTime->new( %user_input, time_zone => $user_tz );
\& $dt->set_time_zone('UTC');
.Ve
.Sp
.Vb 1
\& # do various operations - store it, retrieve it, add, subtract, etc.
.Ve
.Sp
.Vb 2
\& $dt->set_time_zone($user_tz);
\& print $dt->datetime;
.Ve
.IP "* math on non-UTC time zones" 4
.IX Item "math on non-UTC time zones"
If you need to do date math on objects with non-UTC time zones, please
read the caveats below carefully. The results \f(CW\*(C`DateTime.pm\*(C'\fR are
predictable and correct, and mostly intuitive, but datetime math gets
very ugly when time zones are involved, and there are a few strange
corner cases involving subtraction of two datetimes across a \s-1DST\s0
change.
.Sp
If you can always use the floating or \s-1UTC\s0 time zones, you can skip
ahead to Leap Seconds and Date Math
.IP "* date vs datetime math" 4
.IX Item "date vs datetime math"
If you only care about the date (calendar) portion of a datetime, you
should use either \f(CW\*(C`delta_md()\*(C'\fR or \f(CW\*(C`delta_days()\*(C'\fR, not
\&\f(CW\*(C`subtract_datetime()\*(C'\fR. This will give predictable, unsurprising
results, free from DST-related complications.
.IP "* \fIsubtract_datetime()\fR and \fIadd_duration()\fR" 4
.IX Item "subtract_datetime() and add_duration()"
You must convert your datetime objects to the \s-1UTC\s0 time zone before
doing date math if you want to make sure that the following formulas
are always true:
.Sp
.Vb 3
\& $dt2 - $dt1 = $dur
\& $dt1 + $dur = $dt2
\& $dt2 - $dur = $dt1
.Ve
.Sp
Note that using \f(CW\*(C`delta_days\*(C'\fR ensures that this formula always works,
regardless of the timezone of the objects involved, as does using
\&\f(CW\*(C`subtract_datetime_absolute()\*(C'\fR. Anything may sometimes be
non\-reversible.
.PP
\fIAdding a Duration to a Datetime\fR
.IX Subsection "Adding a Duration to a Datetime"
.PP
The parts of a duration can be broken down into five parts. These are
months, days, minutes, seconds, and nanoseconds. Adding one month to
a date is different than adding 4 weeks or 28, 29, 30, or 31 days.
Similarly, due to \s-1DST\s0 and leap seconds, adding a day can be different
than adding 86,400 seconds, and adding a minute is not exactly the
same as 60 seconds.
.PP
We cannot convert between these units, except for seconds and
nanoseconds, because there is no fixed conversion between the two
units, because of things like leap seconds, \s-1DST\s0 changes, etc.
.PP
\&\f(CW\*(C`DateTime.pm\*(C'\fR always adds (or subtracts) days, then months, minutes,
and then seconds and nanoseconds. If there are any boundary
overflows, these are normalized at each step. For the days and months
(the calendar) the local (not \s-1UTC\s0) values are used. For minutes and
seconds, the local values are used. This generally just works.
.PP
This means that adding one month and one day to February 28, 2003 will
produce the date April 1, 2003, not March 29, 2003.
.PP
.Vb 1
\& my $dt = DateTime->new( year => 2003, month => 2, day => 28 );
.Ve
.PP
.Vb 1
\& $dt->add( months => 1, days => 1 );
.Ve
.PP
.Vb 1
\& # 2003-04-01 - the result
.Ve
.PP
On the other hand, if we add months first, and then separately add
days, we end up with March 29, 2003:
.PP
.Vb 1
\& $dt->add( months => 1 )->add( days => 1 );
.Ve
.PP
.Vb 1
\& # 2003-03-29
.Ve
.PP
We see similar strangeness when math crosses a \s-1DST\s0 boundary:
.PP
.Vb 4
\& my $dt = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 1, minute => 58,
\& time_zone => "America/Chicago",
\& );
.Ve
.PP
.Vb 2
\& $dt->add( days => 1, minutes => 3 );
\& # 2003-04-06 02:01:00
.Ve
.PP
.Vb 2
\& $dt->add( minutes => 3 )->( days => 1 );
\& # 2003-04-06 03:01:00
.Ve
.PP
Note that if you converted the datetime object to \s-1UTC\s0 first you would
get predictable results.
.PP
If you want to know how many seconds a duration object represents, you
have to add it to a datetime to find out, so you could do:
.PP
.Vb 2
\& my $now = DateTime->now( time_zone => 'UTC' );
\& my $later = $now->clone->add_duration($duration);
.Ve
.PP
.Vb 1
\& my $seconds_dur = $later->subtract_datetime_absolute($now);
.Ve
.PP
This returns a duration which only contains seconds and nanoseconds.
.PP
If we were add the duration to a different datetime object we might
get a different number of seconds.
.PP
If you need to do lots of work with durations, take a look at Rick
Measham's \f(CW\*(C`DateTime::Format::Duration\*(C'\fR module, which lets you present
information from durations in many useful ways.
.PP
There are other subtract/delta methods in DateTime.pm to generate
different types of durations. These methods are
\&\f(CW\*(C`subtract_datetime()\*(C'\fR, \f(CW\*(C`subtract_datetime_absolute()\*(C'\fR,
\&\f(CW\*(C`delta_md()\*(C'\fR, \f(CW\*(C`delta_days()\*(C'\fR, and \f(CW\*(C`delta_ms()\*(C'\fR.
.PP
\fIDatetime Subtraction\fR
.IX Subsection "Datetime Subtraction"
.PP
Date subtraction is done solely based on the two object's local
datetimes, with one exception to handle \s-1DST\s0 changes. Also, if the two
datetime objects are in different time zones, one of them is converted
to the other's time zone first before subtraction. This is best
explained through examples:
.PP
The first of these probably makes the most sense:
.PP
.Vb 4
\& my $dt1 = DateTime->new( year => 2003, month => 5, day => 6,
\& time_zone => 'America/Chicago',
\& );
\& # not DST
.Ve
.PP
.Vb 4
\& my $dt2 = DateTime->new( year => 2003, month => 11, day => 6,
\& time_zone => 'America/Chicago',
\& );
\& # is DST
.Ve
.PP
.Vb 2
\& my $dur = $dt2->subtract_datetime($dt1);
\& # 6 months
.Ve
.PP
Nice and simple.
.PP
This one is a little trickier, but still fairly logical:
.PP
.Vb 5
\& my $dt1 = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 1, minute => 58,
\& time_zone => "America/Chicago",
\& );
\& # is DST
.Ve
.PP
.Vb 5
\& my $dt2 = DateTime->new( year => 2003, month => 4, day => 7,
\& hour => 2, minute => 1,
\& time_zone => "America/Chicago",
\& );
\& # not DST
.Ve
.PP
.Vb 2
\& my $dur = $dt2->subtract_datetime($dt1);
\& # 2 days and 3 minutes
.Ve
.PP
Which contradicts the result this one gives, even though they both
make sense:
.PP
.Vb 5
\& my $dt1 = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 1, minute => 58,
\& time_zone => "America/Chicago",
\& );
\& # is DST
.Ve
.PP
.Vb 5
\& my $dt2 = DateTime->new( year => 2003, month => 4, day => 6,
\& hour => 3, minute => 1,
\& time_zone => "America/Chicago",
\& );
\& # not DST
.Ve
.PP
.Vb 2
\& my $dur = $dt2->subtract_datetime($dt1);
\& # 1 day and 3 minutes
.Ve
.PP
This last example illustrates the \*(L"\s-1DST\s0\*(R" exception mentioned earlier.
The exception accounts for the fact 2003\-04\-06 only lasts 23 hours.
.PP
And finally:
.PP
.Vb 4
\& my $dt2 = DateTime->new( year => 2003, month => 10, day => 26,
\& hour => 1,
\& time_zone => 'America/Chicago',
\& );
.Ve
.PP
.Vb 1
\& my $dt1 = $dt2->clone->subtract( hours => 1 );
.Ve
.PP
.Vb 2
\& my $dur = $dt2->subtract_datetime($dt1);
\& # 60 minutes
.Ve
.PP
This seems obvious until you realize that subtracting 60 minutes from
\&\f(CW$dt2\fR in the above example still leaves the clock time at
\&\*(L"01:00:00\*(R". This time we are accounting for a 25 hour day.
.PP
\fIReversibility\fR
.IX Subsection "Reversibility"
.PP
Date math operations are not always reversible. This is because of
the way that addition operations are ordered. As was discussed
earlier, adding 1 day and 3 minutes in one call to \f(CW\*(C`add()\*(C'\fR is not the
same as first adding 3 minutes and 1 day in two separate calls.
.PP
If we take a duration returned from \f(CW\*(C`subtract_datetime()\*(C'\fR and then
try to add or subtract that duration from one of the datetimes we just
used, we sometimes get interesting results:
.PP
.Vb 4
\& my $dt1 = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 1, minute => 58,
\& time_zone => "America/Chicago",
\& );
.Ve
.PP
.Vb 4
\& my $dt2 = DateTime->new( year => 2003, month => 4, day => 6,
\& hour => 3, minute => 1,
\& time_zone => "America/Chicago",
\& );
.Ve
.PP
.Vb 2
\& my $dur = $dt2->subtract_datetime($dt1);
\& # 1 day and 3 minutes
.Ve
.PP
.Vb 2
\& $dt1->add_duration($dur);
\& # gives us $dt2
.Ve
.PP
.Vb 2
\& $dt2->subtract_duration($dur);
\& # gives us 2003-04-05 02:58:00 - 1 hour later than $dt1
.Ve
.PP
The \f(CW\*(C`subtract_dauration()\*(C'\fR operation gives us a (perhaps) unexpected
answer because it first subtracts one day to get 2003\-04\-05T03:01:00
and then subtracts 3 minutes to get the final result.
.PP
If we explicitly reverse the order we can get the original value of
\&\f(CW$dt1\fR. This can be facilitated by \f(CW\*(C`DateTime::Duration\*(C'\fR's
\&\f(CW\*(C`calendar_duration()\*(C'\fR and \f(CW\*(C`clock_duration()\*(C'\fR methods:
.PP
.Vb 2
\& $dt2->subtract_duration( $dur->clock_duration )
\& ->subtract_duration( $dur->calendar_duration );
.Ve
.PP
\fILeap Seconds and Date Math\fR
.IX Subsection "Leap Seconds and Date Math"
.PP
The presence of leap seconds can cause even more anomalies in date
math. For example, the following is a legal datetime:
.PP
.Vb 3
\& my $dt = DateTime->new( year => 1972, month => 12, day => 31,
\& hour => 23, minute => 59, second => 60,
\& time_zone => 'UTC' );
.Ve
.PP
If we do the following:
.PP
.Vb 1
\& $dt->add( months => 1 );
.Ve
.PP
Then the datetime is now \*(L"1973\-02\-01 00:00:00\*(R", because there is no
23:59:60 on 1973\-01\-31.
.PP
Leap seconds also force us to distinguish between minutes and seconds
during date math. Given the following datetime:
.PP
.Vb 3
\& my $dt = DateTime->new( year => 1972, month => 12, day => 31,
\& hour => 23, minute => 59, second => 30,
\& time_zone => 'UTC' );
.Ve
.PP
we will get different results when adding 1 minute than we get if we
add 60 seconds. This is because in this case, the last minute of the
day, beginning at 23:59:00, actually contains 61 seconds.
.PP
Here are the results we get:
.PP
.Vb 1
\& # 1972-12-31 23:59:30 - our starting datetime
.Ve
.PP
.Vb 2
\& $dt->clone->add( minutes => 1 );
\& # 1973-01-01 00:00:30 - one minute later
.Ve
.PP
.Vb 2
\& $dt->clone->add( seconds => 60 );
\& # 1973-01-01 00:00:29 - 60 seconds later
.Ve
.PP
.Vb 2
\& $dt->clone->add( seconds => 61 );
\& # 1973-01-01 00:00:30 - 61 seconds later
.Ve
.PP
\fILocal vs. \s-1UTC\s0 and 24 hours vs. 1 day\fR
.IX Subsection "Local vs. UTC and 24 hours vs. 1 day"
.PP
When math crosses a daylight saving boundary, a single day may have
more or less than 24 hours.
.PP
For example, if you do this:
.PP
.Vb 5
\& my $dt = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 2,
\& time_zone => 'America/Chicago',
\& );
\& $dt->add( days => 1 );
.Ve
.PP
then you will produce an \fIinvalid\fR local time, and therefore an
exception will be thrown.
.PP
However, this works:
.PP
.Vb 5
\& my $dt = DateTime->new( year => 2003, month => 4, day => 5,
\& hour => 2,
\& time_zone => 'America/Chicago',
\& );
\& $dt->add( hours => 24 );
.Ve
.PP
and produces a datetime with the local time of \*(L"03:00\*(R".
.PP
If all this makes your head hurt, there is a simple alternative. Just
convert your datetime object to the \*(L"\s-1UTC\s0\*(R" time zone before doing date
math on it, and switch it back to the local time zone afterwards.
This avoids the possibility of having date math throw an exception,
and makes sure that 1 day equals 24 hours. Of course, this may not
always be desirable, so caveat user!
.Sh "Overloading"
.IX Subsection "Overloading"
This module explicitly overloads the addition (+), subtraction (\-),
string and numeric comparison operators. This means that the
following all do sensible things:
.PP
.Vb 1
\& my $new_dt = $dt + $duration_obj;
.Ve
.PP
.Vb 1
\& my $new_dt = $dt - $duration_obj;
.Ve
.PP
.Vb 1
\& my $duration_obj = $dt - $new_dt;
.Ve
.PP
.Vb 1
\& foreach my $dt ( sort @dts ) { ... }
.Ve
.PP
Additionally, the fallback parameter is set to true, so other
derivable operators (+=, \-=, etc.) will work properly. Do not expect
increment (++) or decrement (\-\-) to do anything useful.
.PP
If you attempt to sort DateTime objects with non\-DateTime.pm objects
or scalars (strings, number, whatever) then an exception will be
thrown. Using the string comparison operators, \f(CW\*(C`eq\*(C'\fR or \f(CW\*(C`ne\*(C'\fR, to
compare a DateTime.pm always returns false.
.PP
The module also overloads stringification to use the \f(CW\*(C`iso8601()\*(C'\fR
method.
.Sh "Formatters And Stringification"
.IX Subsection "Formatters And Stringification"
You can optionally specify a \*(L"formatter\*(R", which is usually a
DateTime::Format::* object/class, to control how the stringification
of the DateTime object.
.PP
Any of the constructor methods can accept a formatter argument:
.PP
.Vb 2
\& my $formatter = DateTime::Format::Strptime->new(...);
\& my $dt = DateTime->new(year => 2004, formatter => $formatter);
.Ve
.PP
Or, you can set it afterwards:
.PP
.Vb 2
\& $dt->set_formatter($formatter);
\& $formatter = $dt->formatter();
.Ve
.PP
Once you set the formatter, the overloaded stringification method will
use the formatter. If unspecified, the \f(CW\*(C`iso8601()\*(C'\fR method is used.
.PP
A formatter can be handy when you know that in your application you
want to stringify your DateTime objects into a special format all the
time, for example to a different language.
.Sh "strftime Specifiers"
.IX Subsection "strftime Specifiers"
The following specifiers are allowed in the format string given to the
\&\f(CW\*(C`strftime()\*(C'\fR method:
.ie n .IP "* %a" 4
.el .IP "* \f(CW%a\fR" 4
.IX Item "%a"
The abbreviated weekday name.
.ie n .IP "* %A" 4
.el .IP "* \f(CW%A\fR" 4
.IX Item "%A"
The full weekday name.
.ie n .IP "* %b" 4
.el .IP "* \f(CW%b\fR" 4
.IX Item "%b"
The abbreviated month name.
.ie n .IP "* %B" 4
.el .IP "* \f(CW%B\fR" 4
.IX Item "%B"
The full month name.
.ie n .IP "* %c" 4
.el .IP "* \f(CW%c\fR" 4
.IX Item "%c"
The default datetime format for the object's locale.
.ie n .IP "* %C" 4
.el .IP "* \f(CW%C\fR" 4
.IX Item "%C"
The century number (year/100) as a 2\-digit integer.
.ie n .IP "* %d" 4
.el .IP "* \f(CW%d\fR" 4
.IX Item "%d"
The day of the month as a decimal number (range 01 to 31).
.ie n .IP "* %D" 4
.el .IP "* \f(CW%D\fR" 4
.IX Item "%D"
Equivalent to \f(CW%m\fR/%d/%y. This is not a good standard format if you
want folks from both the United States and the rest of the world to
understand the date!
.ie n .IP "* %e" 4
.el .IP "* \f(CW%e\fR" 4
.IX Item "%e"
Like \f(CW%d\fR, the day of the month as a decimal number, but a leading zero
is replaced by a space.
.ie n .IP "* %F" 4
.el .IP "* \f(CW%F\fR" 4
.IX Item "%F"
Equivalent to \f(CW%Y\fR\-%m\-%d (the \s-1ISO\s0 8601 date format)
.ie n .IP "* %G" 4
.el .IP "* \f(CW%G\fR" 4
.IX Item "%G"
The \s-1ISO\s0 8601 year with century as a decimal number. The 4\-digit year
corresponding to the \s-1ISO\s0 week number (see \f(CW%V\fR). This has the same
format and value as \f(CW%Y\fR, except that if the \s-1ISO\s0 week number belongs to
the previous or next year, that year is used instead. (\s-1TZ\s0)
.ie n .IP "* %g" 4
.el .IP "* \f(CW%g\fR" 4
.IX Item "%g"
Like \f(CW%G\fR, but without century, i.e., with a 2\-digit year (00\-99).
.ie n .IP "* %h" 4
.el .IP "* \f(CW%h\fR" 4
.IX Item "%h"
Equivalent to \f(CW%b\fR.
.ie n .IP "* %H" 4
.el .IP "* \f(CW%H\fR" 4
.IX Item "%H"
The hour as a decimal number using a 24\-hour clock (range 00 to 23).
.ie n .IP "* %I" 4
.el .IP "* \f(CW%I\fR" 4
.IX Item "%I"
The hour as a decimal number using a 12\-hour clock (range 01 to 12).
.ie n .IP "* %j" 4
.el .IP "* \f(CW%j\fR" 4
.IX Item "%j"
The day of the year as a decimal number (range 001 to 366).
.ie n .IP "* %k" 4
.el .IP "* \f(CW%k\fR" 4
.IX Item "%k"
The hour (24\-hour clock) as a decimal number (range 0 to 23); single
digits are preceded by a blank. (See also \f(CW%H\fR.)
.ie n .IP "* %l" 4
.el .IP "* \f(CW%l\fR" 4
.IX Item "%l"
The hour (12\-hour clock) as a decimal number (range 1 to 12); single
digits are preceded by a blank. (See also \f(CW%I\fR.)
.ie n .IP "* %m" 4
.el .IP "* \f(CW%m\fR" 4
.IX Item "%m"
The month as a decimal number (range 01 to 12).
.ie n .IP "* %M" 4
.el .IP "* \f(CW%M\fR" 4
.IX Item "%M"
The minute as a decimal number (range 00 to 59).
.ie n .IP "* %n" 4
.el .IP "* \f(CW%n\fR" 4
.IX Item "%n"
A newline character.
.ie n .IP "* %N" 4
.el .IP "* \f(CW%N\fR" 4
.IX Item "%N"
The fractional seconds digits. Default is 9 digits (nanoseconds).
.Sp
.Vb 3
\& %3N milliseconds (3 digits)
\& %6N microseconds (6 digits)
\& %9N nanoseconds (9 digits)
.Ve
.ie n .IP "* %p" 4
.el .IP "* \f(CW%p\fR" 4
.IX Item "%p"
Either `\s-1AM\s0' or `\s-1PM\s0' according to the given time value, or the
corresponding strings for the current locale. Noon is treated as `pm'
and midnight as `am'.
.ie n .IP "* %P" 4
.el .IP "* \f(CW%P\fR" 4
.IX Item "%P"
Like \f(CW%p\fR but in lowercase: `am' or `pm' or a corresponding string for
the current locale.
.ie n .IP "* %r" 4
.el .IP "* \f(CW%r\fR" 4
.IX Item "%r"
The time in a.m. or p.m. notation. In the \s-1POSIX\s0 locale this is
equivalent to `%I:%M:%S \f(CW%p\fR'.
.ie n .IP "* %R" 4
.el .IP "* \f(CW%R\fR" 4
.IX Item "%R"
The time in 24\-hour notation (%H:%M). (\s-1SU\s0) For a version including the
seconds, see \f(CW%T\fR below.
.ie n .IP "* %s" 4
.el .IP "* \f(CW%s\fR" 4
.IX Item "%s"
The number of seconds since the epoch.
.ie n .IP "* %S" 4
.el .IP "* \f(CW%S\fR" 4
.IX Item "%S"
The second as a decimal number (range 00 to 61).
.ie n .IP "* %t" 4
.el .IP "* \f(CW%t\fR" 4
.IX Item "%t"
A tab character.
.ie n .IP "* %T" 4
.el .IP "* \f(CW%T\fR" 4
.IX Item "%T"
The time in 24\-hour notation (%H:%M:%S).
.ie n .IP "* %u" 4
.el .IP "* \f(CW%u\fR" 4
.IX Item "%u"
The day of the week as a decimal, range 1 to 7, Monday being 1. See
also \f(CW%w\fR.
.ie n .IP "* %U" 4
.el .IP "* \f(CW%U\fR" 4
.IX Item "%U"
The week number of the current year as a decimal number, range 00 to
53, starting with the first Sunday as the first day of week 01. See
also \f(CW%V\fR and \f(CW%W\fR.
.ie n .IP "* %V" 4
.el .IP "* \f(CW%V\fR" 4
.IX Item "%V"
The \s-1ISO\s0 8601:1988 week number of the current year as a decimal number,
range 01 to 53, where week 1 is the first week that has at least 4
days in the current year, and with Monday as the first day of the
week. See also \f(CW%U\fR and \f(CW%W\fR.
.ie n .IP "* %w" 4
.el .IP "* \f(CW%w\fR" 4
.IX Item "%w"
The day of the week as a decimal, range 0 to 6, Sunday being 0. See
also \f(CW%u\fR.
.ie n .IP "* %W" 4
.el .IP "* \f(CW%W\fR" 4
.IX Item "%W"
The week number of the current year as a decimal number, range 00 to
53, starting with the first Monday as the first day of week 01.
.ie n .IP "* %x" 4
.el .IP "* \f(CW%x\fR" 4
.IX Item "%x"
The default date format for the object's locale.
.ie n .IP "* %X" 4
.el .IP "* \f(CW%X\fR" 4
.IX Item "%X"
The default time format for the object's locale.
.ie n .IP "* %y" 4
.el .IP "* \f(CW%y\fR" 4
.IX Item "%y"
The year as a decimal number without a century (range 00 to 99).
.ie n .IP "* %Y" 4
.el .IP "* \f(CW%Y\fR" 4
.IX Item "%Y"
The year as a decimal number including the century.
.ie n .IP "* %z" 4
.el .IP "* \f(CW%z\fR" 4
.IX Item "%z"
The time-zone as hour offset from \s-1UTC\s0. Required to emit
RFC822\-conformant dates (using \*(L"%a, \f(CW%d\fR \f(CW%b\fR \f(CW%Y\fR \f(CW%H:\fR%M:%S \f(CW%z\fR\*(R").
.ie n .IP "* %Z" 4
.el .IP "* \f(CW%Z\fR" 4
.IX Item "%Z"
The time zone or name or abbreviation.
.IP "* %%" 4
A literal `%' character.
.IP "* %{method}" 4
.IX Item "%{method}"
Any method name may be specified using the format \f(CW\*(C`%{method}\*(C'\fR name
where \*(L"method\*(R" is a valid \f(CW\*(C`DateTime.pm\*(C'\fR object method.
.SH "DateTime.pm and Storable"
.IX Header "DateTime.pm and Storable"
As of version 0.13, DateTime implements Storable hooks in order to
reduce the size of a serialized DateTime object.
.SH "KNOWN BUGS"
.IX Header "KNOWN BUGS"
The tests in \fI20infinite.t\fR seem to fail on some machines,
particularly on Win32. This appears to be related to Perl's internal
handling of \s-1IEEE\s0 infinity and NaN, and seems to be highly
platform/compiler/phase of moon dependent.
.PP
If you don't plan to use infinite datetimes you can probably ignore
this. This will be fixed (somehow) in future versions.
.SH "SUPPORT"
.IX Header "SUPPORT"
Support for this module is provided via the datetime@perl.org email
list. See http://datetime.perl.org/?MailingList for details.
.PP
Please submit bugs to the \s-1CPAN\s0 \s-1RT\s0 system at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=datetime or via email
at bug\-datetime@rt.cpan.org.
.SH "AUTHOR"
.IX Header "AUTHOR"
Dave Rolsky
.PP
However, please see the \s-1CREDITS\s0 file for more details on who I really
stole all the code from.
.SH "COPYRIGHT"
.IX Header "COPYRIGHT"
Copyright (c) 2003\-2006 David Rolsky. All rights reserved. This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
.PP
Portions of the code in this distribution are derived from other
works. Please see the \s-1CREDITS\s0 file for more details.
.PP
The full text of the license can be found in the \s-1LICENSE\s0 file included
with this module.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
datetime@perl.org mailing list
.PP
http://datetime.perl.org/