Skip to content

Commit f9cb7bc

Browse files
committed
Improved time units and time zones integration
1 parent c476ef9 commit f9cb7bc

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

servercom/_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ def __setattr__(self, *args, **kwargs):
1818
if hasattr(self, '_frozen'):
1919
raise AttributeError("This object is immutable. You cannot set any instance variables.")
2020
object.__setattr__(self, *args, **kwargs)
21+
22+
def enum(**enums):
23+
"""Fake enum-maker"""
24+
return type('Enum', (), enums)

servercom/implementations/circuitpy.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .common import *
44
from ..timetools import Time
5+
from .._utils import enum
56

67
import ssl
78
import wifi
@@ -22,10 +23,6 @@ def _replace_code(new_code: bytes, do_reset=True):
2223
if do_reset:
2324
reset()
2425

25-
def enum(**enums):
26-
"""Fake enum-maker"""
27-
return type('Enum', (), enums)
28-
2926
def basic_auth_str(user: str, pwd: str) -> str:
3027
"""Encodes the username and password as per RFC7617 on Basic Auth"""
3128
return b2a_base64(f"{user}:{pwd}".encode()).strip().decode("utf-8")

servercom/timetools.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,26 @@
33
(to a precision of 1 second)
44
"""
55

6-
from time import monotonic
7-
from ._utils import Immutable
6+
from time import monotonic, struct_time
7+
from ._utils import Immutable, enum
88

99
# Time unit declarations in seconds:
10-
MINUTE = 60
11-
HOUR = 3600
12-
DAY = HOUR * 24
13-
WEEK = DAY * 7
14-
YEAR = DAY * 365
10+
TimeUnit = enum(
11+
SECOND = 1,
12+
MINUTE = 60,
13+
HOUR = 3600,
14+
DAY = 3600 * 24,
15+
WEEK = 3600 * 24 * 7,
16+
YEAR = 3600 * 24 * 365
17+
)
1518

1619
# Time zone UTC offsets:
17-
EST = -5 * HOUR
18-
EDT = -4 * HOUR
20+
TimeZone = enum(
21+
UTC = 0,
22+
GMT = 0,
23+
EST = -5 * TimeUnit.HOUR,
24+
EDT = -4 * TimeUnit.HOUR
25+
)
1926

2027
class Time(Immutable):
2128
"""Represents an instant in time based around a UNIX timestamp"""
@@ -53,8 +60,13 @@ def now(cls) -> 'Time':
5360
return cls(cls.get_unix_time())
5461

5562
# Constructor and Instance Methods:
56-
def __init__(self, unix_timestamp: int, absolute: bool = True):
57-
self.seconds = unix_timestamp
63+
def __init__(
64+
self,
65+
value: int,
66+
unit: TimeUnit = TimeUnit.SECOND,
67+
absolute: bool = True
68+
):
69+
self.seconds = value * unit
5870
self.absolute = absolute
5971
super().__init__()
6072

@@ -74,11 +86,11 @@ def offset(self, seconds:int=0, minutes:int=0, hours:int=0, days:int=0, weeks:in
7486
"""
7587
return Time(self.seconds
7688
+ seconds
77-
+ minutes * MINUTE
78-
+ hours * HOUR
79-
+ days * DAY
80-
+ weeks * WEEK
81-
+ years * YEAR
89+
+ minutes * TimeUnit.MINUTE
90+
+ hours * TimeUnit.HOUR
91+
+ days * TimeUnit.DAY
92+
+ weeks * TimeUnit.WEEK
93+
+ years * TimeUnit.YEAR
8294
)
8395

8496
def since_last(self, timeunit: int) -> 'Time':
@@ -137,6 +149,11 @@ def __div__(self, other) -> 'Time':
137149
self.seconds // other,
138150
absolute=False
139151
)
152+
153+
def __floordiv__(self, other: int) -> int:
154+
if not isinstance(other, int) or self.absolute:
155+
raise TypeError("Floor division is only to be used as Time // TimeUnit (int) to find the number of a unit that fits in a given relative time/time delta")
156+
return self.seconds // other
140157

141158
def __mod__(self, other: 'Time') -> 'Time':
142159
if not (isinstance(other, Time) or isinstance(other, int)):

0 commit comments

Comments
 (0)