Skip to content

Commit 568af27

Browse files
[3.15] gh-159706: Fix pure-Python ZoneInfo.fromutc() fold detection for single-transition zones (GH-159828) (#160152)
(cherry picked from commit 1c5b959) Co-authored-by: Lazizbek Ergashev <lazerg2@gmail.com>
1 parent fa795c2 commit 568af27

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎Lib/test/test_zoneinfo/test_zoneinfo.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,26 @@ def test_one_transition(self):
642642
dt_utc_actual = dt_local.astimezone(timezone.utc)
643643
self.assertEqual(dt_utc_actual, dt_utc)
644644

645+
def test_one_transition_fold(self):
646+
GMT = ZoneOffset("-00", ZERO)
647+
N03 = ZoneOffset("-03", -timedelta(hours=3))
648+
649+
transitions = [
650+
ZoneTransition(datetime(1976, 12, 1), GMT, N03),
651+
]
652+
653+
after = "<-03>3"
654+
655+
zf = self.construct_zone(transitions, after)
656+
zi = self.klass.from_file(zf)
657+
658+
dt_utc = datetime(1976, 12, 1, 1, tzinfo=timezone.utc)
659+
dt_local = dt_utc.astimezone(zi)
660+
661+
self.assertEqual(dt_local.fold, 1)
662+
self.assertEqual(dt_local.utcoffset(), N03.utcoffset)
663+
self.assertEqual(dt_local.astimezone(timezone.utc), dt_utc)
664+
645665
def test_one_zone_dst(self):
646666
DST = ZoneOffset("DST", ONE_H, ONE_H)
647667
transitions = [

‎Lib/zoneinfo/_zoneinfo.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def fromutc(self, dt):
144144
if num_trans > 1 and timestamp >= self._trans_utc[1]:
145145
tti_prev, tti = self._ttinfos[idx - 2 : idx]
146146
elif timestamp > self._trans_utc[-1]:
147-
tti_prev = self._ttinfos[-1]
147+
tti_prev = self._tti_before
148148
tti = self._tz_after
149149
else:
150150
tti_prev = self._tti_before
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix the pure-Python :class:`zoneinfo.ZoneInfo` setting ``fold=0`` in
2+
:meth:`~datetime.tzinfo.fromutc` for times in the fold created by the only
3+
transition of a zone with a single transition.

0 commit comments

Comments
 (0)