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

The programmers guide to timestamps and timezones - PyCon India 2023

The programmers guide to timestamps and timezones - PyCon India 2023

Shrayas Rajagopal

October 02, 2023
Tweet

More Decks by Shrayas Rajagopal

Other Decks in Programming

Transcript

  1. The Programmers guide
    to timestamps and time zones

    View Slide

  2. Head of engineering, Logic Soft
    @shrayasr
    Shrayas

    View Slide

  3. My Aim
    🎯

    View Slide

  4. Foster
    conceptual
    thinking.

    View Slide

  5. Assumptions
    TODAYS

    View Slide

  6. Assumptions
    Leap seconds

    View Slide

  7. Assumptions
    Leap seconds

    View Slide

  8. Assumptions
    Leap seconds
    Storage layer - PostgreSQL

    View Slide

  9. Assumptions
    Leap seconds
    Storage layer - PostgreSQL
    Testing is left as an exercise
    to the listener

    View Slide

  10. Agenda

    View Slide

  11. Agenda
    Concepts & the chaos
    My recommendations
    Takeaways

    View Slide

  12. Concepts

    View Slide

  13. EPOCH
    1

    View Slide

  14. Reference Point in the past
    THEN
    NOW

    View Slide

  15. January 1st 1970 00:00:00

    View Slide

  16. INSTANT
    2

    View Slide

  17. View Slide

  18. 󰏝

    View Slide

  19. 👀
    󰑑
    👀
    󰑜
    👀
    󰎲
    󰏝

    View Slide

  20. INSTANT
    =
    UNAMBIGUOUS

    View Slide

  21. EPOCH
    Number of seconds since epoch

    View Slide

  22. LOCAL
    DATE
    TIME
    {
    3

    View Slide

  23. 👀
    󰑑
    👀
    󰑜
    👀
    󰎲
    󰏝

    View Slide

  24. 👀
    󰑑
    👀
    󰑜
    👀
    󰎲 September 17th
    11:15AM
    September 17th
    4:15PM
    September 17th
    12:15PM
    󰏝
    September 17th
    2:45PM

    View Slide

  25. 👀
    󰑑
    👀
    󰑜
    👀
    󰎲 September 17th
    11:15AM
    September 17th
    4:15PM
    September 17th
    12:15PM
    󰏝
    September 17th
    2:45PM
    Wall Time

    View Slide

  26. TIME ZONES
    4

    View Slide

  27. View Slide

  28. “Yeah, let’s meet at 3:15PM”

    View Slide

  29. A group of places, that observes the same
    time on their watches, for any given
    instant.

    View Slide

  30. 󰑑 Africa/Dar_es_Salaam
    󰎲 Europe/Berlin
    󰑜 Asia/Ho_Chi_Minh
    󰏝 Asia/Kolkata
    󰑔 America/New_York

    View Slide

  31. View Slide

  32. UTC
    5
    Coordinated Universal Time

    View Slide

  33. 👀
    󰑑
    👀
    󰑜
    👀
    󰎲 September 17th
    11:15AM
    Europe/Berlin
    September 17th
    4:15PM
    Asia/Ho Chi Minh
    September 17th
    12:15PM
    Africa/Dar es Salaam
    󰏝
    September 17th
    2:45PM
    Asia/Kolkata

    View Slide

  34. View Slide

  35. EPOCH
    Number of seconds since epoch

    View Slide

  36. EPOCH
    1694942100 seconds since epoch

    View Slide

  37. 1694942100
    instant

    View Slide

  38. January 1st 1970 00:00:00
    +
    1694942100s

    View Slide

  39. September 17th 2023 9:15AM

    View Slide

  40. Europe/Berlin
    September 17th
    11:15AM
    Asia/Ho Chi Minh
    September 17th
    4:15PM
    Africa/Dar es Salaam
    September 17th
    12:15PM
    Asia/Kolkata
    September 17th
    2:45PM
    󰏝
    󰑑 󰑜
    󰎲
    September 17th 2023 9:15AM

    View Slide

  41. September 17th 2023 9:15AM
    Europe/Berlin
    September 17th
    11:15AM (+2)
    Asia/Ho Chi Minh
    September 17th
    4:15PM (+7)
    Africa/Dar es Salaam
    September 17th
    12:15PM (+3)
    Asia/Kolkata
    September 17th
    2:45PM (+05:30)
    󰏝
    󰑑 󰑜
    󰎲

    View Slide

  42. September 17th 2023 9:15AM (+0)
    Europe/Berlin
    September 17th
    11:15AM (+2)
    Asia/Ho Chi Minh
    September 17th
    4:15PM (+7)
    Africa/Dar es Salaam
    September 17th
    12:15PM (+3)
    Asia/Kolkata
    September 17th
    2:45PM (+05:30)
    󰏝
    󰑑 󰑜
    󰎲

    View Slide

  43. REFERENCE TIME ZONE

    View Slide

  44. DST
    Daylight Savings Time
    6

    View Slide

  45. CHENNAI
    +05:30

    View Slide

  46. Sun Graph @ Chennai, India

    View Slide

  47. BERLIN
    +02:00

    View Slide

  48. Sun Graph @ Berlin, Germany

    View Slide

  49. SUMMER TIME

    View Slide

  50. DST
    Spring Forward
    Fall Back

    View Slide

  51. Sun Graph @ Berlin, Germany

    View Slide

  52. Sun Graph @ Berlin, Germany
    SPRING FORWARD

    View Slide

  53. On 26th of March, Just as the local time
    reaches 2:00AM, move the clock forward
    to 3:00AM
    Europe/Berlin
    +02:00 -> +01:00

    View Slide

  54. 01:58
    01:59
    03:00

    View Slide

  55. 01:58
    01:59
    03:00

    View Slide

  56. 02:00
    to?
    02:59

    View Slide

  57. 02:00
    to
    02:59

    View Slide

  58. 02:00
    to
    02:59
    Imaginary Times

    View Slide

  59. Sun Graph @ Berlin, Germany
    FALL BACK

    View Slide

  60. On 29th of October, Just as the local time
    reaches 3:00AM, move the clock backward
    to 2:00AM
    Europe/Berlin
    +01:00 -> +02:00

    View Slide

  61. 02:58
    02:59
    02:00

    View Slide

  62. 02:58
    02:59
    02:00

    View Slide

  63. 02:00
    to?
    02:59

    View Slide

  64. 02:00
    to
    02:59
    02:00
    to
    02:59
    .- OCCURS TWICE .>

    View Slide

  65. 02:00
    to
    02:59
    02:00
    to
    02:59
    .- OCCURS TWICE .>

    View Slide

  66. 02:00
    to
    02:59
    02:00
    to
    02:59
    .- OCCURS TWICE .>
    Ambiguous Times

    View Slide

  67. THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS

    View Slide

  68. THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    THE CHAOS THE CHAOS THE CHAOS THE CHAOS
    The Chaos

    View Slide

  69. View Slide

  70. NO

    View Slide

  71. View Slide

  72. View Slide

  73. View Slide

  74. View Slide

  75. View Slide

  76. View Slide

  77. 2023

    View Slide

  78. View Slide

  79. ✨TZDB✨
    Time Zone Database
    Paul Eggert
    IANA

    View Slide

  80. The tzdb is a collaborative compilation of
    information about the world's time zones,
    primarily intended for use with computer
    programs and operating systems

    View Slide


  81. TZDB

    YOUR
    AWESOME
    APP

    View Slide

  82. One more thing …

    View Slide


  83. TZDB

    YOUR
    AWESOME
    APP

    View Slide


  84. TZDB

    YOUR
    AWESOME
    APP

    View Slide

  85. Decision

    TZDB

    Windows
    OSX
    Linux
    iOS
    Android
    Languages
    Databases
    Frameworks
    YOUR
    AWESOME
    APP

    View Slide

  86. View Slide

  87. View Slide

  88. Recommendations

    View Slide

  89. Case 1

    View Slide

  90. Alice
    Bob
    󰏝 󰎲
    Heyy, what’s up? How are you?

    View Slide

  91. Alice
    Bob
    󰏝
    󰎲
    Heyy, what’s up? How are you?
    󰐵 SERVER

    View Slide

  92. 󰏝
    󰎲
    󰐵 SERVER
    󰑡
    󰏶

    View Slide

  93. The server’s operating
    system should be on the
    UTC time zone
    ⭐ RECOMMENDATION 1 ⭐

    View Slide

  94. 󰏝
    Bob Heyy, what’s up? How are you?
    At 2:45PM

    View Slide

  95. Heyy, what’s up? How are you? 󰏝
    Bob
    󰐵 SERVER
    Message

    View Slide

  96. Store timestamps of
    events in the past in UTC
    ⭐ RECOMMENDATION 2 ⭐

    View Slide

  97. create table messages
    (
    ...
    message text,
    sent_at timestamp with time zone,
    ...
    );

    View Slide

  98. ..> datetime.now(timezone.utc)
    datetime.datetime(2023, 9, 23, 11, 27, 7,
    725900, tzinfo = datetime.timezone.utc)

    View Slide

  99. Heyy, what’s up? How are you? 󰏝
    Bob
    󰐵 SERVER
    Message

    View Slide

  100. Heyy, what’s up? How are you? 󰏝
    Bob
    󰐵 SERVER
    Message ACK

    View Slide

  101. Server communicates
    only in UTC
    ⭐ RECOMMENDATION 3 ⭐

    View Slide

  102. 󰏝
    Bob Heyy, what’s up? How are you?
    9:15 AM
    At 2:45PM

    View Slide

  103. Client stores UTC &
    converts to local time
    ⭐ RECOMMENDATION 4 ⭐

    View Slide

  104. 󰏝
    Bob Heyy, what’s up? How are you?
    2:45PM
    At 2:45PM

    View Slide

  105. Heyy, what’s up? How are you?
    2:45PM
    󰏝
    Bob
    Alice 󰎲
    󰐵 SERVER
    Message

    View Slide

  106. Heyy, what’s up? How are you?
    2:45PM
    󰏝
    Bob
    Alice 󰎲
    󰐵 SERVER
    Message

    View Slide

  107. Alice
    󰐵 SERVER
    󰎲
    Message

    View Slide

  108. Server communicates
    only in UTC
    ⭐ RECOMMENDATION 3 ⭐

    View Slide

  109. Client stores UTC &
    converts to local time
    ⭐ RECOMMENDATION 4 ⭐

    View Slide

  110. Alice
    󰎲
    Heyy, what’s up? How are you?
    11:15AM
    󰐵 SERVER

    View Slide

  111. Heyy, what’s up? How are you?
    2:45PM
    󰏝
    Bob
    Alice
    󰎲
    󰐵 SERVER
    Heyy, what’s up? How are you?
    11:15AM

    View Slide

  112. Case 2

    View Slide

  113. Title: Company All Hands Meeting
    Start: April 28th 2024 - 01:00 PM 󰎺

    View Slide

  114. Title: Company All Hands Meeting
    Start: April 28th 2024 - 01:00 PM 󰎺
    󰐵 SERVER
    Send a reminder before
    the meeting

    View Slide

  115. Title: Company All Hands Meeting
    Start: April 28th 2024 - 01:00 PM 󰎺
    󰐵 SERVER
    Send a reminder before
    the meeting

    View Slide

  116. UTC
    JUST USE

    View Slide

  117. NOW

    View Slide

  118. NOW
    April 28th 2024, 01:00 PM
    Local Time (Africa/Cairo +3)
    APPOINTMENT

    View Slide

  119. NOW
    April 28th 2024, 01:00 PM
    Local Time (Africa/Cairo +3)
    April 28th 2024, 10:00 AM
    UTC +0
    APPOINTMENT

    View Slide

  120. NOW
    April 28th 2024, 10:00 AM
    UTC +0
    APPOINTMENT

    View Slide

  121. NOW APPOINTMENT
    Egypt to cancel DST and switch
    to the +2 time zone for the
    entire year of 2024
    March 1st, 2024

    View Slide

  122. NOW APPOINTMENT
    ✨TZDB✨
    Updated

    View Slide

  123. NOW
    April 28th 2024, 10:00 AM
    APPOINTMENT
    UTC +0

    View Slide

  124. NOW
    April 28th 2024, 10:00 AM
    APPOINTMENT
    UTC +0

    View Slide

  125. NOW
    April 28th 2024, 10:00 AM
    APPOINTMENT
    UTC +0
    April 28th 2024, 12:00 PM
    Local Time (Africa/Cairo +2)

    View Slide

  126. April 28th 2024, 01:00 PM
    Local Time (Africa/Cairo +3)

    View Slide

  127. April 28th 2024, 01:00 PM
    Local Time (Africa/Cairo +3)
    April 28th 2024, 12:00 PM
    Local Time (Africa/Cairo +2)

    View Slide

  128. April 28th 2024, 01:00 PM
    Local Time (Africa/Cairo +3)
    April 28th 2024, 12:00 PM
    Local Time (Africa/Cairo +2)

    View Slide

  129. NOW APPOINTMENT
    Egypt to cancel DST and switch
    to the +2 time zone for the
    entire year of 2024
    March 1st, 2024

    View Slide

  130. NOW APPOINTMENT
    Egypt to cancel DST and stay in
    the +2 time zone for the entire
    year of 2024
    March 1st, 2024
    RARE?

    View Slide

  131. 2013 9
    2014 10
    2015 7
    2016 10
    2017 3
    2018 9
    2019 3
    2020 6
    2021 5
    2022 7
    2023 3
    TZDB
    VERSIONS

    View Slide

  132. 2013 9
    2014 10
    2015 7
    2016 10
    2017 3
    2018 9
    2019 3
    2020 6
    2021 5
    2022 7
    2023 3
    TZDB
    VERSIONS
    AVERAGE
    6
    PER YEAR

    View Slide

  133. For future events, store
    the wall time with the
    timezone
    ⭐ RECOMMENDATION 5 ⭐

    View Slide

  134. create table appointments
    (
    ...
    title text,
    scheduled_at_local timestamp without time zone,
    scheduled_at_timezone text,
    ...
    );

    View Slide

  135. create table appointments
    (
    ...
    title text,
    scheduled_at_local timestamp without time zone,
    scheduled_at_timezone text,
    scheduled_at_utc timestamp with time zone,
    ...
    );

    View Slide

  136. Every minute, check scheduled_at_utc
    Send notification
    Match found

    View Slide

  137. Recommendations
    🔁 A RECAP 🔁

    View Slide

  138. 1 Server OS sits in UTC time zone

    View Slide

  139. 1 Server OS sits in UTC time zone
    2 Server communicates only in UTC

    View Slide

  140. 1 Server OS sits in UTC time zone
    2 Server communicates only in UTC
    3 Store past-ish timestamps as UTC

    View Slide

  141. 1 Server OS sits in UTC time zone
    2 Server communicates only in UTC
    3 Store past-ish timestamps as UTC
    4 Store future timestamps as wall time
    with time zone

    View Slide

  142. 1 Server OS sits in UTC time zone
    2 Server communicates only in UTC
    3 Store past-ish timestamps as UTC
    4 Store future timestamps as wall time
    with time zone
    5 Client stores UTC and converts to
    local time

    View Slide

  143. View Slide

  144. Takeaways

    View Slide

  145. 1 Think about
    your domain
    1

    View Slide

  146. 1. Dates only?
    2. Times only?
    3. Both dates & times?
    4. Microsecond precision?
    5. Customers only in one time zone?

    View Slide

  147. 6. Limits on future datetimes?
    7. Any customers from volatile time zones?
    8. Current libraries in the stack
    9. Channel for customer intimation

    View Slide

  148. 2
    2Don’t guess.
    Just don’t.

    View Slide

  149. 3
    3UTC is good …
    till its not

    View Slide

  150. 4Respect the
    TZDB
    4

    View Slide

  151. Fin
    @shrayasr

    View Slide