|
2 | 2 |
|
3 | 3 | See https://www.zope.dev/Members/fdrake/DateTimeUncyclo/TestCases
|
4 | 4 | """
|
5 |
| -import io |
6 |
| -import itertools |
7 | 5 | import bisect
|
8 | 6 | import copy
|
9 | 7 | import decimal
|
10 |
| -import sys |
| 8 | +import io |
| 9 | +import itertools |
| 10 | +import math |
11 | 11 | import os
|
12 | 12 | import pickle
|
13 | 13 | import random
|
14 | 14 | import re
|
15 | 15 | import struct
|
| 16 | +import sys |
16 | 17 | import unittest
|
17 | 18 |
|
18 | 19 | from array import array
|
|
57 | 58 | NAN = float("nan")
|
58 | 59 |
|
59 | 60 |
|
| 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 | + |
60 | 83 | #############################################################################
|
61 | 84 | # module tests
|
62 | 85 |
|
@@ -6098,15 +6121,14 @@ def stats(cls, start_year=1):
|
6098 | 6121 | def transitions(self):
|
6099 | 6122 | for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
|
6100 | 6123 | 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 |
6103 | 6125 |
|
6104 | 6126 | def nondst_folds(self):
|
6105 | 6127 | """Find all folds with the same value of isdst on both sides of the transition."""
|
6106 | 6128 | for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
|
6107 | 6129 | shift = ti[0] - prev_ti[0]
|
6108 | 6130 | 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] |
6110 | 6132 |
|
6111 | 6133 | @classmethod
|
6112 | 6134 | def print_all_nondst_folds(cls, same_abbr=False, start_year=1):
|
|
0 commit comments