3. Dates and Times Are Easy!
• 60 seconds in a minute, 60 minutes in an hour.
Saturday, August 11, 12
4. Dates and Times Are Easy!
• 60 seconds in a minute, 60 minutes in an hour.
• 24 hours in a day, 365 days in a year, unless the year
is a multiple of 4.
Saturday, August 11, 12
5. Dates and Times Are Easy!
• 60 seconds in a minute, 60 minutes in an hour.
• 24 hours in a day, 365 days in a year, unless the year
is a multiple of 4.
• Simple, right?
Saturday, August 11, 12
8. A reality check
• There are not always 60 seconds in a minute,
sometimes more (leap seconds, joy!)
Saturday, August 11, 12
9. A reality check
• There are not always 60 seconds in a minute,
sometimes more (leap seconds, joy!)
• Not always 24 hours in a day: sometimes more,
sometimes less—consider DST
Saturday, August 11, 12
16. • There were... 28 days in February 1900
Saturday, August 11, 12
17. • There were... 28 days in February 1900
• ... and 29 in February 2000
Saturday, August 11, 12
18. • There were... 28 days in February 1900
• ... and 29 in February 2000
• Why? Years that are divisible by 100 are not leap
years, whereas years divisible by 400 are
Saturday, August 11, 12
19. Why? How does that affect me? How do
I do it right?
Saturday, August 11, 12
20. Why? How does that affect me? How do
I do it right?
• Variances in Earth’s rotation and axial tilt cause our
rotation and revolution to be inconsistent
Saturday, August 11, 12
21. Why? How does that affect me? How do
I do it right?
• Variances in Earth’s rotation and axial tilt cause our
rotation and revolution to be inconsistent
• It means that there’s a lot of particulars and edge
cases for doing date/time math
Saturday, August 11, 12
22. Why? How does that affect me? How do
I do it right?
• Variances in Earth’s rotation and axial tilt cause our
rotation and revolution to be inconsistent
• It means that there’s a lot of particulars and edge
cases for doing date/time math
• Currently, NYC observes DST. But the government
could change that. Certain parts of the US (Indiana,
Arizona) don't observe
Saturday, August 11, 12
23. If that wasn’t enough...
• Consider what you’d do if you were working for
Indiana Jones and he wanted you to convert dates
between the Mayan calendar and the Gregorian
calendar. Or, more likely, the Julian calendar (run cal
9 1752 and prepare to be surprised) and the
Gregorian calendar.
Saturday, August 11, 12
24. How do we handle this?
Saturday, August 11, 12
26. use DateTime;
• DateTime knows about all these edge cases and
vagaries and handles them for you, intelligently
Saturday, August 11, 12
27. use DateTime;
• DateTime knows about all these edge cases and
vagaries and handles them for you, intelligently
• It throws exceptions for invalid data, so you can’t
construct something that shouldn’t exist
Saturday, August 11, 12
29. A Word about Timezones
• By default, DateTime objects use the “floating”
timezone
Saturday, August 11, 12
30. A Word about Timezones
• By default, DateTime objects use the “floating”
timezone
• This means that it doesn’t take into account any local
timezone issues like leap years
Saturday, August 11, 12
31. A Word about Timezones
• By default, DateTime objects use the “floating”
timezone
• This means that it doesn’t take into account any local
timezone issues like leap years
• It’s best to specify the timezone when creating a DT
object, convert to UTC for working with time/dates,
and then convert to the user’s local timezone for
display
Saturday, August 11, 12
32. my $dt = DateTime->now(time_zone =>
'America/New_York');
$dt->set_time_zone('UTC');
$dt->add(months => 3) # leap year!
# the next day...
$dt->set_time_zone('Asia/Taipei');
Saturday, August 11, 12
34. Getting Dat[ae] into DateTime
• Several constructors: new, now, from_epoch
Saturday, August 11, 12
35. Getting Dat[ae] into DateTime
• Several constructors: new, now, from_epoch
• The new constructor takes parameters like hour,
minute, second, day, month, year
Saturday, August 11, 12
36. Getting Dat[ae] into DateTime
• Several constructors: new, now, from_epoch
• The new constructor takes parameters like hour,
minute, second, day, month, year
• from_epoch takes a named epoch argument
Saturday, August 11, 12
37. Getting Dat[ae] into DateTime
• Several constructors: new, now, from_epoch
• The new constructor takes parameters like hour,
minute, second, day, month, year
• from_epoch takes a named epoch argument
• All constructors take a time_zone named
argument
Saturday, August 11, 12
38. my $dt = DateTime->now(
time_zone => 'EST5EDT'
);
my $dt = DateTime->from_epoch(
epoch => 1234567890,
time_zone => 'America/New_York',
);
my $dt = DateTime->new(
minute => 24,
hour => 20,
year => 2012,
month => 3,
day => 1
);
Saturday, August 11, 12
40. Manipulating Dates
• Arithmetic operations are overloaded
Saturday, August 11, 12
41. Manipulating Dates
• Arithmetic operations are overloaded
• Subtracting two DateTime objects yields a
DateTime::Duration (the delta)
Saturday, August 11, 12
42. Manipulating Dates
• Arithmetic operations are overloaded
• Subtracting two DateTime objects yields a
DateTime::Duration (the delta)
• Adding two objects doesn't make sense; add
a ::Duration to get a DateTime object
Saturday, August 11, 12
43. Manipulating Dates
• Arithmetic operations are overloaded
• Subtracting two DateTime objects yields a
DateTime::Duration (the delta)
• Adding two objects doesn't make sense; add
a ::Duration to get a DateTime object
• See also add, subtract methods (take same args as
new)
Saturday, August 11, 12
44. my $later = $dt->clone->add(days => 2);
my $duration = $later - $dt;
my $later_also = $dt + $duration;
Saturday, August 11, 12
46. Getting Dat[ae] Out
• DateTime overloads the " operator; this produces an
ISO8601-formatted string. There are lots of other
formatters on CPAN, including one for MySQL types.
Saturday, August 11, 12
47. Getting Dat[ae] Out
• DateTime overloads the " operator; this produces an
ISO8601-formatted string. There are lots of other
formatters on CPAN, including one for MySQL types.
• There's a strftime method for very flexible
formatting
Saturday, August 11, 12
48. Getting Dat[ae] Out
• DateTime overloads the " operator; this produces an
ISO8601-formatted string. There are lots of other
formatters on CPAN, including one for MySQL types.
• There's a strftime method for very flexible
formatting
• Other simpler methods, like ymd, as well as varied
accessors
Saturday, August 11, 12
49. my $announcement = "It is now $dt";
my $proper_date = $dt->strftime(
"%Y-%m-%d"
);
my $identical = $dt->ymd;
Saturday, August 11, 12