4. Like Git:
• Date and time is complicated
• At first, we avoid learning date and time
But also, like Git:
• Date and time makes sense
• Date and time can be fun
8. Coordinated UniversalTime (UTC)
• A perspective of the global timeline
• Allows us to keep track of absolute time
• Primary standard by which the world regulates clocks
• Defined precisely by scientific community
• Includes leap seconds
• Has no relationship to geography
• Basically the same as GMT, but GMT is not defined precisely by the scientific
community
9. LocalTime
• A local time is a perspective of time
• It does not represent a point on the global timeline
• It is usually not a contiguous timeline (DST)
10. UTC Time:
2016-04-09T14:17:47Z
What we know
• The point in absolute time
• Whether this time is before or after
another point in time
What we don’t know
• Whether it is morning or night
• What day of the week it is
11. LocalTime:
Saturday,April 9, 2016 9:11 AM
We Know
• It is Saturday
• It is morning
We Don’t Know
• What point this is on the global
timeline
• Whether it is before or after a time
from a different locality
• What the time will be if we add one
hour to this time
13. Time Zone Basics
• A time zone is a region that observes a uniform standard time
• A time zone is defined by a set of offsets from UTC
• Offsets are typically in one hour intervals, but not always
• Nepal StandardTime is UTC +5:45
• Time zones frequently have multiple offsets
• There are two main resources for time zones
• Internet Assigned Numbers Authority (IANA)
• WindowsTime Zones
• IANA time zones are more widely used, and more accurate
15. Politics
• Time zones are inherently political
• Governments change time zones regularly
• Governments change time zones suddenly
• Russia this year is an example of politicians causing time chaos
• http://bit.ly/1SB9TvW
• Morocco reverts to standard time during Ramadan
• Assume nothing
16. IANATime Zone Libraries
• JavaScript - MomentTimeZone
• http://momentjs.com/timezone/
• .NET – NodaTime
• http://nodatime.org/
• Java 8 + - java.time (native)
• Java 7 - JodaTime
• See Stack Overflow post for more exhaustive list
• http://bit.ly/1RUYuuM
17. Time Zones are not Offsets!
"2016-04-09T19:39:00-05:00“
This date could be in:
• America/Chicago
• America/Bahia_Banderas
• America/Bogata
• America/Cancun
• America/Cayman
• And more
21. Show me all the messages for the
business day of April 4th.
I have users in London, and across the United States.
In LondonApril 4th is between 2016-04-03 23:00:00Z and 2016-04-04 22:59:59Z
In MinneapolisApril 4th is between 2016-04-04 05:00:00Z and 2016-04-05 04:59:59Z
22. Perspectives
• When storing a date, consider the following perspectives
• Absolute time
• Time local to the date’s viewer
• Time local to the date’s originator
24. Why use ISO 8601 format?
• With offset, reflects both local and absolute perspective
• Has unambiguous ordering
• Compare this to 4/10/2016
• In the US this is April 10th
• In the UK this is October 4th
• Helps avoid having to compute local perspective from absolute
during querying
• Supported by nearly all modern databases
• Databases will automatically order in absolute time
25. Assumption
“If I just store everything in ISO 8601 with an
offset, everything will be fine.”
26. Future Dates
• Time zones change over time
• In the future, the offset of a scheduled time could change
• Store future dates in local time with a time zone
30. As Seen on Stack Overflow
var startHours = 8;
var startMinutes = 30;
var ed = new Date();
var endHours = ed.getHours();
var endMinutes = ed.getMinutes();
var elapsedMinutes = (endHours * 60 + endMinutes) - (startHours * 60 +
startMinutes);
console.log(elapsedMinutes);
33. Time Math vs Date Math
Time math:
• Refers to operations involving hours, minutes, seconds, milliseconds
• Works by incrementing or decrementing the position on the global timeline by the number of
units in question
• Can use fractional units
Date Math:
• Refers to all operations larger than hours – days, months, years, quarters, etc.
• Works by moving places on the calendar
• Cannot be converted to time math
• Cannot use fractional units
38. Samoa
• Samoa is very close to the international date line
• Samoa found itself trading more frequently with Australia than America
• Samoa switched sides of the international date line
• December 30 2011 does not exist in Samoan time
40. var a = new Date('2016-01-01');
a.toString();
"Thu Dec 31 2015 18:00:00 GMT-0600 (Central StandardTime)"
var a = new Date('1/1/2016');
a.toString();
"Fri Jan 01 2016 00:00:00 GMT-0600 (Central StandardTime)"
41. Known JavaScript Date Issues
• DSTTransitions can go both directions
• Months index from zero
• No support for time zones other than user’s local time zone and UTC
• Older browsers know only current DST rules
• Parsing is implementation specific and basically completely broken
• The spec is a disaster
http://codeofmatt.com/2013/06/07/javascript-date-type-is-
horribly-broken/
43. Parsing
• Consistently parses date values according to your expectations
• More than 100 languages supported
• Thousands of options for parse-able formats
• Parse multiple formats if necessary