Share
Embed
The time relativity principle
The moment when you code. You type. You type a lot. You type `LocalDateTime`. The moment you know you are doomed. How hard can time be? Sometimes, time gives us a hard time. This time, we will have a deeper dive into what it takes for a String of `2019-05-15T09:00:00` to become an instance of `java.time.Instant`. This is what this lightning talk will be about.
Jakub Marchwicki
May 17, 2019
Transcript
38 slides
-
Software engineer Consultant Trainer Chief Mob Officer Jakub Marchwicki <@kubem>
http://jakub.marchwicki.pl -
<Expedition> <LineId>106</LineId> <RouteId>131</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>1017</BusStopId> <Order>0</Order> <PassingTime>1899-12-30T07:18:00</PassingTime> </BusStopTime> <BusStopTime>
<BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T07:20:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> -
<Expedition> <LineId>106</LineId> <RouteId>131</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>1017</BusStopId> <Order>0</Order> <PassingTime>1899-12-30T07:18:00</PassingTime> </BusStopTime> <BusStopTime>
<BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T07:20:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> -
What is time “zero”? January 1, 1970 - Linux, Java,
JavaScript, PHP https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
What is time “zero”? January 1, 1970 - Linux, Java,
JavaScript, PHP January 1, 2000 - PostgreSQL January 1, 2001 - Apple Cocoa https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
What is time “zero”? January 1, 1 - Microsoft .NET
January 1, 1601 - COBOL January 1, 1900 - Common Lisp January 1, 1970 - Linux, Java, JavaScript, PHP January 1, 2000 - PostgreSQL January 1, 2001 - Apple Cocoa https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
What is time “zero”? January 1, 1 - Microsoft .NET
January 1, 1601 - COBOL December 30, 1899 - Microsoft COM DATE January 1, 1900 - Common Lisp January 1, 1970 - Linux, Java, JavaScript, PHP January 1, 2000 - PostgreSQL January 1, 2001 - Apple Cocoa https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
http://www.odi.ch/prog/design/datetime.php FALSEHOOD Epoch is the universal representation of beginning of
time in computer systems jakub marchwicki <@kubem> -
Let’s convert - naive approach <soap:element name="PassingTime" minOccurs="1" maxOccurs="1" type="s:dateTime"
/> @XmlElement(name = "PassingTime", required = true) @XmlSchemaType(name = "dateTime") protected XMLGregorianCalendar passingTime; jakub marchwicki <@kubem> -
Let’s convert - the quiz var date = DatatypeFactory.newInstance() .newXMLGregorianCalendar("1899-12-30T07:20:00")
.toGregorianCalendar().toInstant() A. 07:20 B. 07:44 jakub marchwicki <@kubem> Point in time, epoch-seconds measured from the standard Java epoch of 1970-01-01T00:00:00Z As we have number of seconds measured from UTC, we need to say which timezone we are in LocalDateTime.ofInstant(date, ZoneId.of("Europe/Warsaw")) .toLocalTime() C. 07:04 D. 06:20 jshell> LocalDateTime.of(1899, 12, 30, 7, 20).toInstant(UTC).getEpochSecond() $8 ==> -2209135200 -
Let’s convert - the quiz var date = DatatypeFactory.newInstance() .newXMLGregorianCalendar("1899-12-30T07:20:00")
.toGregorianCalendar().toInstant() LocalDateTime.ofInstant(date, ZoneId.of("Europe/Warsaw")) .toLocalTime() A. 07:20 B. 07:44 C. 07:04 D. 06:20 -
What is the timezone for Europe/Warsaw CET (UTC+1h) / CEST
(UTC+2h) https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
UTC+1h - 1965 - 1976 UTC+1h / UTC+2h - 1957
- 1964 … UTC+1:24h - 1800 - 1914 What is the timezone for Europe/Warsaw CET (UTC+1h) / CEST (UTC+2h) - since 1977 https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem> -
time zone rules change, and that applications should expect that
they will change. This isn’t a corner case, it’s the normal way things work https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/ TAKEAWAY jakub marchwicki <@kubem> -
The rule of the game for programming with time is
that you should opt to go as simple as possible as soon as possible. https://zachholman.com/talk/utc-is-enough-for-everyone-right TAKEAWAY jakub marchwicki <@kubem> -
Don’t be like the Russian Olympic shooting team The Russian
Olympic shooting team was proud to head to London for the Olympic Games in 1908 They saved the date, hit the road And arrived to London two days too late. Missed their game Great Britain was on Gregorian Calendar since 1752. Poland since 1582 (first to adopt) Russian adopted Gregorian Calendar in 1918. Switched from Julian Calendar, changing 1st February 1918 to 14th of Feb. -
<Expedition> <LineId>124</LineId> <RouteId>219</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>114</BusStopId> <Order>0</Order> <PassingTime>1899-12-30T03:48:00</PassingTime> </BusStopTime> <BusStopTime>
<BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T03:51:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> <Expedition> <LineId>124</LineId> <RouteId>219</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>114</BusStopId> <Order>0</Order> <PassingTime>1899-12-30T03:48:00Z</PassingTime> </BusStopTime> <BusStopTime> <BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T03:51:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> <Expedition> <LineId>124</LineId> <RouteId>219</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>114</BusStopId> <Order>0</Order> <PassingTime>228</PassingTime> </BusStopTime> <BusStopTime> <BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T03:51:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> <Expedition> <LineId>124</LineId> <RouteId>219</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>114</BusStopId> <Order>0</Order> <PassingTime>PT3H48M</PassingTime> </BusStopTime> <BusStopTime> <BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T03:51:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> Why can’t we do just LocalTime -
<Expedition> <LineId>149</LineId> <RouteId>112</RouteId> <BusStopsTime> <BusStopTime> <BusStopId>114</BusStopId> <Order>0</Order> <PassingTime>1899-12-30T03:48:00</PassingTime> </BusStopTime> <!--
… --> </BusStopsTime> </Expedition> <Expedition> <LineId>622</LineId> <RouteId>100</RouteId> <BusStopsTime> <!-- … --> <BusStopTime> <BusStopId>1963</BusStopId> <Order>43</Order> <PassingTime>1899-12-31T04:26:00</PassingTime> </BusStopTime> </BusStopsTime> </Expedition> -
Let’s convert <soap:element name="PassingTime" minOccurs="1" maxOccurs="1" type="s:long" /> protected Long
passingTime; LocalDateTime.of( LocalDate.of(2019, 11, 07), LocalTime.MIDNIGHT.plus(Duration.ofMinutes(passingTime)) ) jakub marchwicki <@kubem> -
https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/ TAKEAWAY it depends! storing UTC is not always the
wrong thing to do, but it’s not always the right thing to do either. jakub marchwicki <@kubem> -
Dates : Time : Timezones :: Links • There is
an ingenious talk by Tomek Nurkiewicz on timezones http://nurkiewicz.github.io/talks/confitura2013/ • Awesome falsehood on time & timezones https://github.com/kdeldycke/awesome-falsehood#dates-and-time • Canvas for this talk was an actual problem https://stackoverflow.com/questions/54326076/timezone-inconsistenci es-on-converting-xmlgregoriancalendar-to-localdatetime • No timezones. Ever! https://en.wikipedia.org/wiki/International_Fixed_Calendar • These slides https://speakerdeck.com/kubamarchwicki/the-time-relativity-principle Jakub Marchwicki <@kubem>