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
2027class 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