Upgrade to Pro — share decks privately, control downloads, hide ads and more …

It's About Time

It's About Time

Time is a quirky business, and I don't mean relativity! Let's look at ways to avoid some of its common pitfalls in software.

Avatar for Martin Richter

Martin Richter

January 08, 2016

Other Decks in Technology

Transcript

  1. Theory It should be noon when the sun is at

    its highest (solar noon). ! ➡ "
  2. Your app/service/thing must handle this. • Do you sort lists

    based on physical or civil time? • How do you display local times to the user?
  3. Daylight Saving Time (DST) • Introduced in the early 20th

    century to save coal • Became commonplace during the 70's energy crisis • Nowadays controversial due to health effects, hassle & $$
  4. Reasons to love DST • Some local times exist twice,

    others not at all • EU example: Oct 30, 2016 3:30am and Mar 20, 2016 3:30am • DST is not always an hour: Australia/Lord_Howe has 30 min • Countries and states merrily adopt & abolish DST all the time • Morocco uses DST, but suspends it during Ramadan • …
  5. Western Australia, the Northern Territory and Queensland stay on standard

    time, while the rest of the country goes on DST.
  6. Gregorian calendar • Introduced in 1582 to stop the drift

    of Easter • Replaced the Julian calendar (0.002% correction) • Considered a Catholic plot by Protestant & Orthodox churches, and adopted very late – if ever • Not exactly quirk-free either • Example: What does it mean to add 1 month to a date?
  7. Types of Time 1. Event time (e.g. a World Cup

    final) 2. Television time • e.g. a TV show screened at 9pm local time around the world • Note that movie premieres tend to use event time • Special case: Someone's birthday 3. Relative time (e.g. 10 days remaining until ticket sales begin) 4. Recurring time (e.g. wake me up every morning at 7am, no matter if DST or time zone changes)
  8. UTC ≠ GMT GMT (Greenwich Mean Time) is a time

    zone, whereas UTC is a standard for global time (! time at 0°). ! "Coordinated Universal Time" / " "Temps Universel Coordonné"
  9. ISO 8601 (continued) 2016-01-08T17:30:00+01:00 The date and 24-hour time, followed

    by a UTC offset. • This is the same time as 2016-01-08T16:30:00Z • Separators and least significant values can be skipped • No offset means local time (discouraged) • Pro: Human-friendly, sorts naturally • Con: Requires parsing
  10. ISO 8601 (continued) P3Y6M4DT12H30M5S A period (duration) of 3 years,

    6 months, 4 days, 12 hours, 30 minutes and 5 seconds.
  11. Unix timestamp 1452270600 The seconds elapsed since Jan 01, 1970

    UTC. • Pro: Good for machine-readable APIs (no parsing) • Con: Not very accessible to (regular) humans
  12. On January 28, 2011, our Futurice colleague Michael Samarin receives

    an invitation to talk at a conference in Moscow. His slot is on November 7, 2011 at 13:00 local time. Michael marks his digital calendar, setting the event's time zone to "Moscow". On the big day, he learns to his horror that he has missed his slot by an hour. What happened?
  13. Solution • The slot begins on November 07, 2011 at

    13:00:00 local time • Moscow does not use DST in November, so his calendar app calculates and stores the following: • UTC timestamp 2011-11-07T10:00:00Z • Time zone: "Europe/Moscow" • On February 8, 2011, Russia decides to stay on "permanent summer time" (UTC+4) after the next shift to DST on March 27. Global time zone databases are updated accordingly.
  14. Solution (continued) • His device eventually downloads the updated time

    zone database. From now on, it will interpret "Europe/Moscow" as UTC+4:00. • When Michael looks at his calendar in November, the device reconstructs the displayed time from the UTC timestamp and the time zone, showing November 7, 2011 14:00:00. • Folks in Moscow miss a great talk – thanks to a sloppy developer.
  15. Advice for developers • Use battle-tested calendar libraries. Don't solve

    this stuff yourself! • Never write code like tuesday = monday + 24*60*60 • Convert input to UTC as soon as possible • Save 3 things: UTC timestamp, UTC offset and time zone • Without UTC offset, you might break future times • Without time zone, you can't do calculations on the date • Convert back into local time when you display it
  16. Advice for designers • Don't stay on the happy path!

    • Don't make false assumptions about time • Consider the purpose (e.g. reminder to buy festival vs. flight tickets) • Relative time is easy, recurring stuff is hard • Make sure your users don't get lost ❤
  17. Links • http:/ /www.creativedeletion.com/2015/03/19/ persistingfuturedatetimes.html • http:/ /www.creativedeletion.com/2015/01/28/falsehoods- programmers-date-time-zones.html •

    http:/ /infiniteundo.com/post/25326999628/falsehoods- programmers-believe-about-time • http:/ /blog.poormansmath.net/the-time-it-takes-to-change-the- time/ • http:/ /futurice.com/blog/time-is-the-fire-in-which-we-burn