Skip to content

Commit ea453a1

Browse files
author
Philipp Matthias Hahn
committed
Python: Fix datetime.replace(tzinfo=None) with Python 3.13
Python 3.13 fixed [gh-89039][gh-89039]. Now `datetime.__new__()` is also used for `datetime.replace()`, which is called with `tzinfo=None` to convert a time-zone aware instance to a _naive_ instance. But `__new__()` replaces `tzinfo=None` with `loczlTZ()`, which just re-adds the offset back. This breaks calling `database.update()` on all database records, which use timestamps as a datetime with the wrong timezone offset is used. Add a new singleton LTZ and use that instead of `tzinfo=None` to keep the old behavior to get the local timezone by default. Fixes: MythTV#1179 [gh-89039]: python/cpython#89039
1 parent 0ad9537 commit ea453a1

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

  • mythtv/bindings/python/MythTV/utility

‎mythtv/bindings/python/MythTV/utility/dt.py‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ class datetime( _pydatetime ):
304304
_localtz = None
305305
_utctz = None
306306

307+
LTZ = object()
308+
307309
@classmethod
308310
def localTZ(cls):
309311
global HAVEZONEINFO
@@ -343,8 +345,8 @@ def UTCTZ(cls):
343345
return cls._utctz
344346

345347
@classmethod
346-
def fromDatetime(cls, dt, tzinfo=None, fold=None):
347-
if tzinfo is None:
348+
def fromDatetime(cls, dt, tzinfo=LTZ, fold=None):
349+
if tzinfo is cls.LTZ:
348350
tzinfo = dt.tzinfo
349351
fold = dt.fold
350352
return cls(dt.year, dt.month, dt.day, dt.hour, dt.minute,
@@ -382,7 +384,7 @@ def utcfromtimestamp(cls, timestamp):
382384
return obj.replace(tzinfo=cls.UTCTZ())
383385

384386
@classmethod
385-
def strptime(cls, datestring, format, tzinfo=None):
387+
def strptime(cls, datestring, format, tzinfo=LTZ):
386388
obj = super().strptime(datestring, format)
387389
return cls.fromDatetime(obj, tzinfo)
388390

@@ -393,10 +395,10 @@ def fromnaiveutc(cls, dt):
393395
.astimezone(cls.localTZ())
394396

395397
@classmethod
396-
def frommythtime(cls, mtime, tz=None):
398+
def frommythtime(cls, mtime, tz=LTZ):
397399
if tz in ('UTC', 'Etc/UTC'):
398400
tz = cls.UTCTZ()
399-
elif tz is None:
401+
elif tz is cls.LTZ:
400402
tz = cls.localTZ()
401403
return cls.strptime(str(mtime), '%Y%m%d%H%M%S', tz)
402404

@@ -507,9 +509,9 @@ def duck(cls, t):
507509
raise TypeError("time data '%s' does not match supported formats"%t)
508510

509511
def __new__(cls, year, month, day, hour=None, minute=None, second=None,
510-
microsecond=None, tzinfo=None, fold=None):
512+
microsecond=None, tzinfo=LTZ, fold=None):
511513

512-
if tzinfo is None:
514+
if tzinfo is cls.LTZ:
513515
kwargs = {'tzinfo':cls.localTZ()}
514516
else:
515517
kwargs = {'tzinfo':tzinfo}

0 commit comments

Comments
 (0)