From 33ae14c6ca3731520d433b2cfb29bf33b02688b8 Mon Sep 17 00:00:00 2001 From: ltbringer Date: Fri, 4 Feb 2022 00:20:25 +0530 Subject: [PATCH] add: datetime utility functions. --- dialogy/utils/datetime.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 dialogy/utils/datetime.py diff --git a/dialogy/utils/datetime.py b/dialogy/utils/datetime.py new file mode 100644 index 00000000..f0e2630b --- /dev/null +++ b/dialogy/utils/datetime.py @@ -0,0 +1,53 @@ +import math +from datetime import datetime +from typing import Callable + +import pytz + + +def make_unix_ts(tz: str = "UTC") -> Callable[[str], int]: + """ + Convert date in ISO 8601 format to unix ms timestamp. + + :param tz: A timezone string, defaults to "UTC" + :type tz: Optional[str], optional + :return: A callable that converts a date in ISO 8601 format to unix ms timestamp. + :rtype: Callable[[str], int] + """ + + def make_tz_aware(date_string: str) -> int: + """ + Convert date in ISO 8601 format to unix ms timestamp. + + :param date_string: A date in ISO 8601 format. + :type date_string: str + :return: A unix timestamp (13 digit). + :rtype: int + """ + if isinstance(date_string, int): + return date_string + dt = datetime.fromisoformat(date_string) + if dt.tzinfo is None and tz not in pytz.all_timezones: + raise ValueError(f"Invalid timezone {tz} pick from:\n{pytz.all_timezones}") + + if dt.tzinfo is None: + dt = dt.replace(tzinfo=pytz.timezone(tz)) + return int(dt.timestamp() * 1000) + + return make_tz_aware + + +def is_unix_ts(ts: int) -> bool: + """ + Check if the input is a unix timestamp. + + :param ts: A unix timestamp (13-digit). + :type ts: int + :return: True if :code:`ts` is a unix timestamp, else False. + :rtype: bool + """ + try: + datetime.fromtimestamp(ts / 1000) + return True + except ValueError: + return False