$30 off During Our Annual Pro Sale. View Details »

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.

Jakub Marchwicki

May 17, 2019
Tweet

More Decks by Jakub Marchwicki

Other Decks in Technology

Transcript

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

    View Slide

  2. Software engineer
    Consultant
    Trainer
    Chief Mob Officer
    Jakub Marchwicki <@kubem>
    http://jakub.marchwicki.pl

    View Slide


  3. 1023
    1
    1899-12-30T07:20:00

    View Slide


  4. 1023
    1
    1899-12-30T07:20:00

    View Slide


  5. 106
    131


    1017
    0
    1899-12-30T07:18:00


    1023
    1
    1899-12-30T07:20:00



    View Slide


  6. 106
    131


    1017
    0
    1899-12-30T07:18:00


    1023
    1
    1899-12-30T07:20:00



    View Slide

  7. Why 1899-12-30T07:20:00?
    jakub marchwicki <@kubem>

    View Slide

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

    View Slide

  9. What is time “zero”?
    January 1, 1970 - Linux, Java, JavaScript, PHP
    https://en.wikipedia.org/wiki/Epoch_(computing)
    jakub marchwicki <@kubem>

    View Slide

  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>

    View Slide

  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>

    View Slide

  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>

    View Slide

  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>

    View Slide

  14. Let’s convert - naive approach
    minOccurs="1" maxOccurs="1" type="s:dateTime" />
    @XmlElement(name = "PassingTime", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar passingTime;
    jakub marchwicki <@kubem>

    View Slide

  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

    View Slide

  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

    View Slide

  17. What is the timezone for Europe/Warsaw
    CET (UTC+1h) / CEST (UTC+2h)
    https://en.wikipedia.org/wiki/Epoch_(computing)
    jakub marchwicki <@kubem>

    View Slide

  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>

    View Slide

  19. http://www.odi.ch/prog/design/datetime.php
    FALSEHOOD
    My timezone will never change
    jakub marchwicki <@kubem>

    View Slide

  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>

    View Slide

  21. Let’s pretend you are Samoan

    View Slide

  22. View Slide

  23. samoa

    View Slide

  24. samoa
    international
    date line

    View Slide

  25. samoa
    international
    date line

    View Slide

  26. View Slide

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

    View Slide

  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>

    View Slide

  29. Let’s convert
    var xmlGregoriaCalendar = DatatypeFactory.newInstance()
    .newXMLGregorianCalendar("1899-12-30T07:20:00")
    LocalDateTime.parse(xmllGregoriaCalendar.toXmlFormat())
    .toLocalTime()
    jakub marchwicki <@kubem>

    View Slide

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

    View Slide

  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.

    View Slide

  32. http://www.odi.ch/prog/design/datetime.php
    FALSEHOOD
    UTC all the things
    jakub marchwicki <@kubem>

    View Slide


  33. 124
    219


    114
    0
    1899-12-30T03:48:00


    1023
    1
    1899-12-30T03:51:00




    124
    219


    114
    0
    1899-12-30T03:48:00Z


    1023
    1
    1899-12-30T03:51:00




    124
    219


    114
    0
    228


    1023
    1
    1899-12-30T03:51:00




    124
    219


    114
    0
    PT3H48M


    1023
    1
    1899-12-30T03:51:00



    Why can’t we do just LocalTime

    View Slide


  34. 149
    112


    114
    0
    1899-12-30T03:48:00





    622
    100



    1963
    43
    1899-12-31T04:26:00



    View Slide

  35. Let’s convert
    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>

    View Slide

  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>

    View Slide

  37. Thank you!
    https://speakerdeck.com/kubamarchwicki/the-time-relativity-principle
    Please vote and rate the session

    View Slide

  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>

    View Slide