Skip to content

ntptime: Year 2036 fix. #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading