1DateTime(3) User Contributed Perl Documentation DateTime(3)
2
3
4
6 DateTime - A date and time object
7
9 version 1.04
10
12 use DateTime;
13
14 $dt = DateTime->new(
15 year => 1964,
16 month => 10,
17 day => 16,
18 hour => 16,
19 minute => 12,
20 second => 47,
21 nanosecond => 500000000,
22 time_zone => 'Asia/Taipei',
23 );
24
25 $dt = DateTime->from_epoch( epoch => $epoch );
26 $dt = DateTime->now; # same as ( epoch => time() )
27
28 $year = $dt->year;
29 $month = $dt->month; # 1-12
30
31 $day = $dt->day; # 1-31
32
33 $dow = $dt->day_of_week; # 1-7 (Monday is 1)
34
35 $hour = $dt->hour; # 0-23
36 $minute = $dt->minute; # 0-59
37
38 $second = $dt->second; # 0-61 (leap seconds!)
39
40 $doy = $dt->day_of_year; # 1-366 (leap years)
41
42 $doq = $dt->day_of_quarter; # 1..
43
44 $qtr = $dt->quarter; # 1-4
45
46 # all of the start-at-1 methods above have corresponding start-at-0
47 # methods, such as $dt->day_of_month_0, $dt->month_0 and so on
48
49 $ymd = $dt->ymd; # 2002-12-06
50 $ymd = $dt->ymd('/'); # 2002/12/06
51
52 $mdy = $dt->mdy; # 12-06-2002
53 $mdy = $dt->mdy('/'); # 12/06/2002
54
55 $dmy = $dt->dmy; # 06-12-2002
56 $dmy = $dt->dmy('/'); # 06/12/2002
57
58 $hms = $dt->hms; # 14:02:29
59 $hms = $dt->hms('!'); # 14!02!29
60
61 $is_leap = $dt->is_leap_year;
62
63 # these are localizable, see Locales section
64 $month_name = $dt->month_name; # January, February, ...
65 $month_abbr = $dt->month_abbr; # Jan, Feb, ...
66 $day_name = $dt->day_name; # Monday, Tuesday, ...
67 $day_abbr = $dt->day_abbr; # Mon, Tue, ...
68
69 # May not work for all possible datetime, see the docs on this
70 # method for more details.
71 $epoch_time = $dt->epoch;
72
73 $dt2 = $dt + $duration_object;
74
75 $dt3 = $dt - $duration_object;
76
77 $duration_object = $dt - $dt2;
78
79 $dt->set( year => 1882 );
80
81 $dt->set_time_zone( 'America/Chicago' );
82
83 $dt->set_formatter( $formatter );
84
86 DateTime is a class for the representation of date/time combinations,
87 and is part of the Perl DateTime project. For details on this project
88 please see <http://datetime.perl.org/>. The DateTime site has a FAQ
89 which may help answer many "how do I do X?" questions. The FAQ is at
90 <http://datetime.perl.org/wiki/datetime/page/FAQ>.
91
92 It represents the Gregorian calendar, extended backwards in time before
93 its creation (in 1582). This is sometimes known as the "proleptic
94 Gregorian calendar". In this calendar, the first day of the calendar
95 (the epoch), is the first day of year 1, which corresponds to the date
96 which was (incorrectly) believed to be the birth of Jesus Christ.
97
98 The calendar represented does have a year 0, and in that way differs
99 from how dates are often written using "BCE/CE" or "BC/AD".
100
101 For infinite datetimes, please see the DateTime::Infinite module.
102
104 0-based Versus 1-based Numbers
105 The DateTime.pm module follows a simple consistent logic for
106 determining whether or not a given number is 0-based or 1-based.
107
108 Month, day of month, day of week, and day of year are 1-based. Any
109 method that is 1-based also has an equivalent 0-based method ending in
110 "_0". So for example, this class provides both "day_of_week()" and
111 "day_of_week_0()" methods.
112
113 The "day_of_week_0()" method still treats Monday as the first day of
114 the week.
115
116 All time-related numbers such as hour, minute, and second are 0-based.
117
118 Years are neither, as they can be both positive or negative, unlike any
119 other datetime component. There is a year 0.
120
121 There is no "quarter_0()" method.
122
123 Error Handling
124 Some errors may cause this module to die with an error string. This can
125 only happen when calling constructor methods, methods that change the
126 object, such as "set()", or methods that take parameters. Methods that
127 retrieve information about the object, such as "year()" or "epoch()",
128 will never die.
129
130 Locales
131 All the object methods which return names or abbreviations return data
132 based on a locale. This is done by setting the locale when constructing
133 a DateTime object. There is also a "DefaultLocale()" class method which
134 may be used to set the default locale for all DateTime objects created.
135 If this is not set, then "en_US" is used.
136
137 Floating DateTimes
138 The default time zone for new DateTime objects, except where stated
139 otherwise, is the "floating" time zone. This concept comes from the
140 iCal standard. A floating datetime is one which is not anchored to any
141 particular time zone. In addition, floating datetimes do not include
142 leap seconds, since we cannot apply them without knowing the datetime's
143 time zone.
144
145 The results of date math and comparison between a floating datetime and
146 one with a real time zone are not really valid, because one includes
147 leap seconds and the other does not. Similarly, the results of datetime
148 math between two floating datetimes and two datetimes with time zones
149 are not really comparable.
150
151 If you are planning to use any objects with a real time zone, it is
152 strongly recommended that you do not mix these with floating datetimes.
153
154 Math
155 If you are going to be using doing date math, please read the section
156 "How DateTime Math Works".
157
158 Time Zone Warnings
159 Determining the local time zone for a system can be slow. If $ENV{TZ}
160 is not set, it may involve reading a number of files in /etc or
161 elsewhere. If you know that the local time zone won't change while your
162 code is running, and you need to make many objects for the local time
163 zone, it is strongly recommended that you retrieve the local time zone
164 once and cache it:
165
166 our $App::LocalTZ = DateTime::TimeZone->new( name => 'local' );
167
168 ... # then everywhere else
169
170 my $dt = DateTime->new( ..., time_zone => $App::LocalTZ );
171
172 DateTime itself does not do this internally because local time zones
173 can change, and there's no good way to determine if it's changed
174 without doing all the work to look it up.
175
176 Do not try to use named time zones (like "America/Chicago") with dates
177 very far in the future (thousands of years). The current implementation
178 of "DateTime::TimeZone" will use a huge amount of memory calculating
179 all the DST changes from now until the future date. Use UTC or the
180 floating time zone and you will be safe.
181
183 DateTime provide many methods. The documentation breaks them down into
184 groups based on what they do (constructor, accessors, modifiers, etc.).
185
186 Constructors
187 All constructors can die when invalid parameters are given.
188
189 DateTime->new( ... )
190
191 This class method accepts parameters for each date and time component:
192 "year", "month", "day", "hour", "minute", "second", "nanosecond". It
193 also accepts "locale", "time_zone", and "formatter" parameters.
194
195 my $dt = DateTime->new(
196 year => 1966,
197 month => 10,
198 day => 25,
199 hour => 7,
200 minute => 15,
201 second => 47,
202 nanosecond => 500000000,
203 time_zone => 'America/Chicago',
204 );
205
206 DateTime validates the "month", "day", "hour", "minute", and "second",
207 and "nanosecond" parameters. The valid values for these parameters are:
208
209 · month
210
211 An integer from 1-12.
212
213 · day
214
215 An integer from 1-31, and it must be within the valid range of
216 days for the specified month.
217
218 · hour
219
220 An integer from 0-23.
221
222 · minute
223
224 An integer from 0-59.
225
226 · second
227
228 An integer from 0-61 (to allow for leap seconds). Values of 60
229 or 61 are only allowed when they match actual leap seconds.
230
231 · nanosecond
232
233 An integer >= 0. If this number is greater than 1 billion, it
234 will be normalized into the second value for the DateTime
235 object.
236
237 Invalid parameter types (like an array reference) will cause the
238 constructor to die.
239
240 The value for seconds may be from 0 to 61, to account for leap seconds.
241 If you give a value greater than 59, DateTime does check to see that it
242 really matches a valid leap second.
243
244 All of the parameters are optional except for "year". The "month" and
245 "day" parameters both default to 1, while the "hour", "minute",
246 "second", and "nanosecond" parameters all default to 0.
247
248 The "locale" parameter should be a string matching one of the valid
249 locales, or a "DateTime::Locale" object. See the DateTime::Locale
250 documentation for details.
251
252 The time_zone parameter can be either a scalar or a
253 "DateTime::TimeZone" object. A string will simply be passed to the
254 "DateTime::TimeZone->new" method as its "name" parameter. This string
255 may be an Olson DB time zone name ("America/Chicago"), an offset string
256 ("+0630"), or the words "floating" or "local". See the
257 "DateTime::TimeZone" documentation for more details.
258
259 The default time zone is "floating".
260
261 The "formatter" can be either a scalar or an object, but the class
262 specified by the scalar or the object must implement a
263 "format_datetime()" method.
264
265 Parsing Dates
266
267 This module does not parse dates! That means there is no constructor to
268 which you can pass things like "March 3, 1970 12:34".
269
270 Instead, take a look at the various "DateTime::Format::*" modules on
271 CPAN. These parse all sorts of different date formats, and you're bound
272 to find something that can handle your particular needs.
273
274 Ambiguous Local Times
275
276 Because of Daylight Saving Time, it is possible to specify a local time
277 that is ambiguous. For example, in the US in 2003, the transition from
278 to saving to standard time occurred on October 26, at 02:00:00 local
279 time. The local clock changed from 01:59:59 (saving time) to 01:00:00
280 (standard time). This means that the hour from 01:00:00 through
281 01:59:59 actually occurs twice, though the UTC time continues to move
282 forward.
283
284 If you specify an ambiguous time, then the latest UTC time is always
285 used, in effect always choosing standard time. In this case, you can
286 simply subtract an hour to the object in order to move to saving time,
287 for example:
288
289 # This object represent 01:30:00 standard time
290 my $dt = DateTime->new(
291 year => 2003,
292 month => 10,
293 day => 26,
294 hour => 1,
295 minute => 30,
296 second => 0,
297 time_zone => 'America/Chicago',
298 );
299
300 print $dt->hms; # prints 01:30:00
301
302 # Now the object represent 01:30:00 saving time
303 $dt->subtract( hours => 1 );
304
305 print $dt->hms; # still prints 01:30:00
306
307 Alternately, you could create the object with the UTC time zone, and
308 then call the "set_time_zone()" method to change the time zone. This is
309 a good way to ensure that the time is not ambiguous.
310
311 Invalid Local Times
312
313 Another problem introduced by Daylight Saving Time is that certain
314 local times just do not exist. For example, in the US in 2003, the
315 transition from standard to saving time occurred on April 6, at the
316 change to 2:00:00 local time. The local clock changes from 01:59:59
317 (standard time) to 03:00:00 (saving time). This means that there is no
318 02:00:00 through 02:59:59 on April 6!
319
320 Attempting to create an invalid time currently causes a fatal error.
321 This may change in future version of this module.
322
323 DateTime->from_epoch( epoch => $epoch, ... )
324
325 This class method can be used to construct a new DateTime object from
326 an epoch time instead of components. Just as with the "new()" method,
327 it accepts "time_zone", "locale", and "formatter" parameters.
328
329 If the epoch value is not an integer, the part after the decimal will
330 be converted to nanoseconds. This is done in order to be compatible
331 with "Time::HiRes". If the floating portion extends past 9 decimal
332 places, it will be truncated to nine, so that 1.1234567891 will become
333 1 second and 123,456,789 nanoseconds.
334
335 By default, the returned object will be in the UTC time zone.
336
337 DateTime->now( ... )
338
339 This class method is equivalent to calling "from_epoch()" with the
340 value returned from Perl's "time()" function. Just as with the "new()"
341 method, it accepts "time_zone" and "locale" parameters.
342
343 By default, the returned object will be in the UTC time zone.
344
345 DateTime->today( ... )
346
347 This class method is equivalent to:
348
349 DateTime->now(@_)->truncate( to => 'day' );
350
351 DateTime->from_object( object => $object, ... )
352
353 This class method can be used to construct a new DateTime object from
354 any object that implements the "utc_rd_values()" method. All
355 "DateTime::Calendar" modules must implement this method in order to
356 provide cross-calendar compatibility. This method accepts a "locale"
357 and "formatter" parameter
358
359 If the object passed to this method has a "time_zone()" method, that is
360 used to set the time zone of the newly created "DateTime.pm" object.
361
362 Otherwise, the returned object will be in the floating time zone.
363
364 DateTime->last_day_of_month( ... )
365
366 This constructor takes the same arguments as can be given to the
367 "new()" method, except for "day". Additionally, both "year" and "month"
368 are required.
369
370 DateTime->from_day_of_year( ... )
371
372 This constructor takes the same arguments as can be given to the
373 "new()" method, except that it does not accept a "month" or "day"
374 argument. Instead, it requires both "year" and "day_of_year". The day
375 of year must be between 1 and 366, and 366 is only allowed for leap
376 years.
377
378 $dt->clone()
379
380 This object method returns a new object that is replica of the object
381 upon which the method is called.
382
383 "Get" Methods
384 This class has many methods for retrieving information about an object.
385
386 $dt->year()
387
388 Returns the year.
389
390 $dt->ce_year()
391
392 Returns the year according to the BCE/CE numbering system. The year
393 before year 1 in this system is year -1, aka "1 BCE".
394
395 $dt->era_name()
396
397 Returns the long name of the current era, something like "Before
398 Christ". See the Locales section for more details.
399
400 $dt->era_abbr()
401
402 Returns the abbreviated name of the current era, something like "BC".
403 See the Locales section for more details.
404
405 $dt->christian_era()
406
407 Returns a string, either "BC" or "AD", according to the year.
408
409 $dt->secular_era()
410
411 Returns a string, either "BCE" or "CE", according to the year.
412
413 $dt->year_with_era()
414
415 Returns a string containing the year immediately followed by its era
416 abbreviation. The year is the absolute value of "ce_year()", so that
417 year 1 is "1AD" and year 0 is "1BC".
418
419 $dt->year_with_christian_era()
420
421 Like "year_with_era()", but uses the christian_era() method to get the
422 era name.
423
424 $dt->year_with_secular_era()
425
426 Like "year_with_era()", but uses the secular_era() method to get the
427 era name.
428
429 $dt->month()
430
431 Returns the month of the year, from 1..12.
432
433 Also available as "$dt->mon()".
434
435 $dt->month_name()
436
437 Returns the name of the current month. See the Locales section for more
438 details.
439
440 $dt->month_abbr()
441
442 Returns the abbreviated name of the current month. See the Locales
443 section for more details.
444
445 $dt->day()
446
447 Returns the day of the month, from 1..31.
448
449 Also available as "$dt->mday()" and "$dt->day_of_month()".
450
451 $dt->day_of_week()
452
453 Returns the day of the week as a number, from 1..7, with 1 being Monday
454 and 7 being Sunday.
455
456 Also available as "$dt->wday()" and "$dt->dow()".
457
458 $dt->local_day_of_week()
459
460 Returns the day of the week as a number, from 1..7. The day
461 corresponding to 1 will vary based on the locale.
462
463 $dt->day_name()
464
465 Returns the name of the current day of the week. See the Locales
466 section for more details.
467
468 $dt->day_abbr()
469
470 Returns the abbreviated name of the current day of the week. See the
471 Locales section for more details.
472
473 $dt->day_of_year()
474
475 Returns the day of the year.
476
477 Also available as "$dt->doy()".
478
479 $dt->quarter()
480
481 Returns the quarter of the year, from 1..4.
482
483 $dt->quarter_name()
484
485 Returns the name of the current quarter. See the Locales section for
486 more details.
487
488 $dt->quarter_abbr()
489
490 Returns the abbreviated name of the current quarter. See the Locales
491 section for more details.
492
493 $dt->day_of_quarter()
494
495 Returns the day of the quarter.
496
497 Also available as "$dt->doq()".
498
499 $dt->weekday_of_month()
500
501 Returns a number from 1..5 indicating which week day of the month this
502 is. For example, June 9, 2003 is the second Monday of the month, and so
503 this method returns 2 for that day.
504
505 $dt->ymd( $optional_separator ), $dt->mdy(...), $dt->dmy(...)
506
507 Each method returns the year, month, and day, in the order indicated by
508 the method name. Years are zero-padded to four digits. Months and days
509 are 0-padded to two digits.
510
511 By default, the values are separated by a dash (-), but this can be
512 overridden by passing a value to the method.
513
514 The "$dt->ymd()" method is also available as "$dt->date()".
515
516 $dt->hour()
517
518 Returns the hour of the day, from 0..23.
519
520 $dt->hour_1()
521
522 Returns the hour of the day, from 1..24.
523
524 $dt->hour_12()
525
526 Returns the hour of the day, from 1..12.
527
528 $dt->hour_12_0()
529
530 Returns the hour of the day, from 0..11.
531
532 $dt->am_or_pm()
533
534 Returns the appropriate localized abbreviation, depending on the
535 current hour.
536
537 $dt->minute()
538
539 Returns the minute of the hour, from 0..59.
540
541 Also available as "$dt->min()".
542
543 $dt->second()
544
545 Returns the second, from 0..61. The values 60 and 61 are used for leap
546 seconds.
547
548 Also available as "$dt->sec()".
549
550 $dt->fractional_second()
551
552 Returns the second, as a real number from 0.0 until 61.999999999
553
554 The values 60 and 61 are used for leap seconds.
555
556 $dt->millisecond()
557
558 Returns the fractional part of the second as milliseconds (1E-3
559 seconds).
560
561 Half a second is 500 milliseconds.
562
563 This value will always be rounded down to the nearest integer.
564
565 $dt->microsecond()
566
567 Returns the fractional part of the second as microseconds (1E-6
568 seconds).
569
570 Half a second is 500_000 microseconds.
571
572 This value will always be rounded down to the nearest integer.
573
574 $dt->nanosecond()
575
576 Returns the fractional part of the second as nanoseconds (1E-9
577 seconds).
578
579 Half a second is 500_000_000 nanoseconds.
580
581 $dt->hms( $optional_separator )
582
583 Returns the hour, minute, and second, all zero-padded to two digits.
584 If no separator is specified, a colon (:) is used by default.
585
586 Also available as "$dt->time()".
587
588 $dt->datetime()
589
590 This method is equivalent to:
591
592 $dt->ymd('-') . 'T' . $dt->hms(':')
593
594 Also available as "$dt->iso8601()".
595
596 $dt->is_leap_year()
597
598 This method returns a true or false indicating whether or not the
599 datetime object is in a leap year.
600
601 $dt->week()
602
603 ($week_year, $week_number) = $dt->week;
604
605 Returns information about the calendar week which contains this
606 datetime object. The values returned by this method are also available
607 separately through the week_year and week_number methods.
608
609 The first week of the year is defined by ISO as the one which contains
610 the fourth day of January, which is equivalent to saying that it's the
611 first week to overlap the new year by at least four days.
612
613 Typically the week year will be the same as the year that the object is
614 in, but dates at the very beginning of a calendar year often end up in
615 the last week of the prior year, and similarly, the final few days of
616 the year may be placed in the first week of the next year.
617
618 $dt->week_year()
619
620 Returns the year of the week. See "$dt->week()" for details.
621
622 $dt->week_number()
623
624 Returns the week of the year, from 1..53. See "$dt->week()" for
625 details.
626
627 $dt->week_of_month()
628
629 The week of the month, from 0..5. The first week of the month is the
630 first week that contains a Thursday. This is based on the ICU
631 definition of week of month, and correlates to the ISO8601 week of year
632 definition. A day in the week before the week with the first Thursday
633 will be week 0.
634
635 $dt->jd(), $dt->mjd()
636
637 These return the Julian Day and Modified Julian Day, respectively. The
638 value returned is a floating point number. The fractional portion of
639 the number represents the time portion of the datetime.
640
641 $dt->time_zone()
642
643 This returns the "DateTime::TimeZone" object for the datetime object.
644
645 $dt->offset()
646
647 This returns the offset from UTC, in seconds, of the datetime object
648 according to the time zone.
649
650 $dt->is_dst()
651
652 Returns a boolean indicating whether or not the datetime object is
653 currently in Daylight Saving Time or not.
654
655 $dt->time_zone_long_name()
656
657 This is a shortcut for "$dt->time_zone->name". It's provided so that
658 one can use "%{time_zone_long_name}" as a strftime format specifier.
659
660 $dt->time_zone_short_name()
661
662 This method returns the time zone abbreviation for the current time
663 zone, such as "PST" or "GMT". These names are not definitive, and
664 should not be used in any application intended for general use by users
665 around the world.
666
667 $dt->strftime( $format, ... )
668
669 This method implements functionality similar to the "strftime()" method
670 in C. However, if given multiple format strings, then it will return
671 multiple scalars, one for each format string.
672
673 See the "strftime Patterns" section for a list of all possible strftime
674 patterns.
675
676 If you give a pattern that doesn't exist, then it is simply treated as
677 text.
678
679 $dt->format_cldr( $format, ... )
680
681 This method implements formatting based on the CLDR date patterns. If
682 given multiple format strings, then it will return multiple scalars,
683 one for each format string.
684
685 See the "CLDR Patterns" section for a list of all possible CLDR
686 patterns.
687
688 If you give a pattern that doesn't exist, then it is simply treated as
689 text.
690
691 $dt->epoch()
692
693 Return the UTC epoch value for the datetime object. Internally, this is
694 implemented using "Time::Local", which uses the Unix epoch even on
695 machines with a different epoch (such as MacOS). Datetimes before the
696 start of the epoch will be returned as a negative number.
697
698 The return value from this method is always an integer.
699
700 Since the epoch does not account for leap seconds, the epoch time for
701 1972-12-31T23:59:60 (UTC) is exactly the same as that for
702 1973-01-01T00:00:00.
703
704 This module uses "Time::Local" to calculate the epoch, which may or may
705 not handle epochs before 1904 or after 2038 (depending on the size of
706 your system's integers, and whether or not Perl was compiled with
707 64-bit int support).
708
709 $dt->hires_epoch()
710
711 Returns the epoch as a floating point number. The floating point
712 portion of the value represents the nanosecond value of the object.
713 This method is provided for compatibility with the "Time::HiRes"
714 module.
715
716 $dt->is_finite(), $dt->is_infinite()
717
718 These methods allow you to distinguish normal datetime objects from
719 infinite ones. Infinite datetime objects are documented in
720 DateTime::Infinite.
721
722 $dt->utc_rd_values()
723
724 Returns the current UTC Rata Die days, seconds, and nanoseconds as a
725 three element list. This exists primarily to allow other calendar
726 modules to create objects based on the values provided by this object.
727
728 $dt->local_rd_values()
729
730 Returns the current local Rata Die days, seconds, and nanoseconds as a
731 three element list. This exists for the benefit of other modules which
732 might want to use this information for date math, such as
733 "DateTime::Event::Recurrence".
734
735 $dt->leap_seconds()
736
737 Returns the number of leap seconds that have happened up to the
738 datetime represented by the object. For floating datetimes, this always
739 returns 0.
740
741 $dt->utc_rd_as_seconds()
742
743 Returns the current UTC Rata Die days and seconds purely as seconds.
744 This number ignores any fractional seconds stored in the object, as
745 well as leap seconds.
746
747 $dt->locale()
748
749 Returns the current locale object.
750
751 $dt->formatter()
752
753 Returns current formatter object or class. See "Formatters And
754 Stringification" for details.
755
756 "Set" Methods
757 The remaining methods provided by "DateTime.pm", except where otherwise
758 specified, return the object itself, thus making method chaining
759 possible. For example:
760
761 my $dt = DateTime->now->set_time_zone( 'Australia/Sydney' );
762
763 my $first = DateTime
764 ->last_day_of_month( year => 2003, month => 3 )
765 ->add( days => 1 )
766 ->subtract( seconds => 1 );
767
768 $dt->set( .. )
769
770 This method can be used to change the local components of a date time,
771 or its locale. This method accepts any parameter allowed by the "new()"
772 method except for "time_zone". Time zones may be set using the
773 "set_time_zone()" method.
774
775 This method performs parameters validation just as is done in the
776 "new()" method.
777
778 Do not use this method to do date math. Use the "add()" and
779 "subtract()" methods instead.
780
781 $dt->set_year(), $dt->set_month(), etc.
782
783 DateTime has a "set_*" method for every item that can be passed to the
784 constructor:
785
786 · $dt->set_year()
787
788 · $dt->set_month()
789
790 · $dt->set_day()
791
792 · $dt->set_hour()
793
794 · $dt->set_minute()
795
796 · $dt->set_second()
797
798 · $dt->set_nanosecond()
799
800 · $dt->set_locale()
801
802 These are shortcuts to calling "set()" with a single key. They all take
803 a single parameter.
804
805 $dt->truncate( to => ... )
806
807 This method allows you to reset some of the local time components in
808 the object to their "zero" values. The "to" parameter is used to
809 specify which values to truncate, and it may be one of "year", "month",
810 "week", "day", "hour", "minute", or "second". For example, if "month"
811 is specified, then the local day becomes 1, and the hour, minute, and
812 second all become 0.
813
814 If "week" is given, then the datetime is set to the beginning of the
815 week in which it occurs, and the time components are all set to 0.
816
817 $dt->set_time_zone( $tz )
818
819 This method accepts either a time zone object or a string that can be
820 passed as the "name" parameter to "DateTime::TimeZone->new()". If the
821 new time zone's offset is different from the old time zone, then the
822 local time is adjusted accordingly.
823
824 For example:
825
826 my $dt = DateTime->new(
827 year => 2000,
828 month => 5,
829 day => 10,
830 hour => 15,
831 minute => 15,
832 time_zone => 'America/Los_Angeles',
833 );
834
835 print $dt->hour; # prints 15
836
837 $dt->set_time_zone( 'America/Chicago' );
838
839 print $dt->hour; # prints 17
840
841 If the old time zone was a floating time zone, then no adjustments to
842 the local time are made, except to account for leap seconds. If the new
843 time zone is floating, then the UTC time is adjusted in order to leave
844 the local time untouched.
845
846 Fans of Tsai Ming-Liang's films will be happy to know that this does
847 work:
848
849 my $dt = DateTime->now( time_zone => 'Asia/Taipei' );
850
851 $dt->set_time_zone( 'Europe/Paris' );
852
853 Yes, now we can know "ni3 na4 bian1 ji2dian3?"
854
855 $dt->set_formatter( $formatter )
856
857 Set the formatter for the object. See "Formatters And Stringification"
858 for details.
859
860 You can set this to "undef" to revert to the default formatter.
861
862 Math Methods
863 Like the set methods, math related methods always return the object
864 itself, to allow for chaining:
865
866 $dt->add( days => 1 )->subtract( seconds => 1 );
867
868 $dt->duration_class()
869
870 This returns "DateTime::Duration", but exists so that a subclass of
871 "DateTime.pm" can provide a different value.
872
873 $dt->add_duration( $duration_object )
874
875 This method adds a "DateTime::Duration" to the current datetime. See
876 the DateTime::Duration docs for more details.
877
878 $dt->add( DateTime::Duration->new parameters )
879
880 This method is syntactic sugar around the "add_duration()" method. It
881 simply creates a new "DateTime::Duration" object using the parameters
882 given, and then calls the "add_duration()" method.
883
884 $dt->subtract_duration( $duration_object )
885
886 When given a "DateTime::Duration" object, this method simply calls
887 "invert()" on that object and passes that new duration to the
888 "add_duration" method.
889
890 $dt->subtract( DateTime::Duration->new parameters )
891
892 Like "add()", this is syntactic sugar for the "subtract_duration()"
893 method.
894
895 $dt->subtract_datetime( $datetime )
896
897 This method returns a new "DateTime::Duration" object representing the
898 difference between the two dates. The duration is relative to the
899 object from which $datetime is subtracted. For example:
900
901 2003-03-15 00:00:00.00000000
902 - 2003-02-15 00:00:00.00000000
903 -------------------------------
904 = 1 month
905
906 Note that this duration is not an absolute measure of the amount of
907 time between the two datetimes, because the length of a month varies,
908 as well as due to the presence of leap seconds.
909
910 The returned duration may have deltas for months, days, minutes,
911 seconds, and nanoseconds.
912
913 $dt->delta_md( $datetime )
914
915 $dt->delta_days( $datetime )
916
917 Each of these methods returns a new "DateTime::Duration" object
918 representing some portion of the difference between two datetimes. The
919 "delta_md()" method returns a duration which contains only the month
920 and day portions of the duration is represented. The "delta_days()"
921 method returns a duration which contains only days.
922
923 The "delta_md" and "delta_days" methods truncate the duration so that
924 any fractional portion of a day is ignored. Both of these methods
925 operate on the date portion of a datetime only, and so effectively
926 ignore the time zone.
927
928 Unlike the subtraction methods, these methods always return a positive
929 (or zero) duration.
930
931 $dt->delta_ms( $datetime )
932
933 Returns a duration which contains only minutes and seconds. Any day and
934 month differences to minutes are converted to minutes and seconds. This
935 method also always return a positive (or zero) duration.
936
937 $dt->subtract_datetime_absolute( $datetime )
938
939 This method returns a new "DateTime::Duration" object representing the
940 difference between the two dates in seconds and nanoseconds. This is
941 the only way to accurately measure the absolute amount of time between
942 two datetimes, since units larger than a second do not represent a
943 fixed number of seconds.
944
945 Class Methods
946 DateTime->DefaultLocale( $locale )
947
948 This can be used to specify the default locale to be used when creating
949 DateTime objects. If unset, then "en_US" is used.
950
951 DateTime->compare( $dt1, $dt2 ), DateTime->compare_ignore_floating(
952 $dt1, $dt2 )
953
954 $cmp = DateTime->compare( $dt1, $dt2 );
955
956 $cmp = DateTime->compare_ignore_floating( $dt1, $dt2 );
957
958 Compare two DateTime objects. The semantics are compatible with Perl's
959 "sort()" function; it returns -1 if $dt1 < $dt2, 0 if $dt1 == $dt2, 1
960 if $dt1 > $dt2.
961
962 If one of the two DateTime objects has a floating time zone, it will
963 first be converted to the time zone of the other object. This is what
964 you want most of the time, but it can lead to inconsistent results when
965 you compare a number of DateTime objects, some of which are floating,
966 and some of which are in other time zones.
967
968 If you want to have consistent results (because you want to sort a
969 number of objects, for example), you can use the
970 "compare_ignore_floating()" method:
971
972 @dates = sort { DateTime->compare_ignore_floating($a, $b) } @dates;
973
974 In this case, objects with a floating time zone will be sorted as if
975 they were UTC times.
976
977 Since DateTime objects overload comparison operators, this:
978
979 @dates = sort @dates;
980
981 is equivalent to this:
982
983 @dates = sort { DateTime->compare($a, $b) } @dates;
984
985 DateTime objects can be compared to any other calendar class that
986 implements the "utc_rd_values()" method.
987
988 How DateTime Math Works
989 It's important to have some understanding of how datetime math is
990 implemented in order to effectively use this module and
991 "DateTime::Duration".
992
993 Making Things Simple
994
995 If you want to simplify your life and not have to think too hard about
996 the nitty-gritty of datetime math, I have several recommendations:
997
998 · use the floating time zone
999
1000 If you do not care about time zones or leap seconds, use the
1001 "floating" timezone:
1002
1003 my $dt = DateTime->now( time_zone => 'floating' );
1004
1005 Math done on two objects in the floating time zone produces very
1006 predictable results.
1007
1008 Note that in most cases you will want to start by creating an
1009 object in a specific zone and then convert it to the floating time
1010 zone. When an object goes from a real zone to the floating zone,
1011 the time for the object remains the same.
1012
1013 This means that passing the floating zone to a constructor may not
1014 do what you want.
1015
1016 my $dt = DateTime->now( time_zone => 'floating' );
1017
1018 is equivalent to
1019
1020 my $dt = DateTime->now( time_zone => 'UTC' )->set_time_zone('floating');
1021
1022 This might not be what you wanted. Instead, you may prefer to do
1023 this:
1024
1025 my $dt = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
1026
1027 · use UTC for all calculations
1028
1029 If you do care about time zones (particularly DST) or leap seconds,
1030 try to use non-UTC time zones for presentation and user input only.
1031 Convert to UTC immediately and convert back to the local time zone
1032 for presentation:
1033
1034 my $dt = DateTime->new( %user_input, time_zone => $user_tz );
1035 $dt->set_time_zone('UTC');
1036
1037 # do various operations - store it, retrieve it, add, subtract, etc.
1038
1039 $dt->set_time_zone($user_tz);
1040 print $dt->datetime;
1041
1042 · math on non-UTC time zones
1043
1044 If you need to do date math on objects with non-UTC time zones,
1045 please read the caveats below carefully. The results "DateTime.pm"
1046 produces are predictable and correct, and mostly intuitive, but
1047 datetime math gets very ugly when time zones are involved, and
1048 there are a few strange corner cases involving subtraction of two
1049 datetimes across a DST change.
1050
1051 If you can always use the floating or UTC time zones, you can skip
1052 ahead to Leap Seconds and Date Math
1053
1054 · date vs datetime math
1055
1056 If you only care about the date (calendar) portion of a datetime,
1057 you should use either "delta_md()" or "delta_days()", not
1058 "subtract_datetime()". This will give predictable, unsurprising
1059 results, free from DST-related complications.
1060
1061 · subtract_datetime() and add_duration()
1062
1063 You must convert your datetime objects to the UTC time zone before
1064 doing date math if you want to make sure that the following
1065 formulas are always true:
1066
1067 $dt2 - $dt1 = $dur
1068 $dt1 + $dur = $dt2
1069 $dt2 - $dur = $dt1
1070
1071 Note that using "delta_days" ensures that this formula always
1072 works, regardless of the timezone of the objects involved, as does
1073 using "subtract_datetime_absolute()". Other methods of subtraction
1074 are not always reversible.
1075
1076 Adding a Duration to a Datetime
1077
1078 The parts of a duration can be broken down into five parts. These are
1079 months, days, minutes, seconds, and nanoseconds. Adding one month to a
1080 date is different than adding 4 weeks or 28, 29, 30, or 31 days.
1081 Similarly, due to DST and leap seconds, adding a day can be different
1082 than adding 86,400 seconds, and adding a minute is not exactly the same
1083 as 60 seconds.
1084
1085 We cannot convert between these units, except for seconds and
1086 nanoseconds, because there is no fixed conversion between the two
1087 units, because of things like leap seconds, DST changes, etc.
1088
1089 "DateTime.pm" always adds (or subtracts) days, then months, minutes,
1090 and then seconds and nanoseconds. If there are any boundary overflows,
1091 these are normalized at each step. For the days and months the local
1092 (not UTC) values are used. For minutes and seconds, the local values
1093 are used. This generally just works.
1094
1095 This means that adding one month and one day to February 28, 2003 will
1096 produce the date April 1, 2003, not March 29, 2003.
1097
1098 my $dt = DateTime->new( year => 2003, month => 2, day => 28 );
1099
1100 $dt->add( months => 1, days => 1 );
1101
1102 # 2003-04-01 - the result
1103
1104 On the other hand, if we add months first, and then separately add
1105 days, we end up with March 29, 2003:
1106
1107 $dt->add( months => 1 )->add( days => 1 );
1108
1109 # 2003-03-29
1110
1111 We see similar strangeness when math crosses a DST boundary:
1112
1113 my $dt = DateTime->new(
1114 year => 2003,
1115 month => 4,
1116 day => 5,
1117 hour => 1,
1118 minute => 58,
1119 time_zone => "America/Chicago",
1120 );
1121
1122 $dt->add( days => 1, minutes => 3 );
1123 # 2003-04-06 02:01:00
1124
1125 $dt->add( minutes => 3 )->add( days => 1 );
1126 # 2003-04-06 03:01:00
1127
1128 Note that if you converted the datetime object to UTC first you would
1129 get predictable results.
1130
1131 If you want to know how many seconds a duration object represents, you
1132 have to add it to a datetime to find out, so you could do:
1133
1134 my $now = DateTime->now( time_zone => 'UTC' );
1135 my $later = $now->clone->add_duration($duration);
1136
1137 my $seconds_dur = $later->subtract_datetime_absolute($now);
1138
1139 This returns a duration which only contains seconds and nanoseconds.
1140
1141 If we were add the duration to a different datetime object we might get
1142 a different number of seconds.
1143
1144 DateTime::Duration supports three different end-of-month algorithms for
1145 adding months. This comes into play when an addition results in a day
1146 past the end of the month (for example, adding one month to January
1147 30).
1148
1149 # 2010-08-31 + 1 month = 2010-10-01
1150 $dt->add( months => 1, end_of_month => 'wrap' );
1151
1152 # 2010-01-30 + 1 month = 2010-02-28
1153 $dt->add( months => 1, end_of_month => 'limit' );
1154
1155 # 2010-04-30 + 1 month = 2010-05-31
1156 $dt->add( months => 1, end_of_month => 'preserve' );
1157
1158 By default, it uses "wrap" for positive durations and "preserve" for
1159 negative durations. See DateTime::Duration for a detailed explanation
1160 of these algorithms.
1161
1162 If you need to do lots of work with durations, take a look at Rick
1163 Measham's "DateTime::Format::Duration" module, which lets you present
1164 information from durations in many useful ways.
1165
1166 There are other subtract/delta methods in DateTime.pm to generate
1167 different types of durations. These methods are "subtract_datetime()",
1168 "subtract_datetime_absolute()", "delta_md()", "delta_days()", and
1169 "delta_ms()".
1170
1171 Datetime Subtraction
1172
1173 Date subtraction is done solely based on the two object's local
1174 datetimes, with one exception to handle DST changes. Also, if the two
1175 datetime objects are in different time zones, one of them is converted
1176 to the other's time zone first before subtraction. This is best
1177 explained through examples:
1178
1179 The first of these probably makes the most sense:
1180
1181 my $dt1 = DateTime->new(
1182 year => 2003,
1183 month => 5,
1184 day => 6,
1185 time_zone => 'America/Chicago',
1186 );
1187
1188 # not DST
1189
1190 my $dt2 = DateTime->new(
1191 year => 2003,
1192 month => 11,
1193 day => 6,
1194 time_zone => 'America/Chicago',
1195 );
1196
1197 # is DST
1198
1199 my $dur = $dt2->subtract_datetime($dt1);
1200 # 6 months
1201
1202 Nice and simple.
1203
1204 This one is a little trickier, but still fairly logical:
1205
1206 my $dt1 = DateTime->new(
1207 year => 2003,
1208 month => 4,
1209 day => 5,
1210 hour => 1,
1211 minute => 58,
1212 time_zone => "America/Chicago",
1213 );
1214
1215 # is DST
1216
1217 my $dt2 = DateTime->new(
1218 year => 2003,
1219 month => 4,
1220 day => 7,
1221 hour => 2,
1222 minute => 1,
1223 time_zone => "America/Chicago",
1224 );
1225
1226 # not DST
1227
1228 my $dur = $dt2->subtract_datetime($dt1);
1229
1230 # 2 days and 3 minutes
1231
1232 Which contradicts the result this one gives, even though they both make
1233 sense:
1234
1235 my $dt1 = DateTime->new(
1236 year => 2003,
1237 month => 4,
1238 day => 5,
1239 hour => 1,
1240 minute => 58,
1241 time_zone => "America/Chicago",
1242 );
1243
1244 # is DST
1245
1246 my $dt2 = DateTime->new(
1247 year => 2003,
1248 month => 4,
1249 day => 6,
1250 hour => 3,
1251 minute => 1,
1252 time_zone => "America/Chicago",
1253 );
1254
1255 # not DST
1256
1257 my $dur = $dt2->subtract_datetime($dt1);
1258
1259 # 1 day and 3 minutes
1260
1261 This last example illustrates the "DST" exception mentioned earlier.
1262 The exception accounts for the fact 2003-04-06 only lasts 23 hours.
1263
1264 And finally:
1265
1266 my $dt2 = DateTime->new(
1267 year => 2003,
1268 month => 10,
1269 day => 26,
1270 hour => 1,
1271 time_zone => 'America/Chicago',
1272 );
1273
1274 my $dt1 = $dt2->clone->subtract( hours => 1 );
1275
1276 my $dur = $dt2->subtract_datetime($dt1);
1277 # 60 minutes
1278
1279 This seems obvious until you realize that subtracting 60 minutes from
1280 $dt2 in the above example still leaves the clock time at "01:00:00".
1281 This time we are accounting for a 25 hour day.
1282
1283 Reversibility
1284
1285 Date math operations are not always reversible. This is because of the
1286 way that addition operations are ordered. As was discussed earlier,
1287 adding 1 day and 3 minutes in one call to "add()" is not the same as
1288 first adding 3 minutes and 1 day in two separate calls.
1289
1290 If we take a duration returned from "subtract_datetime()" and then try
1291 to add or subtract that duration from one of the datetimes we just
1292 used, we sometimes get interesting results:
1293
1294 my $dt1 = DateTime->new(
1295 year => 2003,
1296 month => 4,
1297 day => 5,
1298 hour => 1,
1299 minute => 58,
1300 time_zone => "America/Chicago",
1301 );
1302
1303 my $dt2 = DateTime->new(
1304 year => 2003,
1305 month => 4,
1306 day => 6,
1307 hour => 3,
1308 minute => 1,
1309 time_zone => "America/Chicago",
1310 );
1311
1312 my $dur = $dt2->subtract_datetime($dt1);
1313 # 1 day and 3 minutes
1314
1315 $dt1->add_duration($dur);
1316 # gives us $dt2
1317
1318 $dt2->subtract_duration($dur);
1319 # gives us 2003-04-05 02:58:00 - 1 hour later than $dt1
1320
1321 The "subtract_duration()" operation gives us a (perhaps) unexpected
1322 answer because it first subtracts one day to get 2003-04-05T03:01:00
1323 and then subtracts 3 minutes to get the final result.
1324
1325 If we explicitly reverse the order we can get the original value of
1326 $dt1. This can be facilitated by "DateTime::Duration"'s
1327 "calendar_duration()" and "clock_duration()" methods:
1328
1329 $dt2->subtract_duration( $dur->clock_duration )
1330 ->subtract_duration( $dur->calendar_duration );
1331
1332 Leap Seconds and Date Math
1333
1334 The presence of leap seconds can cause even more anomalies in date
1335 math. For example, the following is a legal datetime:
1336
1337 my $dt = DateTime->new(
1338 year => 1972,
1339 month => 12,
1340 day => 31,
1341 hour => 23,
1342 minute => 59,
1343 second => 60,
1344 time_zone => 'UTC'
1345 );
1346
1347 If we do the following:
1348
1349 $dt->add( months => 1 );
1350
1351 Then the datetime is now "1973-02-01 00:00:00", because there is no
1352 23:59:60 on 1973-01-31.
1353
1354 Leap seconds also force us to distinguish between minutes and seconds
1355 during date math. Given the following datetime:
1356
1357 my $dt = DateTime->new(
1358 year => 1972,
1359 month => 12,
1360 day => 31,
1361 hour => 23,
1362 minute => 59,
1363 second => 30,
1364 time_zone => 'UTC'
1365 );
1366
1367 we will get different results when adding 1 minute than we get if we
1368 add 60 seconds. This is because in this case, the last minute of the
1369 day, beginning at 23:59:00, actually contains 61 seconds.
1370
1371 Here are the results we get:
1372
1373 # 1972-12-31 23:59:30 - our starting datetime
1374
1375 $dt->clone->add( minutes => 1 );
1376 # 1973-01-01 00:00:30 - one minute later
1377
1378 $dt->clone->add( seconds => 60 );
1379 # 1973-01-01 00:00:29 - 60 seconds later
1380
1381 $dt->clone->add( seconds => 61 );
1382 # 1973-01-01 00:00:30 - 61 seconds later
1383
1384 Local vs. UTC and 24 hours vs. 1 day
1385
1386 When math crosses a daylight saving boundary, a single day may have
1387 more or less than 24 hours.
1388
1389 For example, if you do this:
1390
1391 my $dt = DateTime->new(
1392 year => 2003,
1393 month => 4,
1394 day => 5,
1395 hour => 2,
1396 time_zone => 'America/Chicago',
1397 );
1398
1399 $dt->add( days => 1 );
1400
1401 then you will produce an invalid local time, and therefore an exception
1402 will be thrown.
1403
1404 However, this works:
1405
1406 my $dt = DateTime->new(
1407 year => 2003,
1408 month => 4,
1409 day => 5,
1410 hour => 2,
1411 time_zone => 'America/Chicago',
1412 );
1413
1414 $dt->add( hours => 24 );
1415
1416 and produces a datetime with the local time of "03:00".
1417
1418 If all this makes your head hurt, there is a simple alternative. Just
1419 convert your datetime object to the "UTC" time zone before doing date
1420 math on it, and switch it back to the local time zone afterwards. This
1421 avoids the possibility of having date math throw an exception, and
1422 makes sure that 1 day equals 24 hours. Of course, this may not always
1423 be desirable, so caveat user!
1424
1425 Overloading
1426 This module explicitly overloads the addition (+), subtraction (-),
1427 string and numeric comparison operators. This means that the following
1428 all do sensible things:
1429
1430 my $new_dt = $dt + $duration_obj;
1431
1432 my $new_dt = $dt - $duration_obj;
1433
1434 my $duration_obj = $dt - $new_dt;
1435
1436 foreach my $dt ( sort @dts ) { ... }
1437
1438 Additionally, the fallback parameter is set to true, so other derivable
1439 operators (+=, -=, etc.) will work properly. Do not expect increment
1440 (++) or decrement (--) to do anything useful.
1441
1442 The string comparison operators, "eq" or "ne", will use the string
1443 value to compare with non-DateTime objects.
1444
1445 DateTime objects do not have a numeric value, using "==" or "<=>" to
1446 compare a DateTime object with a non-DateTime object will result in an
1447 exception. To safely sort mixed DateTime and non-DateTime objects, use
1448 "sort { $a cmp $b } @dates".
1449
1450 The module also overloads stringification using the object's formatter,
1451 defaulting to "iso8601()" method. See "Formatters And Stringification"
1452 for details.
1453
1454 Formatters And Stringification
1455 You can optionally specify a "formatter", which is usually a
1456 DateTime::Format::* object/class, to control the stringification of the
1457 DateTime object.
1458
1459 Any of the constructor methods can accept a formatter argument:
1460
1461 my $formatter = DateTime::Format::Strptime->new(...);
1462 my $dt = DateTime->new(year => 2004, formatter => $formatter);
1463
1464 Or, you can set it afterwards:
1465
1466 $dt->set_formatter($formatter);
1467 $formatter = $dt->formatter();
1468
1469 Once you set the formatter, the overloaded stringification method will
1470 use the formatter. If unspecified, the "iso8601()" method is used.
1471
1472 A formatter can be handy when you know that in your application you
1473 want to stringify your DateTime objects into a special format all the
1474 time, for example to a different language.
1475
1476 If you provide a formatter class name or object, it must implement a
1477 "format_datetime" method. This method will be called with just the
1478 DateTime object as its argument.
1479
1480 CLDR Patterns
1481 The CLDR pattern language is both more powerful and more complex than
1482 strftime. Unlike strftime patterns, you often have to explicitly escape
1483 text that you do not want formatted, as the patterns are simply letters
1484 without any prefix.
1485
1486 For example, "yyyy-MM-dd" is a valid CLDR pattern. If you want to
1487 include any lower or upper case ASCII characters as-is, you can
1488 surround them with single quotes ('). If you want to include a single
1489 quote, you must escape it as two single quotes ('').
1490
1491 'Today is ' EEEE
1492 'It is now' h 'o''clock' a
1493
1494 Spaces and any non-letter text will always be passed through as-is.
1495
1496 Many CLDR patterns which produce numbers will pad the number with
1497 leading zeroes depending on the length of the format specifier. For
1498 example, "h" represents the current hour from 1-12. If you specify "hh"
1499 then the 1-9 will have a leading zero prepended.
1500
1501 However, CLDR often uses five of a letter to represent the narrow form
1502 of a pattern. This inconsistency is necessary for backwards
1503 compatibility.
1504
1505 CLDR often distinguishes between the "format" and "stand-alone" forms
1506 of a pattern. The format pattern is used when the thing in question is
1507 being placed into a larger string. The stand-alone form is used when
1508 displaying that item by itself, for example in a calendar.
1509
1510 It also often provides three sizes for each item, wide (the full name),
1511 abbreviated, and narrow. The narrow form is often just a single
1512 character, for example "T" for "Tuesday", and may not be unique.
1513
1514 CLDR provides a fairly complex system for localizing time zones that we
1515 ignore entirely. The time zone patterns just use the information
1516 provided by "DateTime::TimeZone", and do not follow the CLDR spec.
1517
1518 The output of a CLDR pattern is always localized, when applicable.
1519
1520 CLDR provides the following patterns:
1521
1522 · G{1,3}
1523
1524 The abbreviated era (BC, AD).
1525
1526 · GGGG
1527
1528 The wide era (Before Christ, Anno Domini).
1529
1530 · GGGGG
1531
1532 The narrow era, if it exists (and it mostly doesn't).
1533
1534 · y and y{3,}
1535
1536 The year, zero-prefixed as needed. Negative years will start with a
1537 "-", and this will be included in the length calculation.
1538
1539 In other, words the "yyyyy" pattern will format year -1234 as
1540 "-1234", not "-01234".
1541
1542 · yy
1543
1544 This is a special case. It always produces a two-digit year, so
1545 "1976" becomes "76". Negative years will start with a "-", making
1546 them one character longer.
1547
1548 · Y{1,}
1549
1550 The week of the year, from "$dt->week_year()".
1551
1552 · u{1,}
1553
1554 Same as "y" except that "uu" is not a special case.
1555
1556 · Q{1,2}
1557
1558 The quarter as a number (1..4).
1559
1560 · QQQ
1561
1562 The abbreviated format form for the quarter.
1563
1564 · QQQQ
1565
1566 The wide format form for the quarter.
1567
1568 · q{1,2}
1569
1570 The quarter as a number (1..4).
1571
1572 · qqq
1573
1574 The abbreviated stand-alone form for the quarter.
1575
1576 · qqqq
1577
1578 The wide stand-alone form for the quarter.
1579
1580 · M{1,2]
1581
1582 The numerical month.
1583
1584 · MMM
1585
1586 The abbreviated format form for the month.
1587
1588 · MMMM
1589
1590 The wide format form for the month.
1591
1592 · MMMMM
1593
1594 The narrow format form for the month.
1595
1596 · L{1,2]
1597
1598 The numerical month.
1599
1600 · LLL
1601
1602 The abbreviated stand-alone form for the month.
1603
1604 · LLLL
1605
1606 The wide stand-alone form for the month.
1607
1608 · LLLLL
1609
1610 The narrow stand-alone form for the month.
1611
1612 · w{1,2}
1613
1614 The week of the year, from "$dt->week_number()".
1615
1616 · W
1617
1618 The week of the month, from "$dt->week_of_month()".
1619
1620 · d{1,2}
1621
1622 The numeric day of the month.
1623
1624 · D{1,3}
1625
1626 The numeric day of the year.
1627
1628 · F
1629
1630 The day of the week in the month, from "$dt->weekday_of_month()".
1631
1632 · g{1,}
1633
1634 The modified Julian day, from "$dt->mjd()".
1635
1636 · E{1,3} and eee
1637
1638 The abbreviated format form for the day of the week.
1639
1640 · EEEE and eeee
1641
1642 The wide format form for the day of the week.
1643
1644 · EEEEE and eeeee
1645
1646 The narrow format form for the day of the week.
1647
1648 · e{1,2}
1649
1650 The local numeric day of the week, from 1 to 7. This number depends
1651 on what day is considered the first day of the week, which varies
1652 by locale. For example, in the US, Sunday is the first day of the
1653 week, so this returns 2 for Monday.
1654
1655 · c
1656
1657 The numeric day of the week from 1 to 7, treating Monday as the
1658 first of the week, regardless of locale.
1659
1660 · ccc
1661
1662 The abbreviated stand-alone form for the day of the week.
1663
1664 · cccc
1665
1666 The wide stand-alone form for the day of the week.
1667
1668 · ccccc
1669
1670 The narrow format form for the day of the week.
1671
1672 · a
1673
1674 The localized form of AM or PM for the time.
1675
1676 · h{1,2}
1677
1678 The hour from 1-12.
1679
1680 · H{1,2}
1681
1682 The hour from 0-23.
1683
1684 · K{1,2}
1685
1686 The hour from 0-11.
1687
1688 · k{1,2}
1689
1690 The hour from 1-24.
1691
1692 · j{1,2}
1693
1694 The hour, in 12 or 24 hour form, based on the preferred form for
1695 the locale. In other words, this is equivalent to either "h{1,2}"
1696 or "H{1,2}".
1697
1698 · m{1,2}
1699
1700 The minute.
1701
1702 · s{1,2}
1703
1704 The second.
1705
1706 · S{1,}
1707
1708 The fractional portion of the seconds, rounded based on the length
1709 of the specifier. This returned without a leading decimal point,
1710 but may have leading or trailing zeroes.
1711
1712 · A{1,}
1713
1714 The millisecond of the day, based on the current time. In other
1715 words, if it is 12:00:00.00, this returns 43200000.
1716
1717 · z{1,3}
1718
1719 The time zone short name.
1720
1721 · zzzz
1722
1723 The time zone long name.
1724
1725 · Z{1,3}
1726
1727 The time zone offset.
1728
1729 · ZZZZ
1730
1731 The time zone short name and the offset as one string, so something
1732 like "CDT-0500".
1733
1734 · v{1,3}
1735
1736 The time zone short name.
1737
1738 · vvvv
1739
1740 The time zone long name.
1741
1742 · V{1,3}
1743
1744 The time zone short name.
1745
1746 · VVVV
1747
1748 The time zone long name.
1749
1750 strftime Patterns
1751 The following patterns are allowed in the format string given to the
1752 "$dt->strftime()" method:
1753
1754 · %a
1755
1756 The abbreviated weekday name.
1757
1758 · %A
1759
1760 The full weekday name.
1761
1762 · %b
1763
1764 The abbreviated month name.
1765
1766 · %B
1767
1768 The full month name.
1769
1770 · %c
1771
1772 The default datetime format for the object's locale.
1773
1774 · %C
1775
1776 The century number (year/100) as a 2-digit integer.
1777
1778 · %d
1779
1780 The day of the month as a decimal number (range 01 to 31).
1781
1782 · %D
1783
1784 Equivalent to %m/%d/%y. This is not a good standard format if you
1785 want folks from both the United States and the rest of the world to
1786 understand the date!
1787
1788 · %e
1789
1790 Like %d, the day of the month as a decimal number, but a leading
1791 zero is replaced by a space.
1792
1793 · %F
1794
1795 Equivalent to %Y-%m-%d (the ISO 8601 date format)
1796
1797 · %G
1798
1799 The ISO 8601 year with century as a decimal number. The 4-digit
1800 year corresponding to the ISO week number (see %V). This has the
1801 same format and value as %Y, except that if the ISO week number
1802 belongs to the previous or next year, that year is used instead.
1803 (TZ)
1804
1805 · %g
1806
1807 Like %G, but without century, i.e., with a 2-digit year (00-99).
1808
1809 · %h
1810
1811 Equivalent to %b.
1812
1813 · %H
1814
1815 The hour as a decimal number using a 24-hour clock (range 00 to
1816 23).
1817
1818 · %I
1819
1820 The hour as a decimal number using a 12-hour clock (range 01 to
1821 12).
1822
1823 · %j
1824
1825 The day of the year as a decimal number (range 001 to 366).
1826
1827 · %k
1828
1829 The hour (24-hour clock) as a decimal number (range 0 to 23);
1830 single digits are preceded by a blank. (See also %H.)
1831
1832 · %l
1833
1834 The hour (12-hour clock) as a decimal number (range 1 to 12);
1835 single digits are preceded by a blank. (See also %I.)
1836
1837 · %m
1838
1839 The month as a decimal number (range 01 to 12).
1840
1841 · %M
1842
1843 The minute as a decimal number (range 00 to 59).
1844
1845 · %n
1846
1847 A newline character.
1848
1849 · %N
1850
1851 The fractional seconds digits. Default is 9 digits (nanoseconds).
1852
1853 %3N milliseconds (3 digits)
1854 %6N microseconds (6 digits)
1855 %9N nanoseconds (9 digits)
1856
1857 This value will always be rounded down to the nearest integer.
1858
1859 · %p
1860
1861 Either `AM' or `PM' according to the given time value, or the
1862 corresponding strings for the current locale. Noon is treated as
1863 `pm' and midnight as `am'.
1864
1865 · %P
1866
1867 Like %p but in lowercase: `am' or `pm' or a corresponding string
1868 for the current locale.
1869
1870 · %r
1871
1872 The time in a.m. or p.m. notation. In the POSIX locale this is
1873 equivalent to `%I:%M:%S %p'.
1874
1875 · %R
1876
1877 The time in 24-hour notation (%H:%M). (SU) For a version including
1878 the seconds, see %T below.
1879
1880 · %s
1881
1882 The number of seconds since the epoch.
1883
1884 · %S
1885
1886 The second as a decimal number (range 00 to 61).
1887
1888 · %t
1889
1890 A tab character.
1891
1892 · %T
1893
1894 The time in 24-hour notation (%H:%M:%S).
1895
1896 · %u
1897
1898 The day of the week as a decimal, range 1 to 7, Monday being 1. See
1899 also %w.
1900
1901 · %U
1902
1903 The week number of the current year as a decimal number, range 00
1904 to 53, starting with the first Sunday as the first day of week 01.
1905 See also %V and %W.
1906
1907 · %V
1908
1909 The ISO 8601:1988 week number of the current year as a decimal
1910 number, range 01 to 53, where week 1 is the first week that has at
1911 least 4 days in the current year, and with Monday as the first day
1912 of the week. See also %U and %W.
1913
1914 · %w
1915
1916 The day of the week as a decimal, range 0 to 6, Sunday being 0. See
1917 also %u.
1918
1919 · %W
1920
1921 The week number of the current year as a decimal number, range 00
1922 to 53, starting with the first Monday as the first day of week 01.
1923
1924 · %x
1925
1926 The default date format for the object's locale.
1927
1928 · %X
1929
1930 The default time format for the object's locale.
1931
1932 · %y
1933
1934 The year as a decimal number without a century (range 00 to 99).
1935
1936 · %Y
1937
1938 The year as a decimal number including the century.
1939
1940 · %z
1941
1942 The time-zone as hour offset from UTC. Required to emit
1943 RFC822-conformant dates (using "%a, %d %b %Y %H:%M:%S %z").
1944
1945 · %Z
1946
1947 The time zone or name or abbreviation.
1948
1949 · %%
1950
1951 A literal `%' character.
1952
1953 · %{method}
1954
1955 Any method name may be specified using the format "%{method}" name
1956 where "method" is a valid "DateTime.pm" object method.
1957
1958 DateTime.pm and Storable
1959 DateTime implements Storable hooks in order to reduce the size of a
1960 serialized DateTime object.
1961
1963 This module is part of a larger ecosystem of modules in the DateTime
1964 family.
1965
1966 DateTime::Set
1967 The DateTime::Set module represents sets (including recurrences) of
1968 datetimes. Many modules return sets or recurrences.
1969
1970 Format Modules
1971 The various format modules exist to parse and format datetimes. For
1972 example, DateTime::Format::HTTP parses dates according to the RFC 1123
1973 format:
1974
1975 my $datetime
1976 = DateTime::Format::HTTP->parse_datetime('Thu Feb 3 17:03:55 GMT 1994');
1977
1978 print DateTime::Format::HTTP->format_datetime($datetime);
1979
1980 Most format modules are suitable for use as a "formatter" with a
1981 DateTime object.
1982
1983 All format modules start with "DateTime::Format::".
1984
1985 Calendar Modules
1986 There are a number of modules on CPAN that implement non-Gregorian
1987 calendars, such as the Chinese, Mayan, and Julian calendars.
1988
1989 All calendar modules start with "DateTime::Calendar::".
1990
1991 Event Modules
1992 There are a number of modules that calculate the dates for events, such
1993 as Easter, Sunrise, etc.
1994
1995 All event modules start with "DateTime::Event::".
1996
1997 Others
1998 There are many other modules that work with DateTime, including modules
1999 in the "DateTimeX" namespace, as well as others.
2000
2001 See the datetime wiki <http://datetime.perl.org> and search.cpan.org
2002 <http://search.cpan.org/search?query=datetime&mode=dist> for more
2003 details.
2004
2006 The tests in 20infinite.t seem to fail on some machines, particularly
2007 on Win32. This appears to be related to Perl's internal handling of
2008 IEEE infinity and NaN, and seems to be highly platform/compiler/phase
2009 of moon dependent.
2010
2011 If you don't plan to use infinite datetimes you can probably ignore
2012 this. This will be fixed (perhaps) in future versions.
2013
2015 Support for this module is provided via the datetime@perl.org email
2016 list. See http://datetime.perl.org/wiki/datetime/page/Mailing_List for
2017 details.
2018
2019 Please submit bugs to the CPAN RT system at
2020 http://rt.cpan.org/NoAuth/Bugs.html?Dist=DateTime or via email at
2021 bug-datetime@rt.cpan.org.
2022
2024 If you'd like to thank me for the work I've done on this module, please
2025 consider making a "donation" to me via PayPal. I spend a lot of free
2026 time creating free software, and would appreciate any support you'd
2027 care to offer.
2028
2029 Please note that I am not suggesting that you must do this in order for
2030 me to continue working on this particular software. I will continue to
2031 do so, inasmuch as I have in the past, for as long as it interests me.
2032
2033 Similarly, a donation made in this way will probably not make me work
2034 on this software much more, unless I get so many donations that I can
2035 consider working on free software full time, which seems unlikely at
2036 best.
2037
2038 To donate, log into PayPal and send money to autarch@urth.org or use
2039 the button on this page:
2040 <http://www.urth.org/~autarch/fs-donation.html>
2041
2043 datetime@perl.org mailing list
2044
2045 http://datetime.perl.org/
2046
2048 Dave Rolsky <autarch@urth.org>
2049
2051 This software is Copyright (c) 2013 by Dave Rolsky.
2052
2053 This is free software, licensed under:
2054
2055 The Artistic License 2.0 (GPL Compatible)
2056
2057
2058
2059perl v5.16.3 2017-08-02 DateTime(3)