Builds a TZif file. ``leap_seconds`` is a sequence of ``(timestamp, correction)`` pairs, and ``indicators`` adds standard/wall and UT/local indicators for every local time type. Neither affects what the file means to ``tzfile``, which skips over them, but they change the layout of t
(
transitions, after=None, version=3, leap_seconds=(), indicators=False
)
| 118 | |
| 119 | |
| 120 | def construct_zone( |
| 121 | transitions, after=None, version=3, leap_seconds=(), indicators=False |
| 122 | ): |
| 123 | """Builds a TZif file. |
| 124 | |
| 125 | ``leap_seconds`` is a sequence of ``(timestamp, correction)`` pairs, and |
| 126 | ``indicators`` adds standard/wall and UT/local indicators for every local |
| 127 | time type. Neither affects what the file means to ``tzfile``, which skips |
| 128 | over them, but they change the layout of the file. |
| 129 | """ |
| 130 | offset_lists = [[], []] |
| 131 | trans_times_lists = [[], []] |
| 132 | trans_idx_lists = [[], []] |
| 133 | |
| 134 | v1_range = (-(2**31), 2**31) |
| 135 | v2_range = (-(2**63), 2**63) |
| 136 | ranges = [v1_range, v2_range] |
| 137 | |
| 138 | def zt_as_tuple(zt): |
| 139 | # zt may be a tuple (timestamp, offset_before, offset_after) or |
| 140 | # a ZoneTransition object -- this is to allow the timestamp to be |
| 141 | # values that are outside the valid range for datetimes but still |
| 142 | # valid 64-bit timestamps. |
| 143 | if isinstance(zt, tuple): |
| 144 | return zt |
| 145 | |
| 146 | if zt.transition: |
| 147 | trans_time = int(datetime.timestamp(zt.transition_utc)) |
| 148 | else: |
| 149 | trans_time = None |
| 150 | |
| 151 | return (trans_time, zt.offset_before, zt.offset_after) |
| 152 | |
| 153 | transitions = sorted(map(zt_as_tuple, transitions), key=lambda x: x[0]) |
| 154 | |
| 155 | for zt in transitions: |
| 156 | trans_time, offset_before, offset_after = zt |
| 157 | |
| 158 | for v, (dt_min, dt_max) in enumerate(ranges): |
| 159 | offsets = offset_lists[v] |
| 160 | trans_times = trans_times_lists[v] |
| 161 | trans_idx = trans_idx_lists[v] |
| 162 | |
| 163 | if trans_time is not None and not (dt_min <= trans_time <= dt_max): |
| 164 | continue |
| 165 | |
| 166 | if offset_before not in offsets: |
| 167 | offsets.append(offset_before) |
| 168 | |
| 169 | if offset_after not in offsets: |
| 170 | offsets.append(offset_after) |
| 171 | |
| 172 | if trans_time is not None: |
| 173 | trans_times.append(trans_time) |
| 174 | trans_idx.append(offsets.index(offset_after)) |
| 175 | |
| 176 | zonefile = io.BytesIO() |
| 177 |
no test coverage detected