Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 60bba43

Browse filesBrowse 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 00e2c59 commit 60bba43
Copy full SHA for 60bba43

File tree

Expand file treeCollapse file tree

1 file changed

+29
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+29
-3
lines changed

‎Lib/test/datetimetester.py

Copy file name to clipboardExpand all lines: Lib/test/datetimetester.py
+29-3Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,33 @@
5757
NAN = float("nan")
5858

5959

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

@@ -6098,15 +6125,14 @@ def stats(cls, start_year=1):
60986125
def transitions(self):
60996126
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61006127
shift = ti[0] - prev_ti[0]
6101-
# TODO: Remove this use of utcfromtimestamp
6102-
yield datetime.utcfromtimestamp(t), shift
6128+
yield _utcfromtimestamp(datetime, t), shift
61036129

61046130
def nondst_folds(self):
61056131
"""Find all folds with the same value of isdst on both sides of the transition."""
61066132
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61076133
shift = ti[0] - prev_ti[0]
61086134
if shift < ZERO and ti[1] == prev_ti[1]:
6109-
yield datetime.utcfromtimestamp(t), -shift, prev_ti[2], ti[2]
6135+
yield _utcfromtimestamp(datetime, t,), -shift, prev_ti[2], ti[2]
61106136

61116137
@classmethod
61126138
def print_all_nondst_folds(cls, same_abbr=False, start_year=1):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.