MCPcopy Index your code
hub / github.com/python/cpython / construct_zone

Method construct_zone

Lib/test/test_zoneinfo/test_zoneinfo.py:858–974  ·  view source on GitHub ↗
(self, transitions, after=None, version=3)

Source from the content-addressed store, hash-verified

856 self.assertEqual(t.dst(), UTC.dst)
857
858 def construct_zone(self, transitions, after=None, version=3):
859 # These are not used for anything, so we're not going to include
860 # them for now.
861 isutc = []
862 isstd = []
863 leap_seconds = []
864
865 offset_lists = [[], []]
866 trans_times_lists = [[], []]
867 trans_idx_lists = [[], []]
868
869 v1_range = (-(2 ** 31), 2 ** 31)
870 v2_range = (-(2 ** 63), 2 ** 63)
871 ranges = [v1_range, v2_range]
872
873 def zt_as_tuple(zt):
874 # zt may be a tuple (timestamp, offset_before, offset_after) or
875 # a ZoneTransition object — this is to allow the timestamp to be
876 # values that are outside the valid range for datetimes but still
877 # valid 64-bit timestamps.
878 if isinstance(zt, tuple):
879 return zt
880
881 if zt.transition:
882 trans_time = int(zt.transition_utc.timestamp())
883 else:
884 trans_time = None
885
886 return (trans_time, zt.offset_before, zt.offset_after)
887
888 transitions = sorted(map(zt_as_tuple, transitions), key=lambda x: x[0])
889
890 for zt in transitions:
891 trans_time, offset_before, offset_after = zt
892
893 for v, (dt_min, dt_max) in enumerate(ranges):
894 offsets = offset_lists[v]
895 trans_times = trans_times_lists[v]
896 trans_idx = trans_idx_lists[v]
897
898 if trans_time is not None and not (
899 dt_min <= trans_time <= dt_max
900 ):
901 continue
902
903 if offset_before not in offsets:
904 offsets.append(offset_before)
905
906 if offset_after not in offsets:
907 offsets.append(offset_after)
908
909 if trans_time is not None:
910 trans_times.append(trans_time)
911 trans_idx.append(offsets.index(offset_after))
912
913 isutcnt = len(isutc)
914 isstdcnt = len(isstd)
915 leapcnt = len(leap_seconds)

Calls 8

writeMethod · 0.95
seekMethod · 0.95
enumerateFunction · 0.85
total_secondsMethod · 0.80
appendMethod · 0.45
indexMethod · 0.45
encodeMethod · 0.45
packMethod · 0.45

Tested by

no test coverage detected