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

The time relativity principle

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.

B5405045448a182b8ee12236ec63d8b0?s=128

Jakub Marchwicki

May 17, 2019
Tweet

Transcript

  1. The time relativity principle Jakub Marchwicki <@kubem>

  2. Software engineer Consultant Trainer Chief Mob Officer Jakub Marchwicki <@kubem>

    http://jakub.marchwicki.pl
  3. <BusStopTime> <BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T07:20:00</PassingTime> </BusStopTime>

  4. <BusStopTime> <BusStopId>1023</BusStopId> <Order>1</Order> <PassingTime>1899-12-30T07:20:00</PassingTime> </BusStopTime>

  5. <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>
  6. <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>
  7. Why 1899-12-30T07:20:00? jakub marchwicki <@kubem>

  8. What is time “zero”? https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem>

  9. What is time “zero”? January 1, 1970 - Linux, Java,

    JavaScript, PHP https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem>
  10. 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>
  11. 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>
  12. 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>
  13. http://www.odi.ch/prog/design/datetime.php FALSEHOOD Epoch is the universal representation of beginning of

    time in computer systems jakub marchwicki <@kubem>
  14. 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>
  15. 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
  16. 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
  17. What is the timezone for Europe/Warsaw CET (UTC+1h) / CEST

    (UTC+2h) https://en.wikipedia.org/wiki/Epoch_(computing) jakub marchwicki <@kubem>
  18. 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>
  19. http://www.odi.ch/prog/design/datetime.php FALSEHOOD My timezone will never change jakub marchwicki <@kubem>

  20. 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>
  21. Let’s pretend you are Samoan

  22. None
  23. samoa

  24. samoa international date line

  25. samoa international date line

  26. None
  27. UTC +12:00 UTC -11:00

  28. 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>
  29. Let’s convert var xmlGregoriaCalendar = DatatypeFactory.newInstance() .newXMLGregorianCalendar("1899-12-30T07:20:00") LocalDateTime.parse(xmllGregoriaCalendar.toXmlFormat()) .toLocalTime() jakub

    marchwicki <@kubem>
  30. http://www.odi.ch/prog/design/datetime.php FALSEHOOD A wall clock time everything jakub marchwicki <@kubem>

  31. 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.
  32. http://www.odi.ch/prog/design/datetime.php FALSEHOOD UTC all the things jakub marchwicki <@kubem>

  33. <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
  34. <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>
  35. 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>
  36. 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>
  37. Thank you! https://speakerdeck.com/kubamarchwicki/the-time-relativity-principle Please vote and rate the session

  38. 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>