Upcoming SlideShare
×

# Date time

487 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
487
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Date time

1. 1. Dates and Times The Silent KillersSaturday, August 11, 12
2. 2. Dates and Times Are Easy!Saturday, August 11, 12
3. 3. Dates and Times Are Easy! • 60 seconds in a minute, 60 minutes in an hour.Saturday, August 11, 12
4. 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. 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
6. 6. WRONGSaturday, August 11, 12
7. 7. A reality checkSaturday, August 11, 12
8. 8. A reality check • There are not always 60 seconds in a minute, sometimes more (leap seconds, joy!)Saturday, August 11, 12
9. 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 DSTSaturday, August 11, 12
10. 10. Pop Quiz!Saturday, August 11, 12
11. 11. Saturday, August 11, 12
12. 12. • How many days were there in February 1900?Saturday, August 11, 12
13. 13. • How many days were there in February 1900? • How about February 2000?Saturday, August 11, 12
14. 14. • How many days were there in February 1900? • How about February 2000? • Remember what you know about leap years.Saturday, August 11, 12
15. 15. Saturday, August 11, 12
16. 16. • There were... 28 days in February 1900Saturday, August 11, 12
17. 17. • There were... 28 days in February 1900 • ... and 29 in February 2000Saturday, August 11, 12
18. 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 areSaturday, August 11, 12
19. 19. Why? How does that affect me? How do I do it right?Saturday, August 11, 12
20. 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 inconsistentSaturday, August 11, 12
21. 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 mathSaturday, August 11, 12
22. 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) dont observeSaturday, August 11, 12
23. 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. 24. How do we handle this?Saturday, August 11, 12
25. 25. use DateTime;Saturday, August 11, 12
26. 26. use DateTime; • DateTime knows about all these edge cases and vagaries and handles them for you, intelligentlySaturday, August 11, 12
27. 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 existSaturday, August 11, 12
28. 28. A Word about TimezonesSaturday, August 11, 12
29. 29. A Word about Timezones • By default, DateTime objects use the “floating” timezoneSaturday, August 11, 12
30. 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 yearsSaturday, August 11, 12
31. 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 displaySaturday, August 11, 12
32. 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
33. 33. Getting Dat[ae] into DateTimeSaturday, August 11, 12
34. 34. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epochSaturday, August 11, 12
35. 35. Getting Dat[ae] into DateTime • Several constructors: new, now, from_epoch • The new constructor takes parameters like hour, minute, second, day, month, yearSaturday, August 11, 12
36. 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 argumentSaturday, August 11, 12
37. 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 argumentSaturday, August 11, 12
38. 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
39. 39. Manipulating DatesSaturday, August 11, 12
40. 40. Manipulating Dates • Arithmetic operations are overloadedSaturday, August 11, 12
41. 41. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta)Saturday, August 11, 12
42. 42. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta) • Adding two objects doesnt make sense; add a ::Duration to get a DateTime objectSaturday, August 11, 12
43. 43. Manipulating Dates • Arithmetic operations are overloaded • Subtracting two DateTime objects yields a DateTime::Duration (the delta) • Adding two objects doesnt 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. 44. my \$later = \$dt->clone->add(days => 2); my \$duration = \$later - \$dt; my \$later_also = \$dt + \$duration;Saturday, August 11, 12
45. 45. Getting Dat[ae] OutSaturday, August 11, 12
46. 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. 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. • Theres a strftime method for very flexible formattingSaturday, August 11, 12
48. 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. • Theres a strftime method for very flexible formatting • Other simpler methods, like ymd, as well as varied accessorsSaturday, August 11, 12
49. 49. my \$announcement = "It is now \$dt"; my \$proper_date = \$dt->strftime( "%Y-%m-%d" ); my \$identical = \$dt->ymd;Saturday, August 11, 12
50. 50. Questions?Saturday, August 11, 12