3434# stdlib
3535import datetime
3636from collections import OrderedDict
37+ from typing import Union
3738
3839# 3rd party
3940try :
4041 import pytz
4142
42- def get_utc_offset (tz , date = None ):
43+ def get_utc_offset (tz , date : datetime . datetime = None ) -> datetime . timedelta :
4344 """
4445 Returns the offset between UTC and the requested timezone on the given date.
4546 If ``date`` is ``None`` then the current date is used.
4647
47- :param tz: :class:` pytz.timezone` or a string representing the timezone
48+ :param tz: `` pytz.timezone` ` or a string representing the timezone
4849 :type tz:
4950 :param date:
50- :type date: :class:` python:datetime.datetime` or None , optional
51+ :type date: python:datetime.datetime, optional
5152 :return:
52- :rtype: :class:`python: datetime.timedelta`
53+ :rtype: datetime.timedelta
5354 """
5455
5556 if date is None :
@@ -60,17 +61,18 @@ def get_utc_offset(tz, date=None):
6061
6162 return date .replace (tzinfo = pytz .utc ).astimezone (tz ).utcoffset ()
6263
63- def get_timezone (tz , date = None ):
64+
65+ def get_timezone (tz : str , date : datetime .datetime = None ) -> datetime .timedelta :
6466 """
6567 Returns a localized :class:`pytz.timezone` object for the given date.
6668 If ``date`` is ``None`` then the current date is used.
6769
6870 :param tz: A string representing a pytz timezone
69- :type tz:
71+ :type tz: str
7072 :param date:
71- :type date: :class:` python:datetime.datetime` or None , optional
73+ :type date: python:datetime.datetime, optional
7274 :return:
73- :rtype: :class:`python: datetime.timedelta`
75+ :rtype: datetime.timedelta
7476 """
7577
7678 if date is None :
@@ -80,6 +82,7 @@ def get_timezone(tz, date=None):
8082
8183 return pytz .timezone (tz ).localize (d ).tzinfo
8284
85+
8386 def current_tzinfo ():
8487 """
8588 Returns a tzinfo object for the current timezone
@@ -124,7 +127,8 @@ def set_timezone(obj, tzinfo):
124127
125128 return obj .replace (tzinfo = tzinfo )
126129
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 :
128132 """
129133 Convert UTC timestamp (seconds from UNIX epoch) to a :class:`datetime.datetime` object
130134
@@ -139,10 +143,10 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
139143 :type utc_timestamp: float, int
140144 :param output_tz: The timezone to output the datetime object for.
141145 If None it will be inferred.
142- :type output_tz: :class:`~python: datetime.tzinfo`
146+ :type output_tz: datetime.tzinfo
143147
144148 :return: The timestamp as a datetime object.
145- :rtype: :class:` datetime.datetime`
149+ :rtype: datetime.datetime
146150
147151 :raises: :class:`~python:OverflowError` if the timestamp is out of the range
148152 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):
153157 new_datetime = datetime .datetime .fromtimestamp (utc_timestamp , output_tz )
154158 return new_datetime .astimezone (output_tz )
155159
160+
156161 # List of months and their 3-character shortcodes.
157162 months = OrderedDict (
158163 Jan = "January" ,
@@ -169,7 +174,8 @@ def utc_timestamp_to_datetime(utc_timestamp, output_tz=None):
169174 Dec = "December" ,
170175 )
171176
172- def parse_month (month ):
177+
178+ def parse_month (month : Union [str , int ]) -> str :
173179 """
174180 Converts an integer or shorthand month into the full month name
175181
@@ -194,7 +200,8 @@ def parse_month(month):
194200 else :
195201 raise ValueError ("Unrecognised month value" )
196202
197- def get_month_number (month ):
203+
204+ def get_month_number (month : Union [str , int ]) -> int :
198205 """
199206 Returns the number of the given month. If ``month`` is already a
200207 number between 1 and 12 it will be returned immediately.
@@ -203,7 +210,7 @@ def get_month_number(month):
203210 :type month: str or int
204211
205212 :return: The number of the month
206- :rtype:
213+ :rtype: int
207214 """
208215
209216 if isinstance (month , int ):
@@ -215,7 +222,7 @@ def get_month_number(month):
215222 month = parse_month (month )
216223 return list (months .values ()).index (month ) + 1
217224
218- def check_date (month , day , leap_year = True ):
225+ def check_date (month : Union [ str , int ], day : int , leap_year : bool = True ) -> bool :
219226 """
220227 Returns ``True`` if the day number is valid for the given month.
221228 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):
225232 :type month: str, int
226233 :param day: The day number to test
227234 :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
230237
231238 :return: ``True`` if the date is valid
232239 :rtype: bool
0 commit comments