Download - Date and Time Odds Ends Oddities
![Page 1: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/1.jpg)
DATE AND TIMEOdds, Ends, and Oddities
![Page 2: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/2.jpg)
About Mewww.maggiepint.com
@maggiepint
https://www.tempworks.com
![Page 3: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/3.jpg)
https://xkcd.com/1597/
![Page 4: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/4.jpg)
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
![Page 5: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/5.jpg)
5 O'clock Somewhere
![Page 6: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/6.jpg)
PERSPECTIVEWe all see dates and times from different angles
![Page 7: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/7.jpg)
The Global Timeline
1 2 3 4 5 6 7 8 9
Point in absolute time
![Page 8: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/8.jpg)
Coordinated Universal Time (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
![Page 9: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/9.jpg)
Local Time• 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)
![Page 10: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/10.jpg)
UTC Time:2016-04-09T14:17:47ZWhat 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
![Page 11: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/11.jpg)
Local Time: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
![Page 12: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/12.jpg)
TIME ZONESUniting Perspectives
![Page 13: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/13.jpg)
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 Standard Time is UTC +5:45• Time zones frequently have multiple offsets• There are two main resources for time zones
• Internet Assigned Numbers Authority (IANA)• Windows Time Zones
• IANA time zones are more widely used, and more accurate
![Page 14: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/14.jpg)
Time Zone: America/Chicago
![Page 15: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/15.jpg)
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
![Page 16: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/16.jpg)
IANA Time Zone Libraries• JavaScript - Moment TimeZone
• 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
![Page 17: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/17.jpg)
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
![Page 18: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/18.jpg)
ASSUMPTIONSThings we think we know
![Page 19: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/19.jpg)
Assumption“If you just store everything in UTC, all your problems will be solved.”
![Page 20: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/20.jpg)
A Table With Everything in UTC
![Page 21: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/21.jpg)
Show me all the messages for the business day of April 4th.
I have users in London, and across the United States.
In London April 4th is between 2016-04-03 23:00:00Z and 2016-04-04 22:59:59ZIn Minneapolis April 4th is between 2016-04-04 05:00:00Z and 2016-04-05 04:59:59Z
![Page 22: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/22.jpg)
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
![Page 23: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/23.jpg)
2016-04-09T20:18:48-05:00
ISO 8601 Format with Offset
Local Date Local Time Offset
![Page 24: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/24.jpg)
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
![Page 25: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/25.jpg)
Assumption“If I just store everything in ISO 8601
with an offset, everything will be fine.”
![Page 26: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/26.jpg)
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
![Page 27: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/27.jpg)
Assumption“There are 24 hours in a day, and 365
days in a year.”
![Page 28: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/28.jpg)
moment('2016-03-12 12:00').add(1, 'day').format('LLL')
"March 13, 2016 12:00 PM"
moment('2016-03-12 12:00').add(24,'hour').format('LLL')
"March 13, 2016 1:00 PM"
![Page 29: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/29.jpg)
moment('2016-02-28').add(365, 'days').format('LLL')"February 27, 2017 12:00 AM"
moment('2016-02-28').add(1, 'year').format('LLL')"February 28, 2017 12:00 AM"
![Page 30: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/30.jpg)
As Seen on Stack Overflowvar 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);
![Page 31: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/31.jpg)
Assumption“Time and date math work in the
same way.”
![Page 32: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/32.jpg)
moment('2016-01-01').add(1.5, 'hours').format('LLL')"January 1, 2016 1:30 AM“
moment('2016-01-01').add(1.5, 'days').format('LLL')"January 3, 2016 12:00 AM"
![Page 33: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/33.jpg)
Time Math vs Date MathTime 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
![Page 34: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/34.jpg)
Assumption“All dates and times exist once in all
time zones”
![Page 35: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/35.jpg)
moment('2016-10-16').format('LLL')
October 16, 2016 1:00 AM
![Page 36: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/36.jpg)
Spring Forward in JavaScript
![Page 37: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/37.jpg)
Fall Back in JavaScript
![Page 38: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/38.jpg)
Assumption“The Date object in JavaScript
works.”
![Page 39: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/39.jpg)
var a = new Date('2016-01-01');a.toString();
"Thu Dec 31 2015 18:00:00 GMT-0600 (Central Standard Time)"
var a = new Date('1/1/2016');a.toString();
"Fri Jan 01 2016 00:00:00 GMT-0600 (Central Standard Time)"
![Page 40: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/40.jpg)
Known JavaScript Date Issues• DST Transitions 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/
![Page 41: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/41.jpg)
IN SUMMARYWe all know what happens when you assume
![Page 42: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/42.jpg)
CONSIDER ALL PERSPECTIVES
![Page 43: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/43.jpg)
DISTINGUISH BETWEEN LOCAL AND ABSOLUTE TIME
![Page 44: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/44.jpg)
REMEMBER, TIME ZONES CHANGE RAPIDLY
![Page 45: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/45.jpg)
STORE FUTURE DATES IN LOCAL TIME
![Page 46: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/46.jpg)
DISTINGUISH BETWEEN DATE MATH AND TIME MATH
![Page 47: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/47.jpg)
DON’T TRUST JAVASCRIPT DATE
![Page 48: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/48.jpg)
USE A QUALITY LIBRARY
![Page 49: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/49.jpg)
MAKE NO ASSUMPTIONS
![Page 50: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/50.jpg)
Be like this guy:
![Page 51: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/51.jpg)
Additional Resources• Date and Time Fundamentals – Matt Johnson, Pluralsight
• https://www.pluralsight.com/courses/date-time-fundamentals• Matt Johnson’s Blog
• http://codeofmatt.com/• Lau Taarnskov’s Blog
• http://www.creativedeletion.com/• NodaTime’s Documentation
• http://nodatime.org/• Time Programming Fundamentals – Greg Miller CPPCon 2015
• http://bit.ly/1SgO3E0• Moment.js
• http://momentjs.com/
![Page 52: Date and Time Odds Ends Oddities](https://reader031.vdocuments.us/reader031/viewer/2022013004/58773bbc1a28ab342e8b5a4f/html5/thumbnails/52.jpg)
About Mewww.maggiepint.com
@maggiepint
https://github.com/maggiepint
https://www.tempworks.com