34
34
# stdlib
35
35
import datetime
36
36
from collections import OrderedDict
37
+ from typing import Union
37
38
38
39
# 3rd party
39
40
try :
40
41
import pytz
41
42
42
- def get_utc_offset (tz , date = None ):
43
+ def get_utc_offset (tz , date : datetime . datetime = None ) -> datetime . timedelta :
43
44
"""
44
45
Returns the offset between UTC and the requested timezone on the given date.
45
46
If ``date`` is ``None`` then the current date is used.
46
47
47
- :param tz: :class:` pytz.timezone` or a string representing the timezone
48
+ :param tz: `` pytz.timezone` ` or a string representing the timezone
48
49
:type tz:
49
50
:param date:
50
- :type date: :class:` python:datetime.datetime` or None , optional
51
+ :type date: python:datetime.datetime, optional
51
52
:return:
52
- :rtype: :class:`python: datetime.timedelta`
53
+ :rtype: datetime.timedelta
53
54
"""
54
55
55
56
if date is None :
@@ -60,17 +61,18 @@ def get_utc_offset(tz, date=None):
60
61
61
62
return date .replace (tzinfo = pytz .utc ).astimezone (tz ).utcoffset ()
62
63
63
- def get_timezone (tz , date = None ):
64
+
65
+ def get_timezone (tz : str , date : datetime .datetime = None ) -> datetime .timedelta :
64
66
"""
65
67
Returns a localized :class:`pytz.timezone` object for the given date.
66
68
If ``date`` is ``None`` then the current date is used.
67
69
68
70
:param tz: A string representing a pytz timezone
69
- :type tz:
71
+ :type tz: str
70
72
:param date:
71
- :type date: :class:` python:datetime.datetime` or None , optional
73
+ :type date: python:datetime.datetime, optional
72
74
:return:
73
- :rtype: :class:`python: datetime.timedelta`
75
+ :rtype: datetime.timedelta
74
76
"""
75
77
76
78
if date is None :
@@ -80,6 +82,7 @@ def get_timezone(tz, date=None):
80
82
81
83
return pytz .timezone (tz ).localize (d ).tzinfo
82
84
85
+
83
86
def current_tzinfo ():
84
87
"""
85
88
Returns a tzinfo object for the current timezone
@@ -124,7 +127,8 @@ def set_timezone(obj, tzinfo):
124
127
125
128
return obj .replace (tzinfo = tzinfo )
126
129
127
- def utc_timestamp_to_datetime (utc_timestamp , output_tz = None ):
130
+
131
+ def utc_timestamp_to_datetime (utc_timestamp : Union [float , int ], output_tz : datetime .tzinfo = None ) -> datetime .datetime :
128
132
"""
129
133
Convert UTC timestamp (seconds from UNIX epoch) to a :class:`datetime.datetime` object
130
134
@@ -139,10 +143,10 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
139
143
:type utc_timestamp: float, int
140
144
:param output_tz: The timezone to output the datetime object for.
141
145
If None it will be inferred.
142
- :type output_tz: :class:`~python: datetime.tzinfo`
146
+ :type output_tz: datetime.tzinfo
143
147
144
148
:return: The timestamp as a datetime object.
145
- :rtype: :class:` datetime.datetime`
149
+ :rtype: datetime.datetime
146
150
147
151
:raises: :class:`~python:OverflowError` if the timestamp is out of the range
148
152
of values supported by the platform C localtime() or gmtime() functions,
@@ -153,6 +157,7 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
153
157
new_datetime = datetime .datetime .fromtimestamp (utc_timestamp , output_tz )
154
158
return new_datetime .astimezone (output_tz )
155
159
160
+
156
161
# List of months and their 3-character shortcodes.
157
162
months = OrderedDict (
158
163
Jan = "January" ,
@@ -169,7 +174,8 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
169
174
Dec = "December" ,
170
175
)
171
176
172
- def parse_month (month ):
177
+
178
+ def parse_month (month : Union [str , int ]) -> str :
173
179
"""
174
180
Converts an integer or shorthand month into the full month name
175
181
@@ -194,7 +200,8 @@ def parse_month(month):
194
200
else :
195
201
raise ValueError ("Unrecognised month value" )
196
202
197
- def get_month_number (month ):
203
+
204
+ def get_month_number (month : Union [str , int ]) -> int :
198
205
"""
199
206
Returns the number of the given month. If ``month`` is already a
200
207
number between 1 and 12 it will be returned immediately.
@@ -203,7 +210,7 @@ def get_month_number(month):
203
210
:type month: str or int
204
211
205
212
:return: The number of the month
206
- :rtype:
213
+ :rtype: int
207
214
"""
208
215
209
216
if isinstance (month , int ):
@@ -215,7 +222,7 @@ def get_month_number(month):
215
222
month = parse_month (month )
216
223
return list (months .values ()).index (month ) + 1
217
224
218
- def check_date (month , day , leap_year = True ):
225
+ def check_date (month : Union [ str , int ], day : int , leap_year : bool = True ) -> bool :
219
226
"""
220
227
Returns ``True`` if the day number is valid for the given month.
221
228
Note that this will return ``True`` for the 29th Feb. If you don't
@@ -225,8 +232,8 @@ def check_date(month, day, leap_year=True):
225
232
:type month: str, int
226
233
:param day: The day number to test
227
234
:type day: int
228
- :param leap_year: Whether to return ``True`` for 29th Feb
229
- :type leap_year: bool
235
+ :param leap_year: Whether to return ``True`` for 29th Feb. Default ``True``
236
+ :type leap_year: bool, optional
230
237
231
238
:return: ``True`` if the date is valid
232
239
:rtype: bool
0 commit comments