Skip to content

Commit

Permalink
ntptime: Fix Year 2036 bug.
Browse files Browse the repository at this point in the history
Fix NTP client - it would report the wrong time after 7 Feb 2036.

Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
  • Loading branch information
jonfoster authored and dpgeorge committed May 15, 2024
1 parent a2e4efa commit cb281a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion micropython/net/ntptime/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(description="NTP client.", version="0.1.0")
metadata(description="NTP client.", version="0.1.1")

module("ntptime.py", opt=3)
27 changes: 26 additions & 1 deletion micropython/net/ntptime/ntptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,37 @@ def time():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.settimeout(timeout)
res = s.sendto(NTP_QUERY, addr)
s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]

# 2024-01-01 00:00:00 converted to an NTP timestamp
MIN_NTP_TIMESTAMP = 3913056000

# Y2036 fix
#
# The NTP timestamp has a 32-bit count of seconds, which will wrap back
# to zero on 7 Feb 2036 at 06:28:16.
#
# We know that this software was written during 2024 (or later).
# So we know that timestamps less than MIN_NTP_TIMESTAMP are impossible.
# So if the timestamp is less than MIN_NTP_TIMESTAMP, that probably means
# that the NTP time wrapped at 2^32 seconds. (Or someone set the wrong
# time on their NTP server, but we can't really do anything about that).
#
# So in that case, we need to add in those extra 2^32 seconds, to get the
# correct timestamp.
#
# This means that this code will work until the year 2160. More precisely,
# this code will not work after 7th Feb 2160 at 06:28:15.
#
if val < MIN_NTP_TIMESTAMP:
val += 0x100000000

# Convert timestamp from NTP format to our internal format

EPOCH_YEAR = utime.gmtime(0)[0]
if EPOCH_YEAR == 2000:
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
Expand Down

0 comments on commit cb281a4

Please sign in to comment.