Skip to content

Commit

Permalink
Add compatible methods to TimestampField for 3.12
Browse files Browse the repository at this point in the history
Refs #2700
  • Loading branch information
coleifer committed Nov 21, 2023
1 parent eaf024f commit 6286684
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions peewee.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ def reraise(tp, value, tb=None):
raise value.with_traceback(tb)
raise value

# Other compat issues.
if sys.version_info < (3, 12):
utcfromtimestamp = datetime.datetime.utcfromtimestamp
utcnow = datetime.datetime.utcnow
else:
def utcfromtimestamp(ts):
return (datetime.datetime
.fromtimestamp(ts, tz=datetime.timezone.utc)
.replace(tzinfo=None))
def utcnow():
return (datetime.datetime
.now(datetime.timezone.utc)
.replace(tzinfo=None))


if sqlite3:
sqlite3.register_adapter(decimal.Decimal, str)
Expand Down Expand Up @@ -5348,7 +5362,7 @@ def __init__(self, *args, **kwargs):
self.ticks_to_microsecond = 1000000 // self.resolution

self.utc = kwargs.pop('utc', False) or False
dflt = datetime.datetime.utcnow if self.utc else datetime.datetime.now
dflt = utcnow if self.utc else datetime.datetime.now
kwargs.setdefault('default', dflt)
super(TimestampField, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -5400,7 +5414,7 @@ def python_value(self, value):
microseconds = 0

if self.utc:
value = datetime.datetime.utcfromtimestamp(value)
value = utcfromtimestamp(value)
else:
value = datetime.datetime.fromtimestamp(value)

Expand Down

0 comments on commit 6286684

Please sign in to comment.