Skip to content

Commit 90f7a12

Browse files
committed
feat: create utc method, start type annotations
1 parent da1b17c commit 90f7a12

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

pytool/time.py

+36-16
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,47 @@ class UTC(datetime.tzinfo):
6868
"""
6969

7070
@property
71-
def _utcoffset(self):
71+
def _utcoffset(self) -> datetime.timedelta:
7272
"""Helps make this work with pytz."""
7373
return datetime.timedelta(0)
7474

75-
def utcoffset(self, stamp):
75+
def utcoffset(self, stamp) -> datetime.timedelta:
7676
return datetime.timedelta(0)
7777

78-
def tzname(self, stamp):
78+
def tzname(self, stamp) -> str:
7979
return "UTC"
8080

81-
def dst(self, stamp):
81+
def dst(self, stamp) -> datetime.timedelta:
8282
return datetime.timedelta(0)
8383

84-
def __repr__(self):
84+
def __repr__(self) -> str:
8585
return "UTC()"
8686

87-
def __reduce__(self):
87+
def __reduce__(self) -> tuple:
8888
# This method makes the UTC object pickleable
8989
return UTC, ()
9090

9191

92+
def utc(
93+
year, month, day, hour=0, minute=0, second=0, microsecond=0
94+
) -> datetime.datetime:
95+
"""Return a timezone-aware datetime object for the given UTC time.
96+
97+
:param int year: Year
98+
:param int month: Month
99+
:param int day: Day
100+
:param int hour: Hour (default: ``0``)
101+
:param int minute: Minute (default: ``0``)
102+
:param int second: Second (default: ``0``)
103+
:param int microsecond: Microsecond (default: ``0``)
104+
:returns: UTC datetime object
105+
106+
"""
107+
return datetime.datetime(
108+
year, month, day, hour, minute, second, microsecond, tzinfo=UTC()
109+
)
110+
111+
92112
def is_dst(stamp):
93113
"""Return ``True`` if `stamp` is daylight savings.
94114
@@ -99,7 +119,7 @@ def is_dst(stamp):
99119
return time.localtime(time.mktime(stamp.timetuple())).tm_isdst == 1
100120

101121

102-
def utcnow():
122+
def utcnow() -> datetime.datetime:
103123
"""Return the current UTC time as a timezone-aware datetime.
104124
105125
:returns: The current UTC time
@@ -108,7 +128,7 @@ def utcnow():
108128
return datetime.datetime.now(UTC())
109129

110130

111-
def fromutctimestamp(stamp):
131+
def fromutctimestamp(stamp) -> datetime.datetime:
112132
"""Return a timezone-aware datetime object from a UTC unix timestamp.
113133
114134
:param float stamp: Unix timestamp in UTC
@@ -131,7 +151,7 @@ def fromutctimestamp(stamp):
131151
return new_stamp
132152

133153

134-
def toutctimestamp(stamp):
154+
def toutctimestamp(stamp) -> datetime.datetime:
135155
"""Converts a naive datetime object to a UTC unix timestamp. This has an
136156
advantage over `time.mktime` in that it preserves the decimal portion
137157
of the timestamp when converting.
@@ -156,7 +176,7 @@ def toutctimestamp(stamp):
156176
return time.mktime(stamp.timetuple()) + decimal
157177

158178

159-
def as_utc(stamp):
179+
def as_utc(stamp) -> datetime.datetime:
160180
"""Converts any datetime (naive or aware) to UTC time.
161181
162182
:param datetime stamp: Datetime to convert
@@ -173,7 +193,7 @@ def as_utc(stamp):
173193
return fromutctimestamp(toutctimestamp(stamp))
174194

175195

176-
def trim_time(stamp):
196+
def trim_time(stamp) -> datetime.datetime:
177197
"""Trims the time portion off of `stamp`, leaving the date intact.
178198
Returns a datetime of the same date, set to 00:00:00 hours. Preserves
179199
timezone information.
@@ -185,7 +205,7 @@ def trim_time(stamp):
185205
return datetime.datetime(*stamp.date().timetuple()[:-3], tzinfo=stamp.tzinfo)
186206

187207

188-
def week_start(stamp):
208+
def week_start(stamp) -> datetime.datetime:
189209
"""Return the start of the week containing `stamp`.
190210
191211
.. versionchanged:: 2.0
@@ -200,7 +220,7 @@ def week_start(stamp):
200220
return stamp
201221

202222

203-
def week_seconds(stamp):
223+
def week_seconds(stamp) -> int:
204224
"""Return `stamp` converted to seconds since 00:00 Monday.
205225
206226
:param datetime stamp: Timestamp to convert
@@ -211,7 +231,7 @@ def week_seconds(stamp):
211231
return int(difference.total_seconds())
212232

213233

214-
def week_seconds_to_datetime(seconds):
234+
def week_seconds_to_datetime(seconds) -> datetime.datetime:
215235
"""Return the datetime that is `seconds` from the start of this week.
216236
217237
:param int seconds: Seconds
@@ -221,7 +241,7 @@ def week_seconds_to_datetime(seconds):
221241
return week_start(datetime.datetime.now()) + datetime.timedelta(seconds=seconds)
222242

223243

224-
def make_week_seconds(day, hour, minute=0, seconds=0):
244+
def make_week_seconds(day, hour, minute=0, seconds=0) -> int:
225245
"""Return :func:`week_seconds` for the given `day` of the week, `hour`
226246
and `minute`.
227247
@@ -240,7 +260,7 @@ def make_week_seconds(day, hour, minute=0, seconds=0):
240260
return week_seconds(stamp)
241261

242262

243-
def floor_minute(stamp=None):
263+
def floor_minute(stamp=None) -> datetime.datetime:
244264
"""Return `stamp` floored to the current minute. If no `stamp` is
245265
specified, the current time is used. Preserves timezone information.
246266

0 commit comments

Comments
 (0)