|
57 | 57 | NAN = float("nan")
|
58 | 58 |
|
59 | 59 |
|
| 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 | + |
60 | 87 | #############################################################################
|
61 | 88 | # module tests
|
62 | 89 |
|
@@ -6098,15 +6125,14 @@ def stats(cls, start_year=1):
|
6098 | 6125 | def transitions(self):
|
6099 | 6126 | for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
|
6100 | 6127 | 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 |
6103 | 6129 |
|
6104 | 6130 | def nondst_folds(self):
|
6105 | 6131 | """Find all folds with the same value of isdst on both sides of the transition."""
|
6106 | 6132 | for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
|
6107 | 6133 | shift = ti[0] - prev_ti[0]
|
6108 | 6134 | 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] |
6110 | 6136 |
|
6111 | 6137 | @classmethod
|
6112 | 6138 | def print_all_nondst_folds(cls, same_abbr=False, start_year=1):
|
|
0 commit comments