From d16093ac9b385be706af37af573f51a18cd99a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= Date: Mon, 22 Apr 2024 15:46:09 +0100 Subject: [PATCH] chore: better legacy support --- src/netius/base/legacy.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/netius/base/legacy.py b/src/netius/base/legacy.py index 00f79770..6140206d 100644 --- a/src/netius/base/legacy.py +++ b/src/netius/base/legacy.py @@ -40,6 +40,8 @@ import os import sys import inspect +import calendar +import datetime import functools import itertools import contextlib @@ -116,6 +118,10 @@ def ctx_absolute(): interpreter is at least Python 3 compliant, this is used to take some of the conversion decision for runtime """ +PYTHON_33 = sys.version_info[0] >= 3 and sys.version_info[1] >= 3 +""" Global variable that defines if the current Python +interpreter is at least Python 3.3 compliant """ + PYTHON_35 = sys.version_info[0] >= 3 and sys.version_info[1] >= 5 """ Global variable that defines if the current Python interpreter is at least Python 3.5 compliant """ @@ -378,6 +384,29 @@ def build_opener(*args, **kwargs): if PYTHON_3: return urllib.request.build_opener(*args, **kwargs) else: return urllib2.build_opener(*args, **kwargs) #@UndefinedVariable +def to_timestamp(date_time): + if PYTHON_33: + return date_time.replace(tzinfo=datetime.timezone.utc).timestamp() + else: + return calendar.timegm(date_time.utctimetuple()) + +def to_datetime(timestamp): + if PYTHON_33: + return datetime.datetime.fromtimestamp( + timestamp, datetime.timezone.utc + ).replace(tzinfo=None) + else: + return datetime.datetime.utcfromtimestamp(timestamp) + +def utcfromtimestamp(timestamp): + return to_datetime(timestamp) + +def utc_now(): + if PYTHON_33: + return datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) + else: + return datetime.datetime.utcnow() + def urlparse(*args, **kwargs): return _urlparse.urlparse(*args, **kwargs)