Skip to content

Commit

Permalink
util: Handle UTC wrapping on old Python versions
Browse files Browse the repository at this point in the history
Old Python versions don't define datetime.UTC, so conditionally import it and
just use the old datetime.utcnow() and datetime.utcfromtimestamp() functions on
those Python versions.

Signed-off-by: Toke Høiland-Jørgensen <[email protected]>
  • Loading branch information
tohojo committed Sep 10, 2024
1 parent 574dcb5 commit 609b39a
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion flent/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@

from copy import copy
from calendar import timegm
from datetime import datetime, timedelta, UTC
from datetime import datetime, timedelta
from math import log10, exp, sqrt

try:
from datetime import UTC
except ImportError:
UTC = None

from flent.loggers import get_logger

MULTIHOST_SEP = '//'
Expand Down Expand Up @@ -68,9 +73,13 @@ def classname(s, suffix=''):
return uscore_to_camel(s) + suffix

def utcnow():
if UTC is None:
return datetime.utcnow()
return datetime.now(UTC).replace(tzinfo=None)

def utcfromtimestamp(ts):
if UTC is None:
return datetime.utcfromtimestamp(ts)
return datetime.fromtimestamp(ts, UTC).replace(tzinfo=None)

def format_date(dt, fmt="%Y-%m-%dT%H:%M:%S.%f", utc=False):
Expand Down

0 comments on commit 609b39a

Please sign in to comment.