Skip to content

Commit 6a94631

Browse files
committed
Remove last use of utcfromtimestamp
There might be easier ways to do this in the future, or we may be able to avoid it entirely by switching to `ZoneInfo` for these tests, but this will remove the last valid use of `utcfromtimetamp` that I see.
1 parent f186557 commit 6a94631

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

Lib/test/datetimetester.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
33
See https://www.zope.dev/Members/fdrake/DateTimeUncyclo/TestCases
44
"""
5-
import io
6-
import itertools
75
import bisect
86
import copy
97
import decimal
10-
import sys
8+
import io
9+
import itertools
10+
import math
1111
import os
1212
import pickle
1313
import random
1414
import re
1515
import struct
16+
import sys
1617
import unittest
1718

1819
from array import array
@@ -57,6 +58,28 @@
5758
NAN = float("nan")
5859

5960

61+
def _utcfromtimestamp(klass, ts):
62+
"""Simple re-implementation of datetime.utcfromtimestamp.
63+
64+
utcfromtimestamp is deprecated because it returns a naïve datetime object
65+
despite being aware that it is UTC. This sort of deliberately wrong object
66+
happens to be useful when calculating transition times from a TZif file,
67+
so this is a re-implementation of that.
68+
"""
69+
frac, ts = math.modf(ts)
70+
71+
us = round(frac * 1e6)
72+
if us >= 1000000:
73+
ts += 1
74+
us -= 1000000
75+
elif us < 0:
76+
ts -= 1
77+
us += 1000000
78+
79+
y, m, d, hh, mm, ss, *_ = _time.gmtime(ts)
80+
81+
return klass(y, m, d, hh, mm, ss, us)
82+
6083
#############################################################################
6184
# module tests
6285

@@ -6098,15 +6121,14 @@ def stats(cls, start_year=1):
60986121
def transitions(self):
60996122
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61006123
shift = ti[0] - prev_ti[0]
6101-
# TODO: Remove this use of utcfromtimestamp
6102-
yield datetime.utcfromtimestamp(t), shift
6124+
yield _utcfromtimestamp(datetime, t), shift
61036125

61046126
def nondst_folds(self):
61056127
"""Find all folds with the same value of isdst on both sides of the transition."""
61066128
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61076129
shift = ti[0] - prev_ti[0]
61086130
if shift < ZERO and ti[1] == prev_ti[1]:
6109-
yield datetime.utcfromtimestamp(t), -shift, prev_ti[2], ti[2]
6131+
yield _utcfromtimestamp(datetime, t,), -shift, prev_ti[2], ti[2]
61106132

61116133
@classmethod
61126134
def print_all_nondst_folds(cls, same_abbr=False, start_year=1):

0 commit comments

Comments
 (0)