| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import base64 |
| 4 |
import contextlib |
| 5 |
import dataclasses |
| 6 |
import importlib.metadata |
| 7 |
import io |
| 8 |
import json |
| 9 |
import os |
| 10 |
import pathlib |
| 11 |
import pickle |
| 12 |
import re |
| 13 |
import shutil |
| 14 |
import struct |
| 15 |
import tempfile |
| 16 |
import unittest |
| 17 |
from datetime import date, datetime, time, timedelta, timezone |
| 18 |
from functools import cached_property |
| 19 |
|
| 20 |
from test.support import MISSING_C_DOCSTRINGS |
| 21 |
from test.support.os_helper import EnvironmentVarGuard, FakePath |
| 22 |
from test.test_zoneinfo import _support as test_support |
| 23 |
from test.test_zoneinfo._support import TZPATH_TEST_LOCK, ZoneInfoTestBase |
| 24 |
from test.support.import_helper import import_module, CleanImport |
| 25 |
from test.support.script_helper import assert_python_ok |
| 26 |
|
| 27 |
lzma = import_module('lzma') |
| 28 |
py_zoneinfo, c_zoneinfo = test_support.get_modules() |
| 29 |
|
| 30 |
try: |
| 31 |
importlib.metadata.metadata("tzdata") |
| 32 |
HAS_TZDATA_PKG = True |
| 33 |
except importlib.metadata.PackageNotFoundError: |
| 34 |
HAS_TZDATA_PKG = False |
| 35 |
|
| 36 |
ZONEINFO_DATA = None |
| 37 |
ZONEINFO_DATA_V1 = None |
| 38 |
TEMP_DIR = None |
| 39 |
DATA_DIR = pathlib.Path(__file__).parent / "data" |
| 40 |
ZONEINFO_JSON = DATA_DIR / "zoneinfo_data.json" |
| 41 |
DRIVE = os.path.splitdrive('x:')[0] |
| 42 |
|
| 43 |
# Useful constants |
| 44 |
ZERO = timedelta(0) |
| 45 |
ONE_H = timedelta(hours=1) |
| 46 |
|
| 47 |
|
| 48 |
def setUpModule(): |
| 49 |
global TEMP_DIR |
| 50 |
global ZONEINFO_DATA |
| 51 |
global ZONEINFO_DATA_V1 |
| 52 |
|
| 53 |
TEMP_DIR = pathlib.Path(tempfile.mkdtemp(prefix="zoneinfo")) |
| 54 |
ZONEINFO_DATA = ZoneInfoData(ZONEINFO_JSON, TEMP_DIR / "v2") |
| 55 |
ZONEINFO_DATA_V1 = ZoneInfoData(ZONEINFO_JSON, TEMP_DIR / "v1", v1=True) |
| 56 |
|
| 57 |
|
| 58 |
def tearDownModule(): |
| 59 |
shutil.rmtree(TEMP_DIR) |
| 60 |
|
| 61 |
|
| 62 |
class CustomError(Exception): |
| 63 |
pass |
| 64 |
|
| 65 |
|
| 66 |
class TzPathUserMixin: |
| 67 |
""" |
| 68 |
Adds a setUp() and tearDown() to make TZPATH manipulations thread-safe. |
| 69 |
|
| 70 |
Any tests that require manipulation of the TZPATH global are necessarily |
| 71 |
thread unsafe, so we will acquire a lock and reset the TZPATH variable |
| 72 |
to the default state before each test and release the lock after the test |
| 73 |
is through. |
| 74 |
""" |
| 75 |
|
| 76 |
@property |
| 77 |
def tzpath(self): # pragma: nocover |
| 78 |
return None |
| 79 |
|
| 80 |
@property |
| 81 |
def block_tzdata(self): |
| 82 |
return True |
| 83 |
|
| 84 |
def setUp(self): |
| 85 |
with contextlib.ExitStack() as stack: |
| 86 |
stack.enter_context( |
| 87 |
self.tzpath_context( |
| 88 |
self.tzpath, |
| 89 |
block_tzdata=self.block_tzdata, |
| 90 |
lock=TZPATH_TEST_LOCK, |
| 91 |
) |
| 92 |
) |
| 93 |
self.addCleanup(stack.pop_all().close) |
| 94 |
|
| 95 |
super().setUp() |
| 96 |
|
| 97 |
|
| 98 |
class DatetimeSubclassMixin: |
| 99 |
""" |
| 100 |
Replaces all ZoneTransition transition dates with a datetime subclass. |
| 101 |
""" |
| 102 |
|
| 103 |
class DatetimeSubclass(datetime): |
| 104 |
@classmethod |
| 105 |
def from_datetime(cls, dt): |
| 106 |
return cls( |
| 107 |
dt.year, |
| 108 |
dt.month, |
| 109 |
dt.day, |
| 110 |
dt.hour, |
| 111 |
dt.minute, |
| 112 |
dt.second, |
| 113 |
dt.microsecond, |
| 114 |
tzinfo=dt.tzinfo, |
| 115 |
fold=dt.fold, |
| 116 |
) |
| 117 |
|
| 118 |
def load_transition_examples(self, key): |
| 119 |
transition_examples = super().load_transition_examples(key) |
| 120 |
for zt in transition_examples: |
| 121 |
dt = zt.transition |
| 122 |
new_dt = self.DatetimeSubclass.from_datetime(dt) |
| 123 |
new_zt = dataclasses.replace(zt, transition=new_dt) |
| 124 |
yield new_zt |
| 125 |
|
| 126 |
|
| 127 |
class ZoneInfoTest(TzPathUserMixin, ZoneInfoTestBase): |
| 128 |
module = py_zoneinfo |
| 129 |
class_name = "ZoneInfo" |
| 130 |
|
| 131 |
def setUp(self): |
| 132 |
super().setUp() |
| 133 |
|
| 134 |
# This is necessary because various subclasses pull from different |
| 135 |
# data sources (e.g. tzdata, V1 files, etc). |
| 136 |
self.klass.clear_cache() |
| 137 |
|
| 138 |
@property |
| 139 |
def zoneinfo_data(self): |
| 140 |
return ZONEINFO_DATA |
| 141 |
|
| 142 |
@property |
| 143 |
def tzpath(self): |
| 144 |
return [self.zoneinfo_data.tzpath] |
| 145 |
|
| 146 |
def zone_from_key(self, key): |
| 147 |
return self.klass(key) |
| 148 |
|
| 149 |
def zones(self): |
| 150 |
return ZoneDumpData.transition_keys() |
| 151 |
|
| 152 |
def fixed_offset_zones(self): |
| 153 |
return ZoneDumpData.fixed_offset_zones() |
| 154 |
|
| 155 |
def load_transition_examples(self, key): |
| 156 |
return ZoneDumpData.load_transition_examples(key) |
| 157 |
|
| 158 |
def test_str(self): |
| 159 |
# Zones constructed with a key must have str(zone) == key |
| 160 |
for key in self.zones(): |
| 161 |
with self.subTest(key): |
| 162 |
zi = self.zone_from_key(key) |
| 163 |
|
| 164 |
self.assertEqual(str(zi), key) |
| 165 |
|
| 166 |
# Zones with no key constructed should have str(zone) == repr(zone) |
| 167 |
file_key = self.zoneinfo_data.keys[0] |
| 168 |
file_path = self.zoneinfo_data.path_from_key(file_key) |
| 169 |
|
| 170 |
with open(file_path, "rb") as f: |
| 171 |
with self.subTest(test_name="Repr test", path=file_path): |
| 172 |
zi_ff = self.klass.from_file(f) |
| 173 |
self.assertEqual(str(zi_ff), repr(zi_ff)) |
| 174 |
|
| 175 |
def test_repr(self): |
| 176 |
# The repr is not guaranteed, but I think we can insist that it at |
| 177 |
# least contain the name of the class. |
| 178 |
key = next(iter(self.zones())) |
| 179 |
|
| 180 |
zi = self.klass(key) |
| 181 |
class_name = self.class_name |
| 182 |
with self.subTest(name="from key"): |
| 183 |
self.assertRegex(repr(zi), class_name) |
| 184 |
|
| 185 |
file_key = self.zoneinfo_data.keys[0] |
| 186 |
file_path = self.zoneinfo_data.path_from_key(file_key) |
| 187 |
with open(file_path, "rb") as f: |
| 188 |
zi_ff = self.klass.from_file(f, key=file_key) |
| 189 |
|
| 190 |
with self.subTest(name="from file with key"): |
| 191 |
self.assertRegex(repr(zi_ff), class_name) |
| 192 |
|
| 193 |
with open(file_path, "rb") as f: |
| 194 |
zi_ff_nk = self.klass.from_file(f) |
| 195 |
|
| 196 |
with self.subTest(name="from file without key"): |
| 197 |
self.assertRegex(repr(zi_ff_nk), class_name) |
| 198 |
|
| 199 |
def test_key_attribute(self): |
| 200 |
key = next(iter(self.zones())) |
| 201 |
|
| 202 |
def from_file_nokey(key): |
| 203 |
with open(self.zoneinfo_data.path_from_key(key), "rb") as f: |
| 204 |
return self.klass.from_file(f) |
| 205 |
|
| 206 |
constructors = ( |
| 207 |
("Primary constructor", self.klass, key), |
| 208 |
("no_cache", self.klass.no_cache, key), |
| 209 |
("from_file", from_file_nokey, None), |
| 210 |
) |
| 211 |
|
| 212 |
for msg, constructor, expected in constructors: |
| 213 |
zi = constructor(key) |
| 214 |
|
| 215 |
# Ensure that the key attribute is set to the input to ``key`` |
| 216 |
with self.subTest(msg): |
| 217 |
self.assertEqual(zi.key, expected) |
| 218 |
|
| 219 |
# Ensure that the key attribute is read-only |
| 220 |
with self.subTest(f"{msg}: readonly"): |
| 221 |
with self.assertRaises(AttributeError): |
| 222 |
zi.key = "Some/Value" |
| 223 |
|
| 224 |
def test_bad_keys(self): |
| 225 |
bad_keys = [ |
| 226 |
"Eurasia/Badzone", # Plausible but does not exist |
| 227 |
"BZQ", |
| 228 |
"America.Los_Angeles", |
| 229 |
"🇨🇦", # Non-ascii |
| 230 |
"America/New\ud800York", # Contains surrogate character |
| 231 |
"Europe", # Is a directory, see issue gh-85702 |
| 232 |
] |
| 233 |
|
| 234 |
for bad_key in bad_keys: |
| 235 |
with self.assertRaises(self.module.ZoneInfoNotFoundError): |
| 236 |
self.klass(bad_key) |
| 237 |
|
| 238 |
def test_bad_keys_paths(self): |
| 239 |
bad_keys = [ |
| 240 |
"/America/Los_Angeles", # Absolute path |
| 241 |
"America/Los_Angeles/", # Trailing slash - not normalized |
| 242 |
"../zoneinfo/America/Los_Angeles", # Traverses above TZPATH |
| 243 |
"America/../America/Los_Angeles", # Not normalized |
| 244 |
"America/./Los_Angeles", |
| 245 |
] |
| 246 |
|
| 247 |
for bad_key in bad_keys: |
| 248 |
with self.assertRaises(ValueError): |
| 249 |
self.klass(bad_key) |
| 250 |
|
| 251 |
def test_bad_zones(self): |
| 252 |
bad_zones = [ |
| 253 |
b"", # Empty file |
| 254 |
b"AAAA3" + b" " * 15, # Bad magic |
| 255 |
# Truncated V2 file (should not loop indefinitely) |
| 256 |
b"TZif2" + (b"\x00" * 39) + b"TZif2" + (b"\x00" * 39) + b"\n" + b"Part", |
| 257 |
] |
| 258 |
|
| 259 |
for bad_zone in bad_zones: |
| 260 |
fobj = io.BytesIO(bad_zone) |
| 261 |
with self.assertRaises(ValueError): |
| 262 |
self.klass.from_file(fobj) |
| 263 |
|
| 264 |
def test_fromutc_errors(self): |
| 265 |
key = next(iter(self.zones())) |
| 266 |
zone = self.zone_from_key(key) |
| 267 |
|
| 268 |
bad_values = [ |
| 269 |
(datetime(2019, 1, 1, tzinfo=timezone.utc), ValueError), |
| 270 |
(datetime(2019, 1, 1), ValueError), |
| 271 |
(date(2019, 1, 1), TypeError), |
| 272 |
(time(0), TypeError), |
| 273 |
(0, TypeError), |
| 274 |
("2019-01-01", TypeError), |
| 275 |
] |
| 276 |
|
| 277 |
for val, exc_type in bad_values: |
| 278 |
with self.subTest(val=val): |
| 279 |
with self.assertRaises(exc_type): |
| 280 |
zone.fromutc(val) |
| 281 |
|
| 282 |
def test_utc(self): |
| 283 |
zi = self.klass("UTC") |
| 284 |
dt = datetime(2020, 1, 1, tzinfo=zi) |
| 285 |
|
| 286 |
self.assertEqual(dt.utcoffset(), ZERO) |
| 287 |
self.assertEqual(dt.dst(), ZERO) |
| 288 |
self.assertEqual(dt.tzname(), "UTC") |
| 289 |
|
| 290 |
def test_unambiguous(self): |
| 291 |
test_cases = [] |
| 292 |
for key in self.zones(): |
| 293 |
for zone_transition in self.load_transition_examples(key): |
| 294 |
test_cases.append( |
| 295 |
( |
| 296 |
key, |
| 297 |
zone_transition.transition - timedelta(days=2), |
| 298 |
zone_transition.offset_before, |
| 299 |
) |
| 300 |
) |
| 301 |
|
| 302 |
test_cases.append( |
| 303 |
( |
| 304 |
key, |
| 305 |
zone_transition.transition + timedelta(days=2), |
| 306 |
zone_transition.offset_after, |
| 307 |
) |
| 308 |
) |
| 309 |
|
| 310 |
for key, dt, offset in test_cases: |
| 311 |
with self.subTest(key=key, dt=dt, offset=offset): |
| 312 |
tzi = self.zone_from_key(key) |
| 313 |
dt = dt.replace(tzinfo=tzi) |
| 314 |
|
| 315 |
self.assertEqual(dt.tzname(), offset.tzname, dt) |
| 316 |
self.assertEqual(dt.utcoffset(), offset.utcoffset, dt) |
| 317 |
self.assertEqual(dt.dst(), offset.dst, dt) |
| 318 |
|
| 319 |
def test_folds_and_gaps(self): |
| 320 |
test_cases = [] |
| 321 |
for key in self.zones(): |
| 322 |
tests = {"folds": [], "gaps": []} |
| 323 |
for zt in self.load_transition_examples(key): |
| 324 |
if zt.fold: |
| 325 |
test_group = tests["folds"] |
| 326 |
elif zt.gap: |
| 327 |
test_group = tests["gaps"] |
| 328 |
else: |
| 329 |
# Assign a random variable here to disable the peephole |
| 330 |
# optimizer so that coverage can see this line. |
| 331 |
# See bpo-2506 for more information. |
| 332 |
no_peephole_opt = None |
| 333 |
continue |
| 334 |
|
| 335 |
# Cases are of the form key, dt, fold, offset |
| 336 |
dt = zt.anomaly_start - timedelta(seconds=1) |
| 337 |
test_group.append((dt, 0, zt.offset_before)) |
| 338 |
test_group.append((dt, 1, zt.offset_before)) |
| 339 |
|
| 340 |
dt = zt.anomaly_start |
| 341 |
test_group.append((dt, 0, zt.offset_before)) |
| 342 |
test_group.append((dt, 1, zt.offset_after)) |
| 343 |
|
| 344 |
dt = zt.anomaly_start + timedelta(seconds=1) |
| 345 |
test_group.append((dt, 0, zt.offset_before)) |
| 346 |
test_group.append((dt, 1, zt.offset_after)) |
| 347 |
|
| 348 |
dt = zt.anomaly_end - timedelta(seconds=1) |
| 349 |
test_group.append((dt, 0, zt.offset_before)) |
| 350 |
test_group.append((dt, 1, zt.offset_after)) |
| 351 |
|
| 352 |
dt = zt.anomaly_end |
| 353 |
test_group.append((dt, 0, zt.offset_after)) |
| 354 |
test_group.append((dt, 1, zt.offset_after)) |
| 355 |
|
| 356 |
dt = zt.anomaly_end + timedelta(seconds=1) |
| 357 |
test_group.append((dt, 0, zt.offset_after)) |
| 358 |
test_group.append((dt, 1, zt.offset_after)) |
| 359 |
|
| 360 |
for grp, test_group in tests.items(): |
| 361 |
test_cases.append(((key, grp), test_group)) |
| 362 |
|
| 363 |
for (key, grp), tests in test_cases: |
| 364 |
with self.subTest(key=key, grp=grp): |
| 365 |
tzi = self.zone_from_key(key) |
| 366 |
|
| 367 |
for dt, fold, offset in tests: |
| 368 |
dt = dt.replace(fold=fold, tzinfo=tzi) |
| 369 |
|
| 370 |
self.assertEqual(dt.tzname(), offset.tzname, dt) |
| 371 |
self.assertEqual(dt.utcoffset(), offset.utcoffset, dt) |
| 372 |
self.assertEqual(dt.dst(), offset.dst, dt) |
| 373 |
|
| 374 |
def test_folds_from_utc(self): |
| 375 |
for key in self.zones(): |
| 376 |
zi = self.zone_from_key(key) |
| 377 |
with self.subTest(key=key): |
| 378 |
for zt in self.load_transition_examples(key): |
| 379 |
if not zt.fold: |
| 380 |
continue |
| 381 |
|
| 382 |
dt_utc = zt.transition_utc |
| 383 |
dt_before_utc = dt_utc - timedelta(seconds=1) |
| 384 |
dt_after_utc = dt_utc + timedelta(seconds=1) |
| 385 |
|
| 386 |
dt_before = dt_before_utc.astimezone(zi) |
| 387 |
self.assertEqual(dt_before.fold, 0, (dt_before, dt_utc)) |
| 388 |
|
| 389 |
dt_after = dt_after_utc.astimezone(zi) |
| 390 |
self.assertEqual(dt_after.fold, 1, (dt_after, dt_utc)) |
| 391 |
|
| 392 |
def test_time_variable_offset(self): |
| 393 |
# self.zones() only ever returns variable-offset zones |
| 394 |
for key in self.zones(): |
| 395 |
zi = self.zone_from_key(key) |
| 396 |
t = time(11, 15, 1, 34471, tzinfo=zi) |
| 397 |
|
| 398 |
with self.subTest(key=key): |
| 399 |
self.assertIs(t.tzname(), None) |
| 400 |
self.assertIs(t.utcoffset(), None) |
| 401 |
self.assertIs(t.dst(), None) |
| 402 |
|
| 403 |
def test_time_fixed_offset(self): |
| 404 |
for key, offset in self.fixed_offset_zones(): |
| 405 |
zi = self.zone_from_key(key) |
| 406 |
|
| 407 |
t = time(11, 15, 1, 34471, tzinfo=zi) |
| 408 |
|
| 409 |
with self.subTest(key=key): |
| 410 |
self.assertEqual(t.tzname(), offset.tzname) |
| 411 |
self.assertEqual(t.utcoffset(), offset.utcoffset) |
| 412 |
self.assertEqual(t.dst(), offset.dst) |
| 413 |
|
| 414 |
def test_cache_exception(self): |
| 415 |
class Incomparable(str): |
| 416 |
eq_called = False |
| 417 |
def __eq__(self, other): |
| 418 |
self.eq_called = True |
| 419 |
raise CustomError |
| 420 |
__hash__ = str.__hash__ |
| 421 |
|
| 422 |
key = "America/Los_Angeles" |
| 423 |
tz1 = self.klass(key) |
| 424 |
key = Incomparable(key) |
| 425 |
try: |
| 426 |
tz2 = self.klass(key) |
| 427 |
except CustomError: |
| 428 |
self.assertTrue(key.eq_called) |
| 429 |
else: |
| 430 |
self.assertFalse(key.eq_called) |
| 431 |
self.assertIs(tz2, tz1) |
| 432 |
|
| 433 |
|
| 434 |
class CZoneInfoTest(ZoneInfoTest): |
| 435 |
module = c_zoneinfo |
| 436 |
|
| 437 |
@unittest.skipIf(MISSING_C_DOCSTRINGS, |
| 438 |
"Signature information for builtins requires docstrings") |
| 439 |
def test_signatures(self): |
| 440 |
"""Ensure that C module has valid method signatures.""" |
| 441 |
import inspect |
| 442 |
|
| 443 |
must_have_signatures = ( |
| 444 |
self.klass.clear_cache, |
| 445 |
self.klass.no_cache, |
| 446 |
self.klass.from_file, |
| 447 |
) |
| 448 |
for method in must_have_signatures: |
| 449 |
with self.subTest(method=method): |
| 450 |
inspect.Signature.from_callable(method) |
| 451 |
|
| 452 |
def test_fold_mutate(self): |
| 453 |
"""Test that fold isn't mutated when no change is necessary. |
| 454 |
|
| 455 |
The underlying C API is capable of mutating datetime objects, and |
| 456 |
may rely on the fact that addition of a datetime object returns a |
| 457 |
new datetime; this test ensures that the input datetime to fromutc |
| 458 |
is not mutated. |
| 459 |
""" |
| 460 |
|
| 461 |
def to_subclass(dt): |
| 462 |
class SameAddSubclass(type(dt)): |
| 463 |
def __add__(self, other): |
| 464 |
if other == timedelta(0): |
| 465 |
return self |
| 466 |
|
| 467 |
return super().__add__(other) # pragma: nocover |
| 468 |
|
| 469 |
return SameAddSubclass( |
| 470 |
dt.year, |
| 471 |
dt.month, |
| 472 |
dt.day, |
| 473 |
dt.hour, |
| 474 |
dt.minute, |
| 475 |
dt.second, |
| 476 |
dt.microsecond, |
| 477 |
fold=dt.fold, |
| 478 |
tzinfo=dt.tzinfo, |
| 479 |
) |
| 480 |
|
| 481 |
subclass = [False, True] |
| 482 |
|
| 483 |
key = "Europe/London" |
| 484 |
zi = self.zone_from_key(key) |
| 485 |
for zt in self.load_transition_examples(key): |
| 486 |
if zt.fold and zt.offset_after.utcoffset == ZERO: |
| 487 |
example = zt.transition_utc.replace(tzinfo=zi) |
| 488 |
break |
| 489 |
|
| 490 |
for subclass in [False, True]: |
| 491 |
if subclass: |
| 492 |
dt = to_subclass(example) |
| 493 |
else: |
| 494 |
dt = example |
| 495 |
|
| 496 |
with self.subTest(subclass=subclass): |
| 497 |
dt_fromutc = zi.fromutc(dt) |
| 498 |
|
| 499 |
self.assertEqual(dt_fromutc.fold, 1) |
| 500 |
self.assertEqual(dt.fold, 0) |
| 501 |
|
| 502 |
|
| 503 |
class ZoneInfoDatetimeSubclassTest(DatetimeSubclassMixin, ZoneInfoTest): |
| 504 |
pass |
| 505 |
|
| 506 |
|
| 507 |
class CZoneInfoDatetimeSubclassTest(DatetimeSubclassMixin, CZoneInfoTest): |
| 508 |
pass |
| 509 |
|
| 510 |
|
| 511 |
class ZoneInfoSubclassTest(ZoneInfoTest): |
| 512 |
@classmethod |
| 513 |
def setUpClass(cls): |
| 514 |
super().setUpClass() |
| 515 |
|
| 516 |
class ZISubclass(cls.klass): |
| 517 |
pass |
| 518 |
|
| 519 |
cls.class_name = "ZISubclass" |
| 520 |
cls.parent_klass = cls.klass |
| 521 |
cls.klass = ZISubclass |
| 522 |
|
| 523 |
def test_subclass_own_cache(self): |
| 524 |
base_obj = self.parent_klass("Europe/London") |
| 525 |
sub_obj = self.klass("Europe/London") |
| 526 |
|
| 527 |
self.assertIsNot(base_obj, sub_obj) |
| 528 |
self.assertIsInstance(base_obj, self.parent_klass) |
| 529 |
self.assertIsInstance(sub_obj, self.klass) |
| 530 |
|
| 531 |
|
| 532 |
class CZoneInfoSubclassTest(ZoneInfoSubclassTest): |
| 533 |
module = c_zoneinfo |
| 534 |
|
| 535 |
|
| 536 |
class ZoneInfoV1Test(ZoneInfoTest): |
| 537 |
@property |
| 538 |
def zoneinfo_data(self): |
| 539 |
return ZONEINFO_DATA_V1 |
| 540 |
|
| 541 |
def load_transition_examples(self, key): |
| 542 |
# We will discard zdump examples outside the range epoch +/- 2**31, |
| 543 |
# because they are not well-supported in Version 1 files. |
| 544 |
epoch = datetime(1970, 1, 1) |
| 545 |
max_offset_32 = timedelta(seconds=2 ** 31) |
| 546 |
min_dt = epoch - max_offset_32 |
| 547 |
max_dt = epoch + max_offset_32 |
| 548 |
|
| 549 |
for zt in ZoneDumpData.load_transition_examples(key): |
| 550 |
if min_dt <= zt.transition <= max_dt: |
| 551 |
yield zt |
| 552 |
|
| 553 |
|
| 554 |
class CZoneInfoV1Test(ZoneInfoV1Test): |
| 555 |
module = c_zoneinfo |
| 556 |
|
| 557 |
|
| 558 |
@unittest.skipIf( |
| 559 |
not HAS_TZDATA_PKG, "Skipping tzdata-specific tests: tzdata not installed" |
| 560 |
) |
| 561 |
class TZDataTests(ZoneInfoTest): |
| 562 |
""" |
| 563 |
Runs all the ZoneInfoTest tests, but against the tzdata package |
| 564 |
|
| 565 |
NOTE: The ZoneDumpData has frozen test data, but tzdata will update, so |
| 566 |
some of the tests (particularly those related to the far future) may break |
| 567 |
in the event that the time zone policies in the relevant time zones change. |
| 568 |
""" |
| 569 |
|
| 570 |
@property |
| 571 |
def tzpath(self): |
| 572 |
return [] |
| 573 |
|
| 574 |
@property |
| 575 |
def block_tzdata(self): |
| 576 |
return False |
| 577 |
|
| 578 |
def zone_from_key(self, key): |
| 579 |
return self.klass(key=key) |
| 580 |
|
| 581 |
|
| 582 |
@unittest.skipIf( |
| 583 |
not HAS_TZDATA_PKG, "Skipping tzdata-specific tests: tzdata not installed" |
| 584 |
) |
| 585 |
class CTZDataTests(TZDataTests): |
| 586 |
module = c_zoneinfo |
| 587 |
|
| 588 |
|
| 589 |
class WeirdZoneTest(ZoneInfoTestBase): |
| 590 |
module = py_zoneinfo |
| 591 |
|
| 592 |
def test_one_transition(self): |
| 593 |
LMT = ZoneOffset("LMT", -timedelta(hours=6, minutes=31, seconds=2)) |
| 594 |
STD = ZoneOffset("STD", -timedelta(hours=6)) |
| 595 |
|
| 596 |
transitions = [ |
| 597 |
ZoneTransition(datetime(1883, 6, 9, 14), LMT, STD), |
| 598 |
] |
| 599 |
|
| 600 |
after = "STD6" |
| 601 |
|
| 602 |
zf = self.construct_zone(transitions, after) |
| 603 |
zi = self.klass.from_file(zf) |
| 604 |
|
| 605 |
dt0 = datetime(1883, 6, 9, 1, tzinfo=zi) |
| 606 |
dt1 = datetime(1883, 6, 10, 1, tzinfo=zi) |
| 607 |
|
| 608 |
for dt, offset in [(dt0, LMT), (dt1, STD)]: |
| 609 |
with self.subTest(name="local", dt=dt): |
| 610 |
self.assertEqual(dt.tzname(), offset.tzname) |
| 611 |
self.assertEqual(dt.utcoffset(), offset.utcoffset) |
| 612 |
self.assertEqual(dt.dst(), offset.dst) |
| 613 |
|
| 614 |
dts = [ |
| 615 |
( |
| 616 |
datetime(1883, 6, 9, 1, tzinfo=zi), |
| 617 |
datetime(1883, 6, 9, 7, 31, 2, tzinfo=timezone.utc), |
| 618 |
), |
| 619 |
( |
| 620 |
datetime(2010, 4, 1, 12, tzinfo=zi), |
| 621 |
datetime(2010, 4, 1, 18, tzinfo=timezone.utc), |
| 622 |
), |
| 623 |
] |
| 624 |
|
| 625 |
for dt_local, dt_utc in dts: |
| 626 |
with self.subTest(name="fromutc", dt=dt_local): |
| 627 |
dt_actual = dt_utc.astimezone(zi) |
| 628 |
self.assertEqual(dt_actual, dt_local) |
| 629 |
|
| 630 |
dt_utc_actual = dt_local.astimezone(timezone.utc) |
| 631 |
self.assertEqual(dt_utc_actual, dt_utc) |
| 632 |
|
| 633 |
def test_one_zone_dst(self): |
| 634 |
DST = ZoneOffset("DST", ONE_H, ONE_H) |
| 635 |
transitions = [ |
| 636 |
ZoneTransition(datetime(1970, 1, 1), DST, DST), |
| 637 |
] |
| 638 |
|
| 639 |
after = "STD0DST-1,0/0,J365/25" |
| 640 |
|
| 641 |
zf = self.construct_zone(transitions, after) |
| 642 |
zi = self.klass.from_file(zf) |
| 643 |
|
| 644 |
dts = [ |
| 645 |
datetime(1900, 3, 1), |
| 646 |
datetime(1965, 9, 12), |
| 647 |
datetime(1970, 1, 1), |
| 648 |
datetime(2010, 11, 3), |
| 649 |
datetime(2040, 1, 1), |
| 650 |
] |
| 651 |
|
| 652 |
for dt in dts: |
| 653 |
dt = dt.replace(tzinfo=zi) |
| 654 |
with self.subTest(dt=dt): |
| 655 |
self.assertEqual(dt.tzname(), DST.tzname) |
| 656 |
self.assertEqual(dt.utcoffset(), DST.utcoffset) |
| 657 |
self.assertEqual(dt.dst(), DST.dst) |
| 658 |
|
| 659 |
def test_no_tz_str(self): |
| 660 |
STD = ZoneOffset("STD", ONE_H, ZERO) |
| 661 |
DST = ZoneOffset("DST", 2 * ONE_H, ONE_H) |
| 662 |
|
| 663 |
transitions = [] |
| 664 |
for year in range(1996, 2000): |
| 665 |
transitions.append( |
| 666 |
ZoneTransition(datetime(year, 3, 1, 2), STD, DST) |
| 667 |
) |
| 668 |
transitions.append( |
| 669 |
ZoneTransition(datetime(year, 11, 1, 2), DST, STD) |
| 670 |
) |
| 671 |
|
| 672 |
after = "" |
| 673 |
|
| 674 |
zf = self.construct_zone(transitions, after) |
| 675 |
|
| 676 |
# According to RFC 8536, local times after the last transition time |
| 677 |
# with an empty TZ string are unspecified. We will go with "hold the |
| 678 |
# last transition", but the most we should promise is "doesn't crash." |
| 679 |
zi = self.klass.from_file(zf) |
| 680 |
|
| 681 |
cases = [ |
| 682 |
(datetime(1995, 1, 1), STD), |
| 683 |
(datetime(1996, 4, 1), DST), |
| 684 |
(datetime(1996, 11, 2), STD), |
| 685 |
(datetime(2001, 1, 1), STD), |
| 686 |
] |
| 687 |
|
| 688 |
for dt, offset in cases: |
| 689 |
dt = dt.replace(tzinfo=zi) |
| 690 |
with self.subTest(dt=dt): |
| 691 |
self.assertEqual(dt.tzname(), offset.tzname) |
| 692 |
self.assertEqual(dt.utcoffset(), offset.utcoffset) |
| 693 |
self.assertEqual(dt.dst(), offset.dst) |
| 694 |
|
| 695 |
# Test that offsets return None when using a datetime.time |
| 696 |
t = time(0, tzinfo=zi) |
| 697 |
with self.subTest("Testing datetime.time"): |
| 698 |
self.assertIs(t.tzname(), None) |
| 699 |
self.assertIs(t.utcoffset(), None) |
| 700 |
self.assertIs(t.dst(), None) |
| 701 |
|
| 702 |
def test_tz_before_only(self): |
| 703 |
# From RFC 8536 Section 3.2: |
| 704 |
# |
| 705 |
# If there are no transitions, local time for all timestamps is |
| 706 |
# specified by the TZ string in the footer if present and nonempty; |
| 707 |
# otherwise, it is specified by time type 0. |
| 708 |
|
| 709 |
offsets = [ |
| 710 |
ZoneOffset("STD", ZERO, ZERO), |
| 711 |
ZoneOffset("DST", ONE_H, ONE_H), |
| 712 |
] |
| 713 |
|
| 714 |
for offset in offsets: |
| 715 |
# Phantom transition to set time type 0. |
| 716 |
transitions = [ |
| 717 |
ZoneTransition(None, offset, offset), |
| 718 |
] |
| 719 |
|
| 720 |
after = "" |
| 721 |
|
| 722 |
zf = self.construct_zone(transitions, after) |
| 723 |
zi = self.klass.from_file(zf) |
| 724 |
|
| 725 |
dts = [ |
| 726 |
datetime(1900, 1, 1), |
| 727 |
datetime(1970, 1, 1), |
| 728 |
datetime(2000, 1, 1), |
| 729 |
] |
| 730 |
|
| 731 |
for dt in dts: |
| 732 |
dt = dt.replace(tzinfo=zi) |
| 733 |
with self.subTest(offset=offset, dt=dt): |
| 734 |
self.assertEqual(dt.tzname(), offset.tzname) |
| 735 |
self.assertEqual(dt.utcoffset(), offset.utcoffset) |
| 736 |
self.assertEqual(dt.dst(), offset.dst) |
| 737 |
|
| 738 |
def test_empty_zone(self): |
| 739 |
zf = self.construct_zone([], "") |
| 740 |
|
| 741 |
with self.assertRaises(ValueError): |
| 742 |
self.klass.from_file(zf) |
| 743 |
|
| 744 |
def test_invalid_transition_index(self): |
| 745 |
STD = ZoneOffset("STD", ZERO) |
| 746 |
DST = ZoneOffset("DST", ONE_H, ONE_H) |
| 747 |
|
| 748 |
zf = self.construct_zone([ |
| 749 |
ZoneTransition(datetime(2026, 3, 1, 2), STD, DST), |
| 750 |
ZoneTransition(datetime(2026, 11, 1, 2), DST, STD), |
| 751 |
], after="", version=1) |
| 752 |
|
| 753 |
data = bytearray(zf.read()) |
| 754 |
timecnt = struct.unpack_from(">l", data, 32)[0] |
| 755 |
idx_offset = 44 + timecnt * 4 |
| 756 |
data[idx_offset + 1] = 2 # typecnt is 2, so index 2 is OOB |
| 757 |
f = io.BytesIO(bytes(data)) |
| 758 |
|
| 759 |
with self.assertRaises(ValueError): |
| 760 |
self.klass.from_file(f) |
| 761 |
|
| 762 |
def test_transition_lookahead_out_of_bounds(self): |
| 763 |
STD = ZoneOffset("STD", ZERO) |
| 764 |
DST = ZoneOffset("DST", ONE_H, ONE_H) |
| 765 |
EXT = ZoneOffset("EXT", ONE_H) |
| 766 |
|
| 767 |
zf = self.construct_zone([ |
| 768 |
ZoneTransition(datetime(2026, 3, 1), STD, DST), |
| 769 |
ZoneTransition(datetime(2026, 6, 1), DST, EXT), |
| 770 |
ZoneTransition(datetime(2026, 9, 1), EXT, DST), |
| 771 |
], after="") |
| 772 |
|
| 773 |
zi = self.klass.from_file(zf) |
| 774 |
self.assertIsNotNone(zi) |
| 775 |
|
| 776 |
def test_zone_very_large_timestamp(self): |
| 777 |
"""Test when a transition is in the far past or future. |
| 778 |
|
| 779 |
Particularly, this is a concern if something: |
| 780 |
|
| 781 |
1. Attempts to call ``datetime.timestamp`` for a datetime outside |
| 782 |
of ``[datetime.min, datetime.max]``. |
| 783 |
2. Attempts to construct a timedelta outside of |
| 784 |
``[timedelta.min, timedelta.max]``. |
| 785 |
|
| 786 |
This actually occurs "in the wild", as some time zones on Ubuntu (at |
| 787 |
least as of 2020) have an initial transition added at ``-2**58``. |
| 788 |
""" |
| 789 |
|
| 790 |
LMT = ZoneOffset("LMT", timedelta(seconds=-968)) |
| 791 |
GMT = ZoneOffset("GMT", ZERO) |
| 792 |
|
| 793 |
transitions = [ |
| 794 |
(-(1 << 62), LMT, LMT), |
| 795 |
ZoneTransition(datetime(1912, 1, 1), LMT, GMT), |
| 796 |
((1 << 62), GMT, GMT), |
| 797 |
] |
| 798 |
|
| 799 |
after = "GMT0" |
| 800 |
|
| 801 |
zf = self.construct_zone(transitions, after) |
| 802 |
zi = self.klass.from_file(zf, key="Africa/Abidjan") |
| 803 |
|
| 804 |
offset_cases = [ |
| 805 |
(datetime.min, LMT), |
| 806 |
(datetime.max, GMT), |
| 807 |
(datetime(1911, 12, 31), LMT), |
| 808 |
(datetime(1912, 1, 2), GMT), |
| 809 |
] |
| 810 |
|
| 811 |
for dt_naive, offset in offset_cases: |
| 812 |
dt = dt_naive.replace(tzinfo=zi) |
| 813 |
with self.subTest(name="offset", dt=dt, offset=offset): |
| 814 |
self.assertEqual(dt.tzname(), offset.tzname) |
| 815 |
self.assertEqual(dt.utcoffset(), offset.utcoffset) |
| 816 |
self.assertEqual(dt.dst(), offset.dst) |
| 817 |
|
| 818 |
utc_cases = [ |
| 819 |
(datetime.min, datetime.min + timedelta(seconds=968)), |
| 820 |
(datetime(1898, 12, 31, 23, 43, 52), datetime(1899, 1, 1)), |
| 821 |
( |
| 822 |
datetime(1911, 12, 31, 23, 59, 59, 999999), |
| 823 |
datetime(1912, 1, 1, 0, 16, 7, 999999), |
| 824 |
), |
| 825 |
(datetime(1912, 1, 1, 0, 16, 8), datetime(1912, 1, 1, 0, 16, 8)), |
| 826 |
(datetime(1970, 1, 1), datetime(1970, 1, 1)), |
| 827 |
(datetime.max, datetime.max), |
| 828 |
] |
| 829 |
|
| 830 |
for naive_dt, naive_dt_utc in utc_cases: |
| 831 |
dt = naive_dt.replace(tzinfo=zi) |
| 832 |
dt_utc = naive_dt_utc.replace(tzinfo=timezone.utc) |
| 833 |
|
| 834 |
self.assertEqual(dt_utc.astimezone(zi), dt) |
| 835 |
self.assertEqual(dt, dt_utc) |
| 836 |
|
| 837 |
def test_fixed_offset_phantom_transition(self): |
| 838 |
UTC = ZoneOffset("UTC", ZERO, ZERO) |
| 839 |
|
| 840 |
transitions = [ZoneTransition(datetime(1970, 1, 1), UTC, UTC)] |
| 841 |
|
| 842 |
after = "UTC0" |
| 843 |
zf = self.construct_zone(transitions, after) |
| 844 |
zi = self.klass.from_file(zf, key="UTC") |
| 845 |
|
| 846 |
dt = datetime(2020, 1, 1, tzinfo=zi) |
| 847 |
with self.subTest("datetime.datetime"): |
| 848 |
self.assertEqual(dt.tzname(), UTC.tzname) |
| 849 |
self.assertEqual(dt.utcoffset(), UTC.utcoffset) |
| 850 |
self.assertEqual(dt.dst(), UTC.dst) |
| 851 |
|
| 852 |
t = time(0, tzinfo=zi) |
| 853 |
with self.subTest("datetime.time"): |
| 854 |
self.assertEqual(t.tzname(), UTC.tzname) |
| 855 |
self.assertEqual(t.utcoffset(), UTC.utcoffset) |
| 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) |
| 916 |
|
| 917 |
zonefile = io.BytesIO() |
| 918 |
|
| 919 |
time_types = ("l", "q") |
| 920 |
for v in range(min((version, 2))): |
| 921 |
offsets = offset_lists[v] |
| 922 |
trans_times = trans_times_lists[v] |
| 923 |
trans_idx = trans_idx_lists[v] |
| 924 |
time_type = time_types[v] |
| 925 |
|
| 926 |
# Translate the offsets into something closer to the C values |
| 927 |
abbrstr = bytearray() |
| 928 |
ttinfos = [] |
| 929 |
|
| 930 |
for offset in offsets: |
| 931 |
utcoff = int(offset.utcoffset.total_seconds()) |
| 932 |
isdst = bool(offset.dst) |
| 933 |
abbrind = len(abbrstr) |
| 934 |
|
| 935 |
ttinfos.append((utcoff, isdst, abbrind)) |
| 936 |
abbrstr += offset.tzname.encode("ascii") + b"\x00" |
| 937 |
abbrstr = bytes(abbrstr) |
| 938 |
|
| 939 |
typecnt = len(offsets) |
| 940 |
timecnt = len(trans_times) |
| 941 |
charcnt = len(abbrstr) |
| 942 |
|
| 943 |
# Write the header |
| 944 |
zonefile.write(b"TZif") |
| 945 |
zonefile.write(b"%d" % version) |
| 946 |
zonefile.write(b" " * 15) |
| 947 |
zonefile.write( |
| 948 |
struct.pack( |
| 949 |
">6l", isutcnt, isstdcnt, leapcnt, timecnt, typecnt, charcnt |
| 950 |
) |
| 951 |
) |
| 952 |
|
| 953 |
# Now the transition data |
| 954 |
zonefile.write(struct.pack(f">{timecnt}{time_type}", *trans_times)) |
| 955 |
zonefile.write(struct.pack(f">{timecnt}B", *trans_idx)) |
| 956 |
|
| 957 |
for ttinfo in ttinfos: |
| 958 |
zonefile.write(struct.pack(">lbb", *ttinfo)) |
| 959 |
|
| 960 |
zonefile.write(bytes(abbrstr)) |
| 961 |
|
| 962 |
# Now the metadata and leap seconds |
| 963 |
zonefile.write(struct.pack(f"{isutcnt}b", *isutc)) |
| 964 |
zonefile.write(struct.pack(f"{isstdcnt}b", *isstd)) |
| 965 |
zonefile.write(struct.pack(f">{leapcnt}l", *leap_seconds)) |
| 966 |
|
| 967 |
# Finally we write the TZ string if we're writing a Version 2+ file |
| 968 |
if v > 0: |
| 969 |
zonefile.write(b"\x0A") |
| 970 |
zonefile.write(after.encode("ascii")) |
| 971 |
zonefile.write(b"\x0A") |
| 972 |
|
| 973 |
zonefile.seek(0) |
| 974 |
return zonefile |
| 975 |
|
| 976 |
|
| 977 |
class CWeirdZoneTest(WeirdZoneTest): |
| 978 |
module = c_zoneinfo |
| 979 |
|
| 980 |
|
| 981 |
class TZStrTest(ZoneInfoTestBase): |
| 982 |
module = py_zoneinfo |
| 983 |
|
| 984 |
NORMAL = 0 |
| 985 |
FOLD = 1 |
| 986 |
GAP = 2 |
| 987 |
|
| 988 |
@classmethod |
| 989 |
def setUpClass(cls): |
| 990 |
super().setUpClass() |
| 991 |
|
| 992 |
cls._populate_test_cases() |
| 993 |
cls.populate_tzstr_header() |
| 994 |
|
| 995 |
@classmethod |
| 996 |
def populate_tzstr_header(cls): |
| 997 |
out = bytearray() |
| 998 |
# The TZif format always starts with a Version 1 file followed by |
| 999 |
# the Version 2+ file. In this case, we have no transitions, just |
| 1000 |
# the tzstr in the footer, so up to the footer, the files are |
| 1001 |
# identical and we can just write the same file twice in a row. |
| 1002 |
for _ in range(2): |
| 1003 |
out += b"TZif" # Magic value |
| 1004 |
out += b"3" # Version |
| 1005 |
out += b" " * 15 # Reserved |
| 1006 |
|
| 1007 |
# We will not write any of the manual transition parts |
| 1008 |
out += struct.pack(">6l", 0, 0, 0, 0, 0, 0) |
| 1009 |
|
| 1010 |
cls._tzif_header = bytes(out) |
| 1011 |
|
| 1012 |
def zone_from_tzstr(self, tzstr): |
| 1013 |
"""Creates a zoneinfo file following a POSIX rule.""" |
| 1014 |
zonefile = io.BytesIO(self._tzif_header) |
| 1015 |
zonefile.seek(0, 2) |
| 1016 |
|
| 1017 |
# Write the footer |
| 1018 |
zonefile.write(b"\x0A") |
| 1019 |
zonefile.write(tzstr.encode("ascii")) |
| 1020 |
zonefile.write(b"\x0A") |
| 1021 |
|
| 1022 |
zonefile.seek(0) |
| 1023 |
|
| 1024 |
return self.klass.from_file(zonefile, key=tzstr) |
| 1025 |
|
| 1026 |
def test_tzstr_localized(self): |
| 1027 |
for tzstr, cases in self.test_cases.items(): |
| 1028 |
with self.subTest(tzstr=tzstr): |
| 1029 |
zi = self.zone_from_tzstr(tzstr) |
| 1030 |
|
| 1031 |
for dt_naive, offset, _ in cases: |
| 1032 |
dt = dt_naive.replace(tzinfo=zi) |
| 1033 |
|
| 1034 |
with self.subTest(tzstr=tzstr, dt=dt, offset=offset): |
| 1035 |
self.assertEqual(dt.tzname(), offset.tzname) |
| 1036 |
self.assertEqual(dt.utcoffset(), offset.utcoffset) |
| 1037 |
self.assertEqual(dt.dst(), offset.dst) |
| 1038 |
|
| 1039 |
def test_tzstr_from_utc(self): |
| 1040 |
for tzstr, cases in self.test_cases.items(): |
| 1041 |
with self.subTest(tzstr=tzstr): |
| 1042 |
zi = self.zone_from_tzstr(tzstr) |
| 1043 |
|
| 1044 |
for dt_naive, offset, dt_type in cases: |
| 1045 |
if dt_type == self.GAP: |
| 1046 |
continue # Cannot create a gap from UTC |
| 1047 |
|
| 1048 |
dt_utc = (dt_naive - offset.utcoffset).replace( |
| 1049 |
tzinfo=timezone.utc |
| 1050 |
) |
| 1051 |
|
| 1052 |
# Check that we can go UTC -> Our zone |
| 1053 |
dt_act = dt_utc.astimezone(zi) |
| 1054 |
dt_exp = dt_naive.replace(tzinfo=zi) |
| 1055 |
|
| 1056 |
self.assertEqual(dt_act, dt_exp) |
| 1057 |
|
| 1058 |
if dt_type == self.FOLD: |
| 1059 |
self.assertEqual(dt_act.fold, dt_naive.fold, dt_naive) |
| 1060 |
else: |
| 1061 |
self.assertEqual(dt_act.fold, 0) |
| 1062 |
|
| 1063 |
# Now check that we can go our zone -> UTC |
| 1064 |
dt_act = dt_exp.astimezone(timezone.utc) |
| 1065 |
|
| 1066 |
self.assertEqual(dt_act, dt_utc) |
| 1067 |
|
| 1068 |
def test_extreme_tzstr(self): |
| 1069 |
tzstrs = [ |
| 1070 |
# Extreme offset hour |
| 1071 |
"AAA24", |
| 1072 |
"AAA+24", |
| 1073 |
"AAA-24", |
| 1074 |
"AAA24BBB,J60/2,J300/2", |
| 1075 |
"AAA+24BBB,J60/2,J300/2", |
| 1076 |
"AAA-24BBB,J60/2,J300/2", |
| 1077 |
"AAA4BBB24,J60/2,J300/2", |
| 1078 |
"AAA4BBB+24,J60/2,J300/2", |
| 1079 |
"AAA4BBB-24,J60/2,J300/2", |
| 1080 |
# Extreme offset minutes |
| 1081 |
"AAA4:00BBB,J60/2,J300/2", |
| 1082 |
"AAA4:59BBB,J60/2,J300/2", |
| 1083 |
"AAA4BBB5:00,J60/2,J300/2", |
| 1084 |
"AAA4BBB5:59,J60/2,J300/2", |
| 1085 |
# Extreme offset seconds |
| 1086 |
"AAA4:00:00BBB,J60/2,J300/2", |
| 1087 |
"AAA4:00:59BBB,J60/2,J300/2", |
| 1088 |
"AAA4BBB5:00:00,J60/2,J300/2", |
| 1089 |
"AAA4BBB5:00:59,J60/2,J300/2", |
| 1090 |
# Extreme total offset |
| 1091 |
"AAA24:59:59BBB5,J60/2,J300/2", |
| 1092 |
"AAA-24:59:59BBB5,J60/2,J300/2", |
| 1093 |
"AAA4BBB24:59:59,J60/2,J300/2", |
| 1094 |
"AAA4BBB-24:59:59,J60/2,J300/2", |
| 1095 |
# Extreme months |
| 1096 |
"AAA4BBB,M12.1.1/2,M1.1.1/2", |
| 1097 |
"AAA4BBB,M1.1.1/2,M12.1.1/2", |
| 1098 |
# Extreme weeks |
| 1099 |
"AAA4BBB,M1.5.1/2,M1.1.1/2", |
| 1100 |
"AAA4BBB,M1.1.1/2,M1.5.1/2", |
| 1101 |
# Extreme weekday |
| 1102 |
"AAA4BBB,M1.1.6/2,M2.1.1/2", |
| 1103 |
"AAA4BBB,M1.1.1/2,M2.1.6/2", |
| 1104 |
# Extreme numeric offset |
| 1105 |
"AAA4BBB,0/2,20/2", |
| 1106 |
"AAA4BBB,0/2,0/14", |
| 1107 |
"AAA4BBB,20/2,365/2", |
| 1108 |
"AAA4BBB,365/2,365/14", |
| 1109 |
# Extreme julian offset |
| 1110 |
"AAA4BBB,J1/2,J20/2", |
| 1111 |
"AAA4BBB,J1/2,J1/14", |
| 1112 |
"AAA4BBB,J20/2,J365/2", |
| 1113 |
"AAA4BBB,J365/2,J365/14", |
| 1114 |
# Extreme transition hour |
| 1115 |
"AAA4BBB,J60/167,J300/2", |
| 1116 |
"AAA4BBB,J60/+167,J300/2", |
| 1117 |
"AAA4BBB,J60/-167,J300/2", |
| 1118 |
"AAA4BBB,J60/2,J300/167", |
| 1119 |
"AAA4BBB,J60/2,J300/+167", |
| 1120 |
"AAA4BBB,J60/2,J300/-167", |
| 1121 |
# Extreme transition minutes |
| 1122 |
"AAA4BBB,J60/2:00,J300/2", |
| 1123 |
"AAA4BBB,J60/2:59,J300/2", |
| 1124 |
"AAA4BBB,J60/2,J300/2:00", |
| 1125 |
"AAA4BBB,J60/2,J300/2:59", |
| 1126 |
# Extreme transition seconds |
| 1127 |
"AAA4BBB,J60/2:00:00,J300/2", |
| 1128 |
"AAA4BBB,J60/2:00:59,J300/2", |
| 1129 |
"AAA4BBB,J60/2,J300/2:00:00", |
| 1130 |
"AAA4BBB,J60/2,J300/2:00:59", |
| 1131 |
# Extreme total transition time |
| 1132 |
"AAA4BBB,J60/167:59:59,J300/2", |
| 1133 |
"AAA4BBB,J60/-167:59:59,J300/2", |
| 1134 |
"AAA4BBB,J60/2,J300/167:59:59", |
| 1135 |
"AAA4BBB,J60/2,J300/-167:59:59", |
| 1136 |
] |
| 1137 |
|
| 1138 |
for tzstr in tzstrs: |
| 1139 |
with self.subTest(tzstr=tzstr): |
| 1140 |
self.zone_from_tzstr(tzstr) |
| 1141 |
|
| 1142 |
def test_invalid_tzstr(self): |
| 1143 |
invalid_tzstrs = [ |
| 1144 |
"PST8PDT", # DST but no transition specified |
| 1145 |
"+11", # Unquoted alphanumeric |
| 1146 |
"GMT,M3.2.0/2,M11.1.0/3", # Transition rule but no DST |
| 1147 |
"GMT0+11,M3.2.0/2,M11.1.0/3", # Unquoted alphanumeric in DST |
| 1148 |
"PST8PDT,M3.2.0/2", # Only one transition rule |
| 1149 |
# Invalid offset hours |
| 1150 |
"AAA168", |
| 1151 |
"AAA+168", |
| 1152 |
"AAA-168", |
| 1153 |
"AAA168BBB,J60/2,J300/2", |
| 1154 |
"AAA+168BBB,J60/2,J300/2", |
| 1155 |
"AAA-168BBB,J60/2,J300/2", |
| 1156 |
"AAA4BBB168,J60/2,J300/2", |
| 1157 |
"AAA4BBB+168,J60/2,J300/2", |
| 1158 |
"AAA4BBB-168,J60/2,J300/2", |
| 1159 |
# Invalid offset minutes |
| 1160 |
"AAA4:0BBB,J60/2,J300/2", |
| 1161 |
"AAA4:100BBB,J60/2,J300/2", |
| 1162 |
"AAA4BBB5:0,J60/2,J300/2", |
| 1163 |
"AAA4BBB5:100,J60/2,J300/2", |
| 1164 |
# Invalid offset seconds |
| 1165 |
"AAA4:00:0BBB,J60/2,J300/2", |
| 1166 |
"AAA4:00:100BBB,J60/2,J300/2", |
| 1167 |
"AAA4BBB5:00:0,J60/2,J300/2", |
| 1168 |
"AAA4BBB5:00:100,J60/2,J300/2", |
| 1169 |
# Completely invalid dates |
| 1170 |
"AAA4BBB,M1443339,M11.1.0/3", |
| 1171 |
"AAA4BBB,M3.2.0/2,0349309483959c", |
| 1172 |
"AAA4BBB,,J300/2", |
| 1173 |
"AAA4BBB,z,J300/2", |
| 1174 |
"AAA4BBB,J60/2,", |
| 1175 |
"AAA4BBB,J60/2,z", |
| 1176 |
# Invalid months |
| 1177 |
"AAA4BBB,M13.1.1/2,M1.1.1/2", |
| 1178 |
"AAA4BBB,M1.1.1/2,M13.1.1/2", |
| 1179 |
"AAA4BBB,M0.1.1/2,M1.1.1/2", |
| 1180 |
"AAA4BBB,M1.1.1/2,M0.1.1/2", |
| 1181 |
# Invalid weeks |
| 1182 |
"AAA4BBB,M1.6.1/2,M1.1.1/2", |
| 1183 |
"AAA4BBB,M1.1.1/2,M1.6.1/2", |
| 1184 |
# Invalid weekday |
| 1185 |
"AAA4BBB,M1.1.7/2,M2.1.1/2", |
| 1186 |
"AAA4BBB,M1.1.1/2,M2.1.7/2", |
| 1187 |
# Invalid numeric offset |
| 1188 |
"AAA4BBB,-1/2,20/2", |
| 1189 |
"AAA4BBB,1/2,-1/2", |
| 1190 |
"AAA4BBB,367,20/2", |
| 1191 |
"AAA4BBB,1/2,367/2", |
| 1192 |
# Invalid julian offset |
| 1193 |
"AAA4BBB,J0/2,J20/2", |
| 1194 |
"AAA4BBB,J20/2,J366/2", |
| 1195 |
# Invalid transition time |
| 1196 |
"AAA4BBB,J60/2/3,J300/2", |
| 1197 |
"AAA4BBB,J60/2,J300/2/3", |
| 1198 |
# Invalid transition hour |
| 1199 |
"AAA4BBB,J60/168,J300/2", |
| 1200 |
"AAA4BBB,J60/+168,J300/2", |
| 1201 |
"AAA4BBB,J60/-168,J300/2", |
| 1202 |
"AAA4BBB,J60/2,J300/168", |
| 1203 |
"AAA4BBB,J60/2,J300/+168", |
| 1204 |
"AAA4BBB,J60/2,J300/-168", |
| 1205 |
# Invalid transition minutes |
| 1206 |
"AAA4BBB,J60/2:0,J300/2", |
| 1207 |
"AAA4BBB,J60/2:100,J300/2", |
| 1208 |
"AAA4BBB,J60/2,J300/2:0", |
| 1209 |
"AAA4BBB,J60/2,J300/2:100", |
| 1210 |
# Invalid transition seconds |
| 1211 |
"AAA4BBB,J60/2:00:0,J300/2", |
| 1212 |
"AAA4BBB,J60/2:00:100,J300/2", |
| 1213 |
"AAA4BBB,J60/2,J300/2:00:0", |
| 1214 |
"AAA4BBB,J60/2,J300/2:00:100", |
| 1215 |
] |
| 1216 |
|
| 1217 |
for invalid_tzstr in invalid_tzstrs: |
| 1218 |
with self.subTest(tzstr=invalid_tzstr): |
| 1219 |
# Not necessarily a guaranteed property, but we should show |
| 1220 |
# the problematic TZ string if that's the cause of failure. |
| 1221 |
tzstr_regex = re.escape(invalid_tzstr) |
| 1222 |
with self.assertRaisesRegex(ValueError, tzstr_regex): |
| 1223 |
self.zone_from_tzstr(invalid_tzstr) |
| 1224 |
|
| 1225 |
@classmethod |
| 1226 |
def _populate_test_cases(cls): |
| 1227 |
# This method uses a somewhat unusual style in that it populates the |
| 1228 |
# test cases for each tzstr by using a decorator to automatically call |
| 1229 |
# a function that mutates the current dictionary of test cases. |
| 1230 |
# |
| 1231 |
# The population of the test cases is done in individual functions to |
| 1232 |
# give each set of test cases its own namespace in which to define |
| 1233 |
# its offsets (this way we don't have to worry about variable reuse |
| 1234 |
# causing problems if someone makes a typo). |
| 1235 |
# |
| 1236 |
# The decorator for calling is used to make it more obvious that each |
| 1237 |
# function is actually called (if it's not decorated, it's not called). |
| 1238 |
def call(f): |
| 1239 |
"""Decorator to call the addition methods. |
| 1240 |
|
| 1241 |
This will call a function which adds at least one new entry into |
| 1242 |
the `cases` dictionary. The decorator will also assert that |
| 1243 |
something was added to the dictionary. |
| 1244 |
""" |
| 1245 |
prev_len = len(cases) |
| 1246 |
f() |
| 1247 |
assert len(cases) > prev_len, "Function did not add a test case!" |
| 1248 |
|
| 1249 |
NORMAL = cls.NORMAL |
| 1250 |
FOLD = cls.FOLD |
| 1251 |
GAP = cls.GAP |
| 1252 |
|
| 1253 |
cases = {} |
| 1254 |
|
| 1255 |
@call |
| 1256 |
def _add(): |
| 1257 |
# Transition to EDT on the 2nd Sunday in March at 4 AM, and |
| 1258 |
# transition back on the first Sunday in November at 3AM |
| 1259 |
tzstr = "EST5EDT,M3.2.0/4:00,M11.1.0/3:00" |
| 1260 |
|
| 1261 |
EST = ZoneOffset("EST", timedelta(hours=-5), ZERO) |
| 1262 |
EDT = ZoneOffset("EDT", timedelta(hours=-4), ONE_H) |
| 1263 |
|
| 1264 |
cases[tzstr] = ( |
| 1265 |
(datetime(2019, 3, 9), EST, NORMAL), |
| 1266 |
(datetime(2019, 3, 10, 3, 59), EST, NORMAL), |
| 1267 |
(datetime(2019, 3, 10, 4, 0, fold=0), EST, GAP), |
| 1268 |
(datetime(2019, 3, 10, 4, 0, fold=1), EDT, GAP), |
| 1269 |
(datetime(2019, 3, 10, 4, 1, fold=0), EST, GAP), |
| 1270 |
(datetime(2019, 3, 10, 4, 1, fold=1), EDT, GAP), |
| 1271 |
(datetime(2019, 11, 2), EDT, NORMAL), |
| 1272 |
(datetime(2019, 11, 3, 1, 59, fold=1), EDT, NORMAL), |
| 1273 |
(datetime(2019, 11, 3, 2, 0, fold=0), EDT, FOLD), |
| 1274 |
(datetime(2019, 11, 3, 2, 0, fold=1), EST, FOLD), |
| 1275 |
(datetime(2020, 3, 8, 3, 59), EST, NORMAL), |
| 1276 |
(datetime(2020, 3, 8, 4, 0, fold=0), EST, GAP), |
| 1277 |
(datetime(2020, 3, 8, 4, 0, fold=1), EDT, GAP), |
| 1278 |
(datetime(2020, 11, 1, 1, 59, fold=1), EDT, NORMAL), |
| 1279 |
(datetime(2020, 11, 1, 2, 0, fold=0), EDT, FOLD), |
| 1280 |
(datetime(2020, 11, 1, 2, 0, fold=1), EST, FOLD), |
| 1281 |
) |
| 1282 |
|
| 1283 |
@call |
| 1284 |
def _add(): |
| 1285 |
# Transition to BST happens on the last Sunday in March at 1 AM GMT |
| 1286 |
# and the transition back happens the last Sunday in October at 2AM BST |
| 1287 |
tzstr = "GMT0BST-1,M3.5.0/1:00,M10.5.0/2:00" |
| 1288 |
|
| 1289 |
GMT = ZoneOffset("GMT", ZERO, ZERO) |
| 1290 |
BST = ZoneOffset("BST", ONE_H, ONE_H) |
| 1291 |
|
| 1292 |
cases[tzstr] = ( |
| 1293 |
(datetime(2019, 3, 30), GMT, NORMAL), |
| 1294 |
(datetime(2019, 3, 31, 0, 59), GMT, NORMAL), |
| 1295 |
(datetime(2019, 3, 31, 2, 0), BST, NORMAL), |
| 1296 |
(datetime(2019, 10, 26), BST, NORMAL), |
| 1297 |
(datetime(2019, 10, 27, 0, 59, fold=1), BST, NORMAL), |
| 1298 |
(datetime(2019, 10, 27, 1, 0, fold=0), BST, GAP), |
| 1299 |
(datetime(2019, 10, 27, 2, 0, fold=1), GMT, GAP), |
| 1300 |
(datetime(2020, 3, 29, 0, 59), GMT, NORMAL), |
| 1301 |
(datetime(2020, 3, 29, 2, 0), BST, NORMAL), |
| 1302 |
(datetime(2020, 10, 25, 0, 59, fold=1), BST, NORMAL), |
| 1303 |
(datetime(2020, 10, 25, 1, 0, fold=0), BST, FOLD), |
| 1304 |
(datetime(2020, 10, 25, 2, 0, fold=1), GMT, NORMAL), |
| 1305 |
) |
| 1306 |
|
| 1307 |
@call |
| 1308 |
def _add(): |
| 1309 |
# Austrialian time zone - DST start is chronologically first |
| 1310 |
tzstr = "AEST-10AEDT,M10.1.0/2,M4.1.0/3" |
| 1311 |
|
| 1312 |
AEST = ZoneOffset("AEST", timedelta(hours=10), ZERO) |
| 1313 |
AEDT = ZoneOffset("AEDT", timedelta(hours=11), ONE_H) |
| 1314 |
|
| 1315 |
cases[tzstr] = ( |
| 1316 |
(datetime(2019, 4, 6), AEDT, NORMAL), |
| 1317 |
(datetime(2019, 4, 7, 1, 59), AEDT, NORMAL), |
| 1318 |
(datetime(2019, 4, 7, 1, 59, fold=1), AEDT, NORMAL), |
| 1319 |
(datetime(2019, 4, 7, 2, 0, fold=0), AEDT, FOLD), |
| 1320 |
(datetime(2019, 4, 7, 2, 1, fold=0), AEDT, FOLD), |
| 1321 |
(datetime(2019, 4, 7, 2, 0, fold=1), AEST, FOLD), |
| 1322 |
(datetime(2019, 4, 7, 2, 1, fold=1), AEST, FOLD), |
| 1323 |
(datetime(2019, 4, 7, 3, 0, fold=0), AEST, NORMAL), |
| 1324 |
(datetime(2019, 4, 7, 3, 0, fold=1), AEST, NORMAL), |
| 1325 |
(datetime(2019, 10, 5, 0), AEST, NORMAL), |
| 1326 |
(datetime(2019, 10, 6, 1, 59), AEST, NORMAL), |
| 1327 |
(datetime(2019, 10, 6, 2, 0, fold=0), AEST, GAP), |
| 1328 |
(datetime(2019, 10, 6, 2, 0, fold=1), AEDT, GAP), |
| 1329 |
(datetime(2019, 10, 6, 3, 0), AEDT, NORMAL), |
| 1330 |
) |
| 1331 |
|
| 1332 |
@call |
| 1333 |
def _add(): |
| 1334 |
# Irish time zone - negative DST |
| 1335 |
tzstr = "IST-1GMT0,M10.5.0,M3.5.0/1" |
| 1336 |
|
| 1337 |
GMT = ZoneOffset("GMT", ZERO, -ONE_H) |
| 1338 |
IST = ZoneOffset("IST", ONE_H, ZERO) |
| 1339 |
|
| 1340 |
cases[tzstr] = ( |
| 1341 |
(datetime(2019, 3, 30), GMT, NORMAL), |
| 1342 |
(datetime(2019, 3, 31, 0, 59), GMT, NORMAL), |
| 1343 |
(datetime(2019, 3, 31, 2, 0), IST, NORMAL), |
| 1344 |
(datetime(2019, 10, 26), IST, NORMAL), |
| 1345 |
(datetime(2019, 10, 27, 0, 59, fold=1), IST, NORMAL), |
| 1346 |
(datetime(2019, 10, 27, 1, 0, fold=0), IST, FOLD), |
| 1347 |
(datetime(2019, 10, 27, 1, 0, fold=1), GMT, FOLD), |
| 1348 |
(datetime(2019, 10, 27, 2, 0, fold=1), GMT, NORMAL), |
| 1349 |
(datetime(2020, 3, 29, 0, 59), GMT, NORMAL), |
| 1350 |
(datetime(2020, 3, 29, 2, 0), IST, NORMAL), |
| 1351 |
(datetime(2020, 10, 25, 0, 59, fold=1), IST, NORMAL), |
| 1352 |
(datetime(2020, 10, 25, 1, 0, fold=0), IST, FOLD), |
| 1353 |
(datetime(2020, 10, 25, 2, 0, fold=1), GMT, NORMAL), |
| 1354 |
) |
| 1355 |
|
| 1356 |
@call |
| 1357 |
def _add(): |
| 1358 |
# Pacific/Kosrae: Fixed offset zone with a quoted numerical tzname |
| 1359 |
tzstr = "<+11>-11" |
| 1360 |
|
| 1361 |
cases[tzstr] = ( |
| 1362 |
( |
| 1363 |
datetime(2020, 1, 1), |
| 1364 |
ZoneOffset("+11", timedelta(hours=11)), |
| 1365 |
NORMAL, |
| 1366 |
), |
| 1367 |
) |
| 1368 |
|
| 1369 |
@call |
| 1370 |
def _add(): |
| 1371 |
# Quoted STD and DST, transitions at 24:00 |
| 1372 |
tzstr = "<-04>4<-03>,M9.1.6/24,M4.1.6/24" |
| 1373 |
|
| 1374 |
M04 = ZoneOffset("-04", timedelta(hours=-4)) |
| 1375 |
M03 = ZoneOffset("-03", timedelta(hours=-3), ONE_H) |
| 1376 |
|
| 1377 |
cases[tzstr] = ( |
| 1378 |
(datetime(2020, 5, 1), M04, NORMAL), |
| 1379 |
(datetime(2020, 11, 1), M03, NORMAL), |
| 1380 |
) |
| 1381 |
|
| 1382 |
@call |
| 1383 |
def _add(): |
| 1384 |
# Permanent daylight saving time is modeled with transitions at 0/0 |
| 1385 |
# and J365/25, as mentioned in RFC 8536 Section 3.3.1 |
| 1386 |
tzstr = "EST5EDT,0/0,J365/25" |
| 1387 |
|
| 1388 |
EDT = ZoneOffset("EDT", timedelta(hours=-4), ONE_H) |
| 1389 |
|
| 1390 |
cases[tzstr] = ( |
| 1391 |
(datetime(2019, 1, 1), EDT, NORMAL), |
| 1392 |
(datetime(2019, 6, 1), EDT, NORMAL), |
| 1393 |
(datetime(2019, 12, 31, 23, 59, 59, 999999), EDT, NORMAL), |
| 1394 |
(datetime(2020, 1, 1), EDT, NORMAL), |
| 1395 |
(datetime(2020, 3, 1), EDT, NORMAL), |
| 1396 |
(datetime(2020, 6, 1), EDT, NORMAL), |
| 1397 |
(datetime(2020, 12, 31, 23, 59, 59, 999999), EDT, NORMAL), |
| 1398 |
(datetime(2400, 1, 1), EDT, NORMAL), |
| 1399 |
(datetime(2400, 3, 1), EDT, NORMAL), |
| 1400 |
(datetime(2400, 12, 31, 23, 59, 59, 999999), EDT, NORMAL), |
| 1401 |
) |
| 1402 |
|
| 1403 |
@call |
| 1404 |
def _add(): |
| 1405 |
# Transitions on March 1st and November 1st of each year |
| 1406 |
tzstr = "AAA3BBB,J60/12,J305/12" |
| 1407 |
|
| 1408 |
AAA = ZoneOffset("AAA", timedelta(hours=-3)) |
| 1409 |
BBB = ZoneOffset("BBB", timedelta(hours=-2), ONE_H) |
| 1410 |
|
| 1411 |
cases[tzstr] = ( |
| 1412 |
(datetime(2019, 1, 1), AAA, NORMAL), |
| 1413 |
(datetime(2019, 2, 28), AAA, NORMAL), |
| 1414 |
(datetime(2019, 3, 1, 11, 59), AAA, NORMAL), |
| 1415 |
(datetime(2019, 3, 1, 12, fold=0), AAA, GAP), |
| 1416 |
(datetime(2019, 3, 1, 12, fold=1), BBB, GAP), |
| 1417 |
(datetime(2019, 3, 1, 13), BBB, NORMAL), |
| 1418 |
(datetime(2019, 11, 1, 10, 59), BBB, NORMAL), |
| 1419 |
(datetime(2019, 11, 1, 11, fold=0), BBB, FOLD), |
| 1420 |
(datetime(2019, 11, 1, 11, fold=1), AAA, FOLD), |
| 1421 |
(datetime(2019, 11, 1, 12), AAA, NORMAL), |
| 1422 |
(datetime(2019, 12, 31, 23, 59, 59, 999999), AAA, NORMAL), |
| 1423 |
(datetime(2020, 1, 1), AAA, NORMAL), |
| 1424 |
(datetime(2020, 2, 29), AAA, NORMAL), |
| 1425 |
(datetime(2020, 3, 1, 11, 59), AAA, NORMAL), |
| 1426 |
(datetime(2020, 3, 1, 12, fold=0), AAA, GAP), |
| 1427 |
(datetime(2020, 3, 1, 12, fold=1), BBB, GAP), |
| 1428 |
(datetime(2020, 3, 1, 13), BBB, NORMAL), |
| 1429 |
(datetime(2020, 11, 1, 10, 59), BBB, NORMAL), |
| 1430 |
(datetime(2020, 11, 1, 11, fold=0), BBB, FOLD), |
| 1431 |
(datetime(2020, 11, 1, 11, fold=1), AAA, FOLD), |
| 1432 |
(datetime(2020, 11, 1, 12), AAA, NORMAL), |
| 1433 |
(datetime(2020, 12, 31, 23, 59, 59, 999999), AAA, NORMAL), |
| 1434 |
) |
| 1435 |
|
| 1436 |
@call |
| 1437 |
def _add(): |
| 1438 |
# Taken from America/Godthab, this rule has a transition on the |
| 1439 |
# Saturday before the last Sunday of March and October, at 22:00 |
| 1440 |
# and 23:00, respectively. This is encoded with negative start |
| 1441 |
# and end transition times. |
| 1442 |
tzstr = "<-03>3<-02>,M3.5.0/-2,M10.5.0/-1" |
| 1443 |
|
| 1444 |
N03 = ZoneOffset("-03", timedelta(hours=-3)) |
| 1445 |
N02 = ZoneOffset("-02", timedelta(hours=-2), ONE_H) |
| 1446 |
|
| 1447 |
cases[tzstr] = ( |
| 1448 |
(datetime(2020, 3, 27), N03, NORMAL), |
| 1449 |
(datetime(2020, 3, 28, 21, 59, 59), N03, NORMAL), |
| 1450 |
(datetime(2020, 3, 28, 22, fold=0), N03, GAP), |
| 1451 |
(datetime(2020, 3, 28, 22, fold=1), N02, GAP), |
| 1452 |
(datetime(2020, 3, 28, 23), N02, NORMAL), |
| 1453 |
(datetime(2020, 10, 24, 21), N02, NORMAL), |
| 1454 |
(datetime(2020, 10, 24, 22, fold=0), N02, FOLD), |
| 1455 |
(datetime(2020, 10, 24, 22, fold=1), N03, FOLD), |
| 1456 |
(datetime(2020, 10, 24, 23), N03, NORMAL), |
| 1457 |
) |
| 1458 |
|
| 1459 |
@call |
| 1460 |
def _add(): |
| 1461 |
# Transition times with minutes and seconds |
| 1462 |
tzstr = "AAA3BBB,M3.2.0/01:30,M11.1.0/02:15:45" |
| 1463 |
|
| 1464 |
AAA = ZoneOffset("AAA", timedelta(hours=-3)) |
| 1465 |
BBB = ZoneOffset("BBB", timedelta(hours=-2), ONE_H) |
| 1466 |
|
| 1467 |
cases[tzstr] = ( |
| 1468 |
(datetime(2012, 3, 11, 1, 0), AAA, NORMAL), |
| 1469 |
(datetime(2012, 3, 11, 1, 30, fold=0), AAA, GAP), |
| 1470 |
(datetime(2012, 3, 11, 1, 30, fold=1), BBB, GAP), |
| 1471 |
(datetime(2012, 3, 11, 2, 30), BBB, NORMAL), |
| 1472 |
(datetime(2012, 11, 4, 1, 15, 44, 999999), BBB, NORMAL), |
| 1473 |
(datetime(2012, 11, 4, 1, 15, 45, fold=0), BBB, FOLD), |
| 1474 |
(datetime(2012, 11, 4, 1, 15, 45, fold=1), AAA, FOLD), |
| 1475 |
(datetime(2012, 11, 4, 2, 15, 45), AAA, NORMAL), |
| 1476 |
) |
| 1477 |
|
| 1478 |
cls.test_cases = cases |
| 1479 |
|
| 1480 |
|
| 1481 |
class CTZStrTest(TZStrTest): |
| 1482 |
module = c_zoneinfo |
| 1483 |
|
| 1484 |
|
| 1485 |
class ZoneInfoCacheTest(TzPathUserMixin, ZoneInfoTestBase): |
| 1486 |
module = py_zoneinfo |
| 1487 |
|
| 1488 |
def setUp(self): |
| 1489 |
self.klass.clear_cache() |
| 1490 |
super().setUp() |
| 1491 |
|
| 1492 |
@property |
| 1493 |
def zoneinfo_data(self): |
| 1494 |
return ZONEINFO_DATA |
| 1495 |
|
| 1496 |
@property |
| 1497 |
def tzpath(self): |
| 1498 |
return [self.zoneinfo_data.tzpath] |
| 1499 |
|
| 1500 |
def test_ephemeral_zones(self): |
| 1501 |
self.assertIs( |
| 1502 |
self.klass("America/Los_Angeles"), self.klass("America/Los_Angeles") |
| 1503 |
) |
| 1504 |
|
| 1505 |
def test_strong_refs(self): |
| 1506 |
tz0 = self.klass("Australia/Sydney") |
| 1507 |
tz1 = self.klass("Australia/Sydney") |
| 1508 |
|
| 1509 |
self.assertIs(tz0, tz1) |
| 1510 |
|
| 1511 |
def test_no_cache(self): |
| 1512 |
|
| 1513 |
tz0 = self.klass("Europe/Lisbon") |
| 1514 |
tz1 = self.klass.no_cache("Europe/Lisbon") |
| 1515 |
|
| 1516 |
self.assertIsNot(tz0, tz1) |
| 1517 |
|
| 1518 |
def test_cache_reset_tzpath(self): |
| 1519 |
"""Test that the cache persists when tzpath has been changed. |
| 1520 |
|
| 1521 |
The PEP specifies that as long as a reference exists to one zone |
| 1522 |
with a given key, the primary constructor must continue to return |
| 1523 |
the same object. |
| 1524 |
""" |
| 1525 |
zi0 = self.klass("America/Los_Angeles") |
| 1526 |
with self.tzpath_context([]): |
| 1527 |
zi1 = self.klass("America/Los_Angeles") |
| 1528 |
|
| 1529 |
self.assertIs(zi0, zi1) |
| 1530 |
|
| 1531 |
def test_clear_cache_explicit_none(self): |
| 1532 |
la0 = self.klass("America/Los_Angeles") |
| 1533 |
self.klass.clear_cache(only_keys=None) |
| 1534 |
la1 = self.klass("America/Los_Angeles") |
| 1535 |
|
| 1536 |
self.assertIsNot(la0, la1) |
| 1537 |
|
| 1538 |
def test_clear_cache_one_key(self): |
| 1539 |
"""Tests that you can clear a single key from the cache.""" |
| 1540 |
la0 = self.klass("America/Los_Angeles") |
| 1541 |
dub0 = self.klass("Europe/Dublin") |
| 1542 |
|
| 1543 |
self.klass.clear_cache(only_keys=["America/Los_Angeles"]) |
| 1544 |
|
| 1545 |
la1 = self.klass("America/Los_Angeles") |
| 1546 |
dub1 = self.klass("Europe/Dublin") |
| 1547 |
|
| 1548 |
self.assertIsNot(la0, la1) |
| 1549 |
self.assertIs(dub0, dub1) |
| 1550 |
|
| 1551 |
def test_clear_cache_two_keys(self): |
| 1552 |
la0 = self.klass("America/Los_Angeles") |
| 1553 |
dub0 = self.klass("Europe/Dublin") |
| 1554 |
tok0 = self.klass("Asia/Tokyo") |
| 1555 |
|
| 1556 |
self.klass.clear_cache( |
| 1557 |
only_keys=["America/Los_Angeles", "Europe/Dublin"] |
| 1558 |
) |
| 1559 |
|
| 1560 |
la1 = self.klass("America/Los_Angeles") |
| 1561 |
dub1 = self.klass("Europe/Dublin") |
| 1562 |
tok1 = self.klass("Asia/Tokyo") |
| 1563 |
|
| 1564 |
self.assertIsNot(la0, la1) |
| 1565 |
self.assertIsNot(dub0, dub1) |
| 1566 |
self.assertIs(tok0, tok1) |
| 1567 |
|
| 1568 |
def test_clear_cache_refleak(self): |
| 1569 |
class Stringy(str): |
| 1570 |
allow_comparisons = True |
| 1571 |
def __eq__(self, other): |
| 1572 |
if not self.allow_comparisons: |
| 1573 |
raise CustomError |
| 1574 |
return super().__eq__(other) |
| 1575 |
__hash__ = str.__hash__ |
| 1576 |
|
| 1577 |
key = Stringy("America/Los_Angeles") |
| 1578 |
self.klass(key) |
| 1579 |
key.allow_comparisons = False |
| 1580 |
try: |
| 1581 |
# Note: This is try/except rather than assertRaises because |
| 1582 |
# there is no guarantee that the key is even still in the cache, |
| 1583 |
# or that the key for the cache is the original `key` object. |
| 1584 |
self.klass.clear_cache(only_keys="America/Los_Angeles") |
| 1585 |
except CustomError: |
| 1586 |
pass |
| 1587 |
|
| 1588 |
def test_weak_cache_descriptor_use_after_free(self): |
| 1589 |
class BombDescriptor: |
| 1590 |
def __get__(self, obj, owner): |
| 1591 |
return {} |
| 1592 |
|
| 1593 |
class EvilZoneInfo(self.klass): |
| 1594 |
pass |
| 1595 |
|
| 1596 |
# Must be set after the class creation. |
| 1597 |
EvilZoneInfo._weak_cache = BombDescriptor() |
| 1598 |
|
| 1599 |
key = "America/Los_Angeles" |
| 1600 |
zone1 = EvilZoneInfo(key) |
| 1601 |
self.assertEqual(str(zone1), key) |
| 1602 |
|
| 1603 |
EvilZoneInfo.clear_cache() |
| 1604 |
zone2 = EvilZoneInfo(key) |
| 1605 |
self.assertEqual(str(zone2), key) |
| 1606 |
self.assertIsNot(zone2, zone1) |
| 1607 |
|
| 1608 |
|
| 1609 |
class CZoneInfoCacheTest(ZoneInfoCacheTest): |
| 1610 |
module = c_zoneinfo |
| 1611 |
|
| 1612 |
def test_inconsistent_weak_cache_get(self): |
| 1613 |
class Cache: |
| 1614 |
def get(self, key, default=None): |
| 1615 |
return 1337 |
| 1616 |
|
| 1617 |
class ZI(self.klass): |
| 1618 |
pass |
| 1619 |
# Class attribute must be set after class creation |
| 1620 |
# to override zoneinfo.ZoneInfo.__init_subclass__. |
| 1621 |
ZI._weak_cache = Cache() |
| 1622 |
|
| 1623 |
with self.assertRaises(RuntimeError) as te: |
| 1624 |
ZI("America/Los_Angeles") |
| 1625 |
self.assertEqual( |
| 1626 |
str(te.exception), |
| 1627 |
"Unexpected instance of int in ZI weak cache for key 'America/Los_Angeles'" |
| 1628 |
) |
| 1629 |
|
| 1630 |
def test_deleted_weak_cache(self): |
| 1631 |
class ZI(self.klass): |
| 1632 |
pass |
| 1633 |
delattr(ZI, '_weak_cache') |
| 1634 |
|
| 1635 |
# These should not segfault |
| 1636 |
with self.assertRaises(AttributeError): |
| 1637 |
ZI("UTC") |
| 1638 |
|
| 1639 |
with self.assertRaises(AttributeError): |
| 1640 |
ZI.clear_cache() |
| 1641 |
|
| 1642 |
def test_inconsistent_weak_cache_setdefault(self): |
| 1643 |
class Cache: |
| 1644 |
def get(self, key, default=None): |
| 1645 |
return default |
| 1646 |
def setdefault(self, key, value): |
| 1647 |
return 1337 |
| 1648 |
|
| 1649 |
class ZI(self.klass): |
| 1650 |
pass |
| 1651 |
# Class attribute must be set after class creation |
| 1652 |
# to override zoneinfo.ZoneInfo.__init_subclass__. |
| 1653 |
ZI._weak_cache = Cache() |
| 1654 |
|
| 1655 |
with self.assertRaises(RuntimeError) as te: |
| 1656 |
ZI("America/Los_Angeles") |
| 1657 |
self.assertEqual( |
| 1658 |
str(te.exception), |
| 1659 |
"Unexpected instance of int in ZI weak cache for key 'America/Los_Angeles'" |
| 1660 |
) |
| 1661 |
|
| 1662 |
|
| 1663 |
class ZoneInfoPickleTest(TzPathUserMixin, ZoneInfoTestBase): |
| 1664 |
module = py_zoneinfo |
| 1665 |
|
| 1666 |
def setUp(self): |
| 1667 |
self.klass.clear_cache() |
| 1668 |
|
| 1669 |
with contextlib.ExitStack() as stack: |
| 1670 |
stack.enter_context(test_support.set_zoneinfo_module(self.module)) |
| 1671 |
self.addCleanup(stack.pop_all().close) |
| 1672 |
|
| 1673 |
super().setUp() |
| 1674 |
|
| 1675 |
@property |
| 1676 |
def zoneinfo_data(self): |
| 1677 |
return ZONEINFO_DATA |
| 1678 |
|
| 1679 |
@property |
| 1680 |
def tzpath(self): |
| 1681 |
return [self.zoneinfo_data.tzpath] |
| 1682 |
|
| 1683 |
def test_cache_hit(self): |
| 1684 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1685 |
with self.subTest(proto=proto): |
| 1686 |
zi_in = self.klass("Europe/Dublin") |
| 1687 |
pkl = pickle.dumps(zi_in, protocol=proto) |
| 1688 |
zi_rt = pickle.loads(pkl) |
| 1689 |
|
| 1690 |
with self.subTest(test="Is non-pickled ZoneInfo"): |
| 1691 |
self.assertIs(zi_in, zi_rt) |
| 1692 |
|
| 1693 |
zi_rt2 = pickle.loads(pkl) |
| 1694 |
with self.subTest(test="Is unpickled ZoneInfo"): |
| 1695 |
self.assertIs(zi_rt, zi_rt2) |
| 1696 |
|
| 1697 |
def test_cache_miss(self): |
| 1698 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1699 |
with self.subTest(proto=proto): |
| 1700 |
zi_in = self.klass("Europe/Dublin") |
| 1701 |
pkl = pickle.dumps(zi_in, protocol=proto) |
| 1702 |
|
| 1703 |
del zi_in |
| 1704 |
self.klass.clear_cache() # Induce a cache miss |
| 1705 |
zi_rt = pickle.loads(pkl) |
| 1706 |
zi_rt2 = pickle.loads(pkl) |
| 1707 |
|
| 1708 |
self.assertIs(zi_rt, zi_rt2) |
| 1709 |
|
| 1710 |
def test_no_cache(self): |
| 1711 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1712 |
with self.subTest(proto=proto): |
| 1713 |
zi_no_cache = self.klass.no_cache("Europe/Dublin") |
| 1714 |
|
| 1715 |
pkl = pickle.dumps(zi_no_cache, protocol=proto) |
| 1716 |
zi_rt = pickle.loads(pkl) |
| 1717 |
|
| 1718 |
with self.subTest(test="Not the pickled object"): |
| 1719 |
self.assertIsNot(zi_rt, zi_no_cache) |
| 1720 |
|
| 1721 |
zi_rt2 = pickle.loads(pkl) |
| 1722 |
with self.subTest(test="Not a second unpickled object"): |
| 1723 |
self.assertIsNot(zi_rt, zi_rt2) |
| 1724 |
|
| 1725 |
zi_cache = self.klass("Europe/Dublin") |
| 1726 |
with self.subTest(test="Not a cached object"): |
| 1727 |
self.assertIsNot(zi_rt, zi_cache) |
| 1728 |
|
| 1729 |
def test_from_file(self): |
| 1730 |
key = "Europe/Dublin" |
| 1731 |
with open(self.zoneinfo_data.path_from_key(key), "rb") as f: |
| 1732 |
zi_nokey = self.klass.from_file(f) |
| 1733 |
|
| 1734 |
f.seek(0) |
| 1735 |
zi_key = self.klass.from_file(f, key=key) |
| 1736 |
|
| 1737 |
test_cases = [ |
| 1738 |
(zi_key, "ZoneInfo with key"), |
| 1739 |
(zi_nokey, "ZoneInfo without key"), |
| 1740 |
] |
| 1741 |
|
| 1742 |
for zi, test_name in test_cases: |
| 1743 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1744 |
with self.subTest(test_name=test_name, proto=proto): |
| 1745 |
with self.assertRaises(pickle.PicklingError): |
| 1746 |
pickle.dumps(zi, protocol=proto) |
| 1747 |
|
| 1748 |
def test_pickle_after_from_file(self): |
| 1749 |
# This may be a bit of paranoia, but this test is to ensure that no |
| 1750 |
# global state is maintained in order to handle the pickle cache and |
| 1751 |
# from_file behavior, and that it is possible to interweave the |
| 1752 |
# constructors of each of these and pickling/unpickling without issues. |
| 1753 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1754 |
with self.subTest(proto=proto): |
| 1755 |
key = "Europe/Dublin" |
| 1756 |
zi = self.klass(key) |
| 1757 |
|
| 1758 |
pkl_0 = pickle.dumps(zi, protocol=proto) |
| 1759 |
zi_rt_0 = pickle.loads(pkl_0) |
| 1760 |
self.assertIs(zi, zi_rt_0) |
| 1761 |
|
| 1762 |
with open(self.zoneinfo_data.path_from_key(key), "rb") as f: |
| 1763 |
zi_ff = self.klass.from_file(f, key=key) |
| 1764 |
|
| 1765 |
pkl_1 = pickle.dumps(zi, protocol=proto) |
| 1766 |
zi_rt_1 = pickle.loads(pkl_1) |
| 1767 |
self.assertIs(zi, zi_rt_1) |
| 1768 |
|
| 1769 |
with self.assertRaises(pickle.PicklingError): |
| 1770 |
pickle.dumps(zi_ff, protocol=proto) |
| 1771 |
|
| 1772 |
pkl_2 = pickle.dumps(zi, protocol=proto) |
| 1773 |
zi_rt_2 = pickle.loads(pkl_2) |
| 1774 |
self.assertIs(zi, zi_rt_2) |
| 1775 |
|
| 1776 |
|
| 1777 |
class CZoneInfoPickleTest(ZoneInfoPickleTest): |
| 1778 |
module = c_zoneinfo |
| 1779 |
|
| 1780 |
|
| 1781 |
class CallingConventionTest(ZoneInfoTestBase): |
| 1782 |
"""Tests for functions with restricted calling conventions.""" |
| 1783 |
|
| 1784 |
module = py_zoneinfo |
| 1785 |
|
| 1786 |
@property |
| 1787 |
def zoneinfo_data(self): |
| 1788 |
return ZONEINFO_DATA |
| 1789 |
|
| 1790 |
def test_from_file(self): |
| 1791 |
with open(self.zoneinfo_data.path_from_key("UTC"), "rb") as f: |
| 1792 |
with self.assertRaises(TypeError): |
| 1793 |
self.klass.from_file(fobj=f) |
| 1794 |
|
| 1795 |
def test_clear_cache(self): |
| 1796 |
with self.assertRaises(TypeError): |
| 1797 |
self.klass.clear_cache(["UTC"]) |
| 1798 |
|
| 1799 |
|
| 1800 |
class CCallingConventionTest(CallingConventionTest): |
| 1801 |
module = c_zoneinfo |
| 1802 |
|
| 1803 |
|
| 1804 |
class TzPathTest(TzPathUserMixin, ZoneInfoTestBase): |
| 1805 |
module = py_zoneinfo |
| 1806 |
|
| 1807 |
@staticmethod |
| 1808 |
@contextlib.contextmanager |
| 1809 |
def python_tzpath_context(value): |
| 1810 |
with EnvironmentVarGuard() as env: |
| 1811 |
env["PYTHONTZPATH"] = value |
| 1812 |
yield |
| 1813 |
|
| 1814 |
def test_env_variable(self): |
| 1815 |
"""Tests that the environment variable works with reset_tzpath.""" |
| 1816 |
new_paths = [ |
| 1817 |
("", []), |
| 1818 |
(f"{DRIVE}/etc/zoneinfo", [f"{DRIVE}/etc/zoneinfo"]), |
| 1819 |
(f"{DRIVE}/a/b/c{os.pathsep}{DRIVE}/d/e/f", [f"{DRIVE}/a/b/c", f"{DRIVE}/d/e/f"]), |
| 1820 |
] |
| 1821 |
|
| 1822 |
for new_path_var, expected_result in new_paths: |
| 1823 |
with self.python_tzpath_context(new_path_var): |
| 1824 |
with self.subTest(tzpath=new_path_var): |
| 1825 |
self.module.reset_tzpath() |
| 1826 |
tzpath = self.module.TZPATH |
| 1827 |
self.assertSequenceEqual(tzpath, expected_result) |
| 1828 |
|
| 1829 |
def test_env_variable_relative_paths(self): |
| 1830 |
test_cases = [ |
| 1831 |
[("path/to/somewhere",), ()], |
| 1832 |
[ |
| 1833 |
(f"{DRIVE}/usr/share/zoneinfo", "path/to/somewhere",), |
| 1834 |
(f"{DRIVE}/usr/share/zoneinfo",), |
| 1835 |
], |
| 1836 |
[("../relative/path",), ()], |
| 1837 |
[ |
| 1838 |
(f"{DRIVE}/usr/share/zoneinfo", "../relative/path",), |
| 1839 |
(f"{DRIVE}/usr/share/zoneinfo",), |
| 1840 |
], |
| 1841 |
[("path/to/somewhere", "../relative/path",), ()], |
| 1842 |
[ |
| 1843 |
( |
| 1844 |
f"{DRIVE}/usr/share/zoneinfo", |
| 1845 |
"path/to/somewhere", |
| 1846 |
"../relative/path", |
| 1847 |
), |
| 1848 |
(f"{DRIVE}/usr/share/zoneinfo",), |
| 1849 |
], |
| 1850 |
] |
| 1851 |
|
| 1852 |
for input_paths, expected_paths in test_cases: |
| 1853 |
path_var = os.pathsep.join(input_paths) |
| 1854 |
with self.python_tzpath_context(path_var): |
| 1855 |
with self.subTest("warning", path_var=path_var): |
| 1856 |
# Note: Per PEP 615 the warning is implementation-defined |
| 1857 |
# behavior, other implementations need not warn. |
| 1858 |
with self.assertWarns(self.module.InvalidTZPathWarning) as w: |
| 1859 |
self.module.reset_tzpath() |
| 1860 |
self.assertEqual(w.warnings[0].filename, __file__) |
| 1861 |
|
| 1862 |
tzpath = self.module.TZPATH |
| 1863 |
with self.subTest("filtered", path_var=path_var): |
| 1864 |
self.assertSequenceEqual(tzpath, expected_paths) |
| 1865 |
|
| 1866 |
def test_env_variable_relative_paths_warning_location(self): |
| 1867 |
path_var = "path/to/somewhere" |
| 1868 |
|
| 1869 |
with self.python_tzpath_context(path_var): |
| 1870 |
with CleanImport("zoneinfo", "zoneinfo._tzpath"): |
| 1871 |
with self.assertWarns(RuntimeWarning) as w: |
| 1872 |
import zoneinfo |
| 1873 |
InvalidTZPathWarning = zoneinfo.InvalidTZPathWarning |
| 1874 |
self.assertIsInstance(w.warnings[0].message, InvalidTZPathWarning) |
| 1875 |
# It should represent the current file: |
| 1876 |
self.assertEqual(w.warnings[0].filename, __file__) |
| 1877 |
|
| 1878 |
def test_reset_tzpath_kwarg(self): |
| 1879 |
self.module.reset_tzpath(to=[f"{DRIVE}/a/b/c"]) |
| 1880 |
|
| 1881 |
self.assertSequenceEqual(self.module.TZPATH, (f"{DRIVE}/a/b/c",)) |
| 1882 |
|
| 1883 |
def test_reset_tzpath_relative_paths(self): |
| 1884 |
bad_values = [ |
| 1885 |
("path/to/somewhere",), |
| 1886 |
("/usr/share/zoneinfo", "path/to/somewhere",), |
| 1887 |
("../relative/path",), |
| 1888 |
("/usr/share/zoneinfo", "../relative/path",), |
| 1889 |
("path/to/somewhere", "../relative/path",), |
| 1890 |
("/usr/share/zoneinfo", "path/to/somewhere", "../relative/path",), |
| 1891 |
(FakePath("path/to/somewhere"),) |
| 1892 |
] |
| 1893 |
for input_paths in bad_values: |
| 1894 |
with self.subTest(input_paths=input_paths): |
| 1895 |
with self.assertRaises(ValueError): |
| 1896 |
self.module.reset_tzpath(to=input_paths) |
| 1897 |
|
| 1898 |
def test_tzpath_type_error(self): |
| 1899 |
bad_values = [ |
| 1900 |
"/etc/zoneinfo:/usr/share/zoneinfo", |
| 1901 |
b"/etc/zoneinfo:/usr/share/zoneinfo", |
| 1902 |
0, |
| 1903 |
(b"/bytes/path", "/valid/path"), |
| 1904 |
(FakePath(b"/bytes/path"),), |
| 1905 |
(0,), |
| 1906 |
] |
| 1907 |
|
| 1908 |
for bad_value in bad_values: |
| 1909 |
with self.subTest(value=bad_value): |
| 1910 |
with self.assertRaises(TypeError): |
| 1911 |
self.module.reset_tzpath(bad_value) |
| 1912 |
|
| 1913 |
def test_tzpath_attribute(self): |
| 1914 |
tzpath_0 = [f"{DRIVE}/one", f"{DRIVE}/two"] |
| 1915 |
tzpath_1 = [f"{DRIVE}/three"] |
| 1916 |
tzpath_pathlike = (FakePath(f"{DRIVE}/usr/share/zoneinfo"),) |
| 1917 |
|
| 1918 |
with self.tzpath_context(tzpath_0): |
| 1919 |
query_0 = self.module.TZPATH |
| 1920 |
|
| 1921 |
with self.tzpath_context(tzpath_1): |
| 1922 |
query_1 = self.module.TZPATH |
| 1923 |
|
| 1924 |
with self.tzpath_context(tzpath_pathlike): |
| 1925 |
query_pathlike = self.module.TZPATH |
| 1926 |
|
| 1927 |
self.assertSequenceEqual(tzpath_0, query_0) |
| 1928 |
self.assertSequenceEqual(tzpath_1, query_1) |
| 1929 |
self.assertSequenceEqual(tuple([os.fspath(p) for p in tzpath_pathlike]), query_pathlike) |
| 1930 |
|
| 1931 |
|
| 1932 |
class CTzPathTest(TzPathTest): |
| 1933 |
module = c_zoneinfo |
| 1934 |
|
| 1935 |
|
| 1936 |
class TestModule(ZoneInfoTestBase): |
| 1937 |
module = py_zoneinfo |
| 1938 |
|
| 1939 |
@property |
| 1940 |
def zoneinfo_data(self): |
| 1941 |
return ZONEINFO_DATA |
| 1942 |
|
| 1943 |
@cached_property |
| 1944 |
def _UTC_bytes(self): |
| 1945 |
zone_file = self.zoneinfo_data.path_from_key("UTC") |
| 1946 |
with open(zone_file, "rb") as f: |
| 1947 |
return f.read() |
| 1948 |
|
| 1949 |
def touch_zone(self, key, tz_root): |
| 1950 |
"""Creates a valid TZif file at key under the zoneinfo root tz_root. |
| 1951 |
|
| 1952 |
tz_root must exist, but all folders below that will be created. |
| 1953 |
""" |
| 1954 |
if not os.path.exists(tz_root): |
| 1955 |
raise FileNotFoundError(f"{tz_root} does not exist.") |
| 1956 |
|
| 1957 |
root_dir, *tail = key.rsplit("/", 1) |
| 1958 |
if tail: # If there's no tail, then the first component isn't a dir |
| 1959 |
os.makedirs(os.path.join(tz_root, root_dir), exist_ok=True) |
| 1960 |
|
| 1961 |
zonefile_path = os.path.join(tz_root, key) |
| 1962 |
with open(zonefile_path, "wb") as f: |
| 1963 |
f.write(self._UTC_bytes) |
| 1964 |
|
| 1965 |
def test_getattr_error(self): |
| 1966 |
with self.assertRaises(AttributeError): |
| 1967 |
self.module.NOATTRIBUTE |
| 1968 |
|
| 1969 |
def test_dir_contains_all(self): |
| 1970 |
"""dir(self.module) should at least contain everything in __all__.""" |
| 1971 |
module_all_set = set(self.module.__all__) |
| 1972 |
module_dir_set = set(dir(self.module)) |
| 1973 |
|
| 1974 |
difference = module_all_set - module_dir_set |
| 1975 |
|
| 1976 |
self.assertFalse(difference) |
| 1977 |
|
| 1978 |
def test_dir_unique(self): |
| 1979 |
"""Test that there are no duplicates in dir(self.module)""" |
| 1980 |
module_dir = dir(self.module) |
| 1981 |
module_unique = set(module_dir) |
| 1982 |
|
| 1983 |
self.assertCountEqual(module_dir, module_unique) |
| 1984 |
|
| 1985 |
def test_available_timezones(self): |
| 1986 |
with self.tzpath_context([self.zoneinfo_data.tzpath]): |
| 1987 |
self.assertTrue(self.zoneinfo_data.keys) # Sanity check |
| 1988 |
|
| 1989 |
available_keys = self.module.available_timezones() |
| 1990 |
zoneinfo_keys = set(self.zoneinfo_data.keys) |
| 1991 |
|
| 1992 |
# If tzdata is not present, zoneinfo_keys == available_keys, |
| 1993 |
# otherwise it should be a subset. |
| 1994 |
union = zoneinfo_keys & available_keys |
| 1995 |
self.assertEqual(zoneinfo_keys, union) |
| 1996 |
|
| 1997 |
def test_available_timezones_weirdzone(self): |
| 1998 |
with tempfile.TemporaryDirectory() as td: |
| 1999 |
# Make a fictional zone at "Mars/Olympus_Mons" |
| 2000 |
self.touch_zone("Mars/Olympus_Mons", td) |
| 2001 |
|
| 2002 |
with self.tzpath_context([td]): |
| 2003 |
available_keys = self.module.available_timezones() |
| 2004 |
self.assertIn("Mars/Olympus_Mons", available_keys) |
| 2005 |
|
| 2006 |
def test_folder_exclusions(self): |
| 2007 |
expected = { |
| 2008 |
"America/Los_Angeles", |
| 2009 |
"America/Santiago", |
| 2010 |
"America/Indiana/Indianapolis", |
| 2011 |
"UTC", |
| 2012 |
"Europe/Paris", |
| 2013 |
"Europe/London", |
| 2014 |
"Asia/Tokyo", |
| 2015 |
"Australia/Sydney", |
| 2016 |
} |
| 2017 |
|
| 2018 |
base_tree = list(expected) |
| 2019 |
posix_tree = [f"posix/{x}" for x in base_tree] |
| 2020 |
right_tree = [f"right/{x}" for x in base_tree] |
| 2021 |
|
| 2022 |
cases = [ |
| 2023 |
("base_tree", base_tree), |
| 2024 |
("base_and_posix", base_tree + posix_tree), |
| 2025 |
("base_and_right", base_tree + right_tree), |
| 2026 |
("all_trees", base_tree + right_tree + posix_tree), |
| 2027 |
] |
| 2028 |
|
| 2029 |
with tempfile.TemporaryDirectory() as td: |
| 2030 |
for case_name, tree in cases: |
| 2031 |
tz_root = os.path.join(td, case_name) |
| 2032 |
os.mkdir(tz_root) |
| 2033 |
|
| 2034 |
for key in tree: |
| 2035 |
self.touch_zone(key, tz_root) |
| 2036 |
|
| 2037 |
with self.tzpath_context([tz_root]): |
| 2038 |
with self.subTest(case_name): |
| 2039 |
actual = self.module.available_timezones() |
| 2040 |
self.assertEqual(actual, expected) |
| 2041 |
|
| 2042 |
def test_exclude_posixrules(self): |
| 2043 |
expected = { |
| 2044 |
"America/New_York", |
| 2045 |
"Europe/London", |
| 2046 |
} |
| 2047 |
|
| 2048 |
tree = list(expected) + ["posixrules"] |
| 2049 |
|
| 2050 |
with tempfile.TemporaryDirectory() as td: |
| 2051 |
for key in tree: |
| 2052 |
self.touch_zone(key, td) |
| 2053 |
|
| 2054 |
with self.tzpath_context([td]): |
| 2055 |
actual = self.module.available_timezones() |
| 2056 |
self.assertEqual(actual, expected) |
| 2057 |
|
| 2058 |
|
| 2059 |
class CTestModule(TestModule): |
| 2060 |
module = c_zoneinfo |
| 2061 |
|
| 2062 |
|
| 2063 |
class MiscTests(unittest.TestCase): |
| 2064 |
def test_pydatetime(self): |
| 2065 |
# Test that zoneinfo works if the C implementation of datetime |
| 2066 |
# is not available and the Python implementation of datetime is used. |
| 2067 |
# The Python implementation of zoneinfo should be used in thet case. |
| 2068 |
# |
| 2069 |
# Run the test in a subprocess, as importing _zoneinfo with |
| 2070 |
# _datettime disabled causes crash in the previously imported |
| 2071 |
# _zoneinfo. |
| 2072 |
assert_python_ok('-c', '''if 1: |
| 2073 |
import sys |
| 2074 |
sys.modules['_datetime'] = None |
| 2075 |
import datetime |
| 2076 |
import zoneinfo |
| 2077 |
tzinfo = zoneinfo.ZoneInfo('Europe/London') |
| 2078 |
datetime.datetime(2025, 10, 26, 2, 0, tzinfo=tzinfo) |
| 2079 |
''', |
| 2080 |
PYTHONTZPATH=str(ZONEINFO_DATA.tzpath)) |
| 2081 |
|
| 2082 |
|
| 2083 |
class ExtensionBuiltTest(unittest.TestCase): |
| 2084 |
"""Smoke test to ensure that the C and Python extensions are both tested. |
| 2085 |
|
| 2086 |
Because the intention is for the Python and C versions of ZoneInfo to |
| 2087 |
behave identically, these tests necessarily rely on implementation details, |
| 2088 |
so the tests may need to be adjusted if the implementations change. Do not |
| 2089 |
rely on these tests as an indication of stable properties of these classes. |
| 2090 |
""" |
| 2091 |
|
| 2092 |
def test_cache_location(self): |
| 2093 |
# The pure Python version stores caches on attributes, but the C |
| 2094 |
# extension stores them in C globals (at least for now) |
| 2095 |
self.assertNotHasAttr(c_zoneinfo.ZoneInfo, "_weak_cache") |
| 2096 |
self.assertHasAttr(py_zoneinfo.ZoneInfo, "_weak_cache") |
| 2097 |
|
| 2098 |
def test_gc_tracked(self): |
| 2099 |
import gc |
| 2100 |
|
| 2101 |
self.assertTrue(gc.is_tracked(py_zoneinfo.ZoneInfo)) |
| 2102 |
self.assertTrue(gc.is_tracked(c_zoneinfo.ZoneInfo)) |
| 2103 |
|
| 2104 |
|
| 2105 |
@dataclasses.dataclass(frozen=True) |
| 2106 |
class ZoneOffset: |
| 2107 |
tzname: str |
| 2108 |
utcoffset: timedelta |
| 2109 |
dst: timedelta = ZERO |
| 2110 |
|
| 2111 |
|
| 2112 |
@dataclasses.dataclass(frozen=True) |
| 2113 |
class ZoneTransition: |
| 2114 |
transition: datetime |
| 2115 |
offset_before: ZoneOffset |
| 2116 |
offset_after: ZoneOffset |
| 2117 |
|
| 2118 |
@property |
| 2119 |
def transition_utc(self): |
| 2120 |
return (self.transition - self.offset_before.utcoffset).replace( |
| 2121 |
tzinfo=timezone.utc |
| 2122 |
) |
| 2123 |
|
| 2124 |
@property |
| 2125 |
def fold(self): |
| 2126 |
"""Whether this introduces a fold""" |
| 2127 |
return self.offset_before.utcoffset > self.offset_after.utcoffset |
| 2128 |
|
| 2129 |
@property |
| 2130 |
def gap(self): |
| 2131 |
"""Whether this introduces a gap""" |
| 2132 |
return self.offset_before.utcoffset < self.offset_after.utcoffset |
| 2133 |
|
| 2134 |
@property |
| 2135 |
def delta(self): |
| 2136 |
return self.offset_after.utcoffset - self.offset_before.utcoffset |
| 2137 |
|
| 2138 |
@property |
| 2139 |
def anomaly_start(self): |
| 2140 |
if self.fold: |
| 2141 |
return self.transition + self.delta |
| 2142 |
else: |
| 2143 |
return self.transition |
| 2144 |
|
| 2145 |
@property |
| 2146 |
def anomaly_end(self): |
| 2147 |
if not self.fold: |
| 2148 |
return self.transition + self.delta |
| 2149 |
else: |
| 2150 |
return self.transition |
| 2151 |
|
| 2152 |
|
| 2153 |
class ZoneInfoData: |
| 2154 |
def __init__(self, source_json, tzpath, v1=False): |
| 2155 |
self.tzpath = pathlib.Path(tzpath) |
| 2156 |
self.keys = [] |
| 2157 |
self.v1 = v1 |
| 2158 |
self._populate_tzpath(source_json) |
| 2159 |
|
| 2160 |
def path_from_key(self, key): |
| 2161 |
return self.tzpath / key |
| 2162 |
|
| 2163 |
def _populate_tzpath(self, source_json): |
| 2164 |
with open(source_json, "rb") as f: |
| 2165 |
zoneinfo_dict = json.load(f) |
| 2166 |
|
| 2167 |
zoneinfo_data = zoneinfo_dict["data"] |
| 2168 |
|
| 2169 |
for key, value in zoneinfo_data.items(): |
| 2170 |
self.keys.append(key) |
| 2171 |
raw_data = self._decode_text(value) |
| 2172 |
|
| 2173 |
if self.v1: |
| 2174 |
data = self._convert_to_v1(raw_data) |
| 2175 |
else: |
| 2176 |
data = raw_data |
| 2177 |
|
| 2178 |
destination = self.path_from_key(key) |
| 2179 |
destination.parent.mkdir(exist_ok=True, parents=True) |
| 2180 |
with open(destination, "wb") as f: |
| 2181 |
f.write(data) |
| 2182 |
|
| 2183 |
def _decode_text(self, contents): |
| 2184 |
raw_data = b"".join(map(str.encode, contents)) |
| 2185 |
decoded = base64.b85decode(raw_data) |
| 2186 |
|
| 2187 |
return lzma.decompress(decoded) |
| 2188 |
|
| 2189 |
def _convert_to_v1(self, contents): |
| 2190 |
assert contents[0:4] == b"TZif", "Invalid TZif data found!" |
| 2191 |
version = int(contents[4:5]) |
| 2192 |
|
| 2193 |
header_start = 4 + 16 |
| 2194 |
header_end = header_start + 24 # 6l == 24 bytes |
| 2195 |
assert version >= 2, "Version 1 file found: no conversion necessary" |
| 2196 |
isutcnt, isstdcnt, leapcnt, timecnt, typecnt, charcnt = struct.unpack( |
| 2197 |
">6l", contents[header_start:header_end] |
| 2198 |
) |
| 2199 |
|
| 2200 |
file_size = ( |
| 2201 |
timecnt * 5 |
| 2202 |
+ typecnt * 6 |
| 2203 |
+ charcnt |
| 2204 |
+ leapcnt * 8 |
| 2205 |
+ isstdcnt |
| 2206 |
+ isutcnt |
| 2207 |
) |
| 2208 |
file_size += header_end |
| 2209 |
out = b"TZif" + b"\x00" + contents[5:file_size] |
| 2210 |
|
| 2211 |
assert ( |
| 2212 |
contents[file_size : (file_size + 4)] == b"TZif" |
| 2213 |
), "Version 2 file not truncated at Version 2 header" |
| 2214 |
|
| 2215 |
return out |
| 2216 |
|
| 2217 |
|
| 2218 |
class ZoneDumpData: |
| 2219 |
@classmethod |
| 2220 |
def transition_keys(cls): |
| 2221 |
return cls._get_zonedump().keys() |
| 2222 |
|
| 2223 |
@classmethod |
| 2224 |
def load_transition_examples(cls, key): |
| 2225 |
return cls._get_zonedump()[key] |
| 2226 |
|
| 2227 |
@classmethod |
| 2228 |
def fixed_offset_zones(cls): |
| 2229 |
if not cls._FIXED_OFFSET_ZONES: |
| 2230 |
cls._populate_fixed_offsets() |
| 2231 |
|
| 2232 |
return cls._FIXED_OFFSET_ZONES.items() |
| 2233 |
|
| 2234 |
@classmethod |
| 2235 |
def _get_zonedump(cls): |
| 2236 |
if not cls._ZONEDUMP_DATA: |
| 2237 |
cls._populate_zonedump_data() |
| 2238 |
return cls._ZONEDUMP_DATA |
| 2239 |
|
| 2240 |
@classmethod |
| 2241 |
def _populate_fixed_offsets(cls): |
| 2242 |
cls._FIXED_OFFSET_ZONES = { |
| 2243 |
"UTC": ZoneOffset("UTC", ZERO, ZERO), |
| 2244 |
} |
| 2245 |
|
| 2246 |
@classmethod |
| 2247 |
def _populate_zonedump_data(cls): |
| 2248 |
def _Africa_Abidjan(): |
| 2249 |
LMT = ZoneOffset("LMT", timedelta(seconds=-968)) |
| 2250 |
GMT = ZoneOffset("GMT", ZERO) |
| 2251 |
|
| 2252 |
return [ |
| 2253 |
ZoneTransition(datetime(1912, 1, 1), LMT, GMT), |
| 2254 |
] |
| 2255 |
|
| 2256 |
def _Africa_Casablanca(): |
| 2257 |
P00_s = ZoneOffset("+00", ZERO, ZERO) |
| 2258 |
P01_d = ZoneOffset("+01", ONE_H, ONE_H) |
| 2259 |
P00_d = ZoneOffset("+00", ZERO, -ONE_H) |
| 2260 |
P01_s = ZoneOffset("+01", ONE_H, ZERO) |
| 2261 |
|
| 2262 |
return [ |
| 2263 |
# Morocco sometimes pauses DST during Ramadan |
| 2264 |
ZoneTransition(datetime(2018, 3, 25, 2), P00_s, P01_d), |
| 2265 |
ZoneTransition(datetime(2018, 5, 13, 3), P01_d, P00_s), |
| 2266 |
ZoneTransition(datetime(2018, 6, 17, 2), P00_s, P01_d), |
| 2267 |
# On October 28th Morocco set standard time to +01, |
| 2268 |
# with negative DST only during Ramadan |
| 2269 |
ZoneTransition(datetime(2018, 10, 28, 3), P01_d, P01_s), |
| 2270 |
ZoneTransition(datetime(2019, 5, 5, 3), P01_s, P00_d), |
| 2271 |
ZoneTransition(datetime(2019, 6, 9, 2), P00_d, P01_s), |
| 2272 |
] |
| 2273 |
|
| 2274 |
def _America_Los_Angeles(): |
| 2275 |
LMT = ZoneOffset("LMT", timedelta(seconds=-28378), ZERO) |
| 2276 |
PST = ZoneOffset("PST", timedelta(hours=-8), ZERO) |
| 2277 |
PDT = ZoneOffset("PDT", timedelta(hours=-7), ONE_H) |
| 2278 |
PWT = ZoneOffset("PWT", timedelta(hours=-7), ONE_H) |
| 2279 |
PPT = ZoneOffset("PPT", timedelta(hours=-7), ONE_H) |
| 2280 |
|
| 2281 |
return [ |
| 2282 |
ZoneTransition(datetime(1883, 11, 18, 12, 7, 2), LMT, PST), |
| 2283 |
ZoneTransition(datetime(1918, 3, 31, 2), PST, PDT), |
| 2284 |
ZoneTransition(datetime(1918, 3, 31, 2), PST, PDT), |
| 2285 |
ZoneTransition(datetime(1918, 10, 27, 2), PDT, PST), |
| 2286 |
# Transition to Pacific War Time |
| 2287 |
ZoneTransition(datetime(1942, 2, 9, 2), PST, PWT), |
| 2288 |
# Transition from Pacific War Time to Pacific Peace Time |
| 2289 |
ZoneTransition(datetime(1945, 8, 14, 16), PWT, PPT), |
| 2290 |
ZoneTransition(datetime(1945, 9, 30, 2), PPT, PST), |
| 2291 |
ZoneTransition(datetime(2015, 3, 8, 2), PST, PDT), |
| 2292 |
ZoneTransition(datetime(2015, 11, 1, 2), PDT, PST), |
| 2293 |
# After 2038: Rules continue indefinitely |
| 2294 |
ZoneTransition(datetime(2450, 3, 13, 2), PST, PDT), |
| 2295 |
ZoneTransition(datetime(2450, 11, 6, 2), PDT, PST), |
| 2296 |
] |
| 2297 |
|
| 2298 |
def _America_Santiago(): |
| 2299 |
LMT = ZoneOffset("LMT", timedelta(seconds=-16966), ZERO) |
| 2300 |
SMT = ZoneOffset("SMT", timedelta(seconds=-16966), ZERO) |
| 2301 |
N05 = ZoneOffset("-05", timedelta(seconds=-18000), ZERO) |
| 2302 |
N04 = ZoneOffset("-04", timedelta(seconds=-14400), ZERO) |
| 2303 |
N03 = ZoneOffset("-03", timedelta(seconds=-10800), ONE_H) |
| 2304 |
|
| 2305 |
return [ |
| 2306 |
ZoneTransition(datetime(1890, 1, 1), LMT, SMT), |
| 2307 |
ZoneTransition(datetime(1910, 1, 10), SMT, N05), |
| 2308 |
ZoneTransition(datetime(1916, 7, 1), N05, SMT), |
| 2309 |
ZoneTransition(datetime(2008, 3, 30), N03, N04), |
| 2310 |
ZoneTransition(datetime(2008, 10, 12), N04, N03), |
| 2311 |
ZoneTransition(datetime(2040, 4, 8), N03, N04), |
| 2312 |
ZoneTransition(datetime(2040, 9, 2), N04, N03), |
| 2313 |
] |
| 2314 |
|
| 2315 |
def _Asia_Tokyo(): |
| 2316 |
JST = ZoneOffset("JST", timedelta(seconds=32400), ZERO) |
| 2317 |
JDT = ZoneOffset("JDT", timedelta(seconds=36000), ONE_H) |
| 2318 |
|
| 2319 |
# Japan had DST from 1948 to 1951, and it was unusual in that |
| 2320 |
# the transition from DST to STD occurred at 25:00, and is |
| 2321 |
# denominated as such in the time zone database |
| 2322 |
return [ |
| 2323 |
ZoneTransition(datetime(1948, 5, 2), JST, JDT), |
| 2324 |
ZoneTransition(datetime(1948, 9, 12, 1), JDT, JST), |
| 2325 |
ZoneTransition(datetime(1951, 9, 9, 1), JDT, JST), |
| 2326 |
] |
| 2327 |
|
| 2328 |
def _Australia_Sydney(): |
| 2329 |
LMT = ZoneOffset("LMT", timedelta(seconds=36292), ZERO) |
| 2330 |
AEST = ZoneOffset("AEST", timedelta(seconds=36000), ZERO) |
| 2331 |
AEDT = ZoneOffset("AEDT", timedelta(seconds=39600), ONE_H) |
| 2332 |
|
| 2333 |
return [ |
| 2334 |
ZoneTransition(datetime(1895, 2, 1), LMT, AEST), |
| 2335 |
ZoneTransition(datetime(1917, 1, 1, 0, 1), AEST, AEDT), |
| 2336 |
ZoneTransition(datetime(1917, 3, 25, 2), AEDT, AEST), |
| 2337 |
ZoneTransition(datetime(2012, 4, 1, 3), AEDT, AEST), |
| 2338 |
ZoneTransition(datetime(2012, 10, 7, 2), AEST, AEDT), |
| 2339 |
ZoneTransition(datetime(2040, 4, 1, 3), AEDT, AEST), |
| 2340 |
ZoneTransition(datetime(2040, 10, 7, 2), AEST, AEDT), |
| 2341 |
] |
| 2342 |
|
| 2343 |
def _Europe_Dublin(): |
| 2344 |
LMT = ZoneOffset("LMT", timedelta(seconds=-1500), ZERO) |
| 2345 |
DMT = ZoneOffset("DMT", timedelta(seconds=-1521), ZERO) |
| 2346 |
IST_0 = ZoneOffset("IST", timedelta(seconds=2079), ONE_H) |
| 2347 |
GMT_0 = ZoneOffset("GMT", ZERO, ZERO) |
| 2348 |
BST = ZoneOffset("BST", ONE_H, ONE_H) |
| 2349 |
GMT_1 = ZoneOffset("GMT", ZERO, -ONE_H) |
| 2350 |
IST_1 = ZoneOffset("IST", ONE_H, ZERO) |
| 2351 |
|
| 2352 |
return [ |
| 2353 |
ZoneTransition(datetime(1880, 8, 2, 0), LMT, DMT), |
| 2354 |
ZoneTransition(datetime(1916, 5, 21, 2), DMT, IST_0), |
| 2355 |
ZoneTransition(datetime(1916, 10, 1, 3), IST_0, GMT_0), |
| 2356 |
ZoneTransition(datetime(1917, 4, 8, 2), GMT_0, BST), |
| 2357 |
ZoneTransition(datetime(2016, 3, 27, 1), GMT_1, IST_1), |
| 2358 |
ZoneTransition(datetime(2016, 10, 30, 2), IST_1, GMT_1), |
| 2359 |
ZoneTransition(datetime(2487, 3, 30, 1), GMT_1, IST_1), |
| 2360 |
ZoneTransition(datetime(2487, 10, 26, 2), IST_1, GMT_1), |
| 2361 |
] |
| 2362 |
|
| 2363 |
def _Europe_Lisbon(): |
| 2364 |
WET = ZoneOffset("WET", ZERO, ZERO) |
| 2365 |
WEST = ZoneOffset("WEST", ONE_H, ONE_H) |
| 2366 |
CET = ZoneOffset("CET", ONE_H, ZERO) |
| 2367 |
CEST = ZoneOffset("CEST", timedelta(seconds=7200), ONE_H) |
| 2368 |
|
| 2369 |
return [ |
| 2370 |
ZoneTransition(datetime(1992, 3, 29, 1), WET, WEST), |
| 2371 |
ZoneTransition(datetime(1992, 9, 27, 2), WEST, CET), |
| 2372 |
ZoneTransition(datetime(1993, 3, 28, 2), CET, CEST), |
| 2373 |
ZoneTransition(datetime(1993, 9, 26, 3), CEST, CET), |
| 2374 |
ZoneTransition(datetime(1996, 3, 31, 2), CET, WEST), |
| 2375 |
ZoneTransition(datetime(1996, 10, 27, 2), WEST, WET), |
| 2376 |
] |
| 2377 |
|
| 2378 |
def _Europe_London(): |
| 2379 |
LMT = ZoneOffset("LMT", timedelta(seconds=-75), ZERO) |
| 2380 |
GMT = ZoneOffset("GMT", ZERO, ZERO) |
| 2381 |
BST = ZoneOffset("BST", ONE_H, ONE_H) |
| 2382 |
|
| 2383 |
return [ |
| 2384 |
ZoneTransition(datetime(1847, 12, 1), LMT, GMT), |
| 2385 |
ZoneTransition(datetime(2005, 3, 27, 1), GMT, BST), |
| 2386 |
ZoneTransition(datetime(2005, 10, 30, 2), BST, GMT), |
| 2387 |
ZoneTransition(datetime(2043, 3, 29, 1), GMT, BST), |
| 2388 |
ZoneTransition(datetime(2043, 10, 25, 2), BST, GMT), |
| 2389 |
] |
| 2390 |
|
| 2391 |
def _Pacific_Kiritimati(): |
| 2392 |
LMT = ZoneOffset("LMT", timedelta(seconds=-37760), ZERO) |
| 2393 |
N1040 = ZoneOffset("-1040", timedelta(seconds=-38400), ZERO) |
| 2394 |
N10 = ZoneOffset("-10", timedelta(seconds=-36000), ZERO) |
| 2395 |
P14 = ZoneOffset("+14", timedelta(seconds=50400), ZERO) |
| 2396 |
|
| 2397 |
# This is literally every transition in Christmas Island history |
| 2398 |
return [ |
| 2399 |
ZoneTransition(datetime(1901, 1, 1), LMT, N1040), |
| 2400 |
ZoneTransition(datetime(1979, 10, 1), N1040, N10), |
| 2401 |
# They skipped December 31, 1994 |
| 2402 |
ZoneTransition(datetime(1994, 12, 31), N10, P14), |
| 2403 |
] |
| 2404 |
|
| 2405 |
cls._ZONEDUMP_DATA = { |
| 2406 |
"Africa/Abidjan": _Africa_Abidjan(), |
| 2407 |
"Africa/Casablanca": _Africa_Casablanca(), |
| 2408 |
"America/Los_Angeles": _America_Los_Angeles(), |
| 2409 |
"America/Santiago": _America_Santiago(), |
| 2410 |
"Australia/Sydney": _Australia_Sydney(), |
| 2411 |
"Asia/Tokyo": _Asia_Tokyo(), |
| 2412 |
"Europe/Dublin": _Europe_Dublin(), |
| 2413 |
"Europe/Lisbon": _Europe_Lisbon(), |
| 2414 |
"Europe/London": _Europe_London(), |
| 2415 |
"Pacific/Kiritimati": _Pacific_Kiritimati(), |
| 2416 |
} |
| 2417 |
|
| 2418 |
_ZONEDUMP_DATA = None |
| 2419 |
_FIXED_OFFSET_ZONES = None |
| 2420 |
|
| 2421 |
|
| 2422 |
if __name__ == '__main__': |
| 2423 |
unittest.main() |