Skip to content

Commit

Permalink
chore: better legacy support
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 22, 2024
1 parent 75e2bdf commit d16093a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/netius/base/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import os
import sys
import inspect
import calendar
import datetime
import functools
import itertools
import contextlib
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit d16093a

Please sign in to comment.