Skip to content

Commit f58a57a

Browse files
authored
Merge pull request #25 from Windham-High-School/16-timetools-precision-is-bad-on-cricuitpython
16 timetools precision is bad on cricuitpython
2 parents 4290b54 + f906ed1 commit f58a57a

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

servercom/implementations/circuitpy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def get_status(self) -> GameStatus:
389389
resp_json = loads(resp[1])
390390
if self.v:
391391
print(f"It is {resp_json['unix_time']} seconds since the epoch.")
392-
return GameStatus(Time(resp_json['unix_time']), resp_json['status']['score'], resp_json['CubeServer_version'])
392+
return GameStatus(Time(resp_json['2020_time']), resp_json['status']['score'], resp_json['CubeServer_version'])
393393

394394
def sync_time(self) -> bool:
395395
"""Syncs the current clock against the server"""

servercom/timetools.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Time Tools
22
Some tools for dealing with times on microcontrollers
33
(to a precision of 1 second)
4+
5+
Note that epoch time is really time since 2020 if you're using CircuitPython
46
"""
57

68
from time import monotonic, struct_time
@@ -24,6 +26,9 @@
2426
EDT = -4 * TimeUnit.HOUR
2527
)
2628

29+
# Offset to allow values to fit in 32-bit signed integers for CircuitPython:
30+
HIGH_PRECISION_OFFSET = TimeUnit.YEAR * 50 # 50 years since epoch to make 0 2020-01-01 00:00:00
31+
2732
class Time(Immutable):
2833
"""Represents an instant in time based around a UNIX timestamp"""
2934

@@ -170,7 +175,7 @@ def __mod__(self, other: 'Time') -> 'Time':
170175

171176
def __str__(self) -> str:
172177
if self.absolute:
173-
return f"{self.seconds} seconds since 1 January 1970 00:00:00 UTC"
178+
return f"{self.seconds} seconds since 1 January 2020 or 1970 00:00:00 UTC"
174179
return f"{self.seconds} seconds"
175180

176181
def __repr__(self) -> str:

0 commit comments

Comments
 (0)