| 1 |
"""Cross-check dateparser's timezone-abbreviation table against the tz database. |
| 2 |
|
| 3 |
Many timezone abbreviations are genuinely ambiguous: several IANA tz database |
| 4 |
zones use the same letters for different UTC offsets ("CST" is both US Central |
| 5 |
Standard Time and China Standard Time). The tz database provides no preferred |
| 6 |
answer for those, so ``dateparser/timezones.py`` keeps one arbitrary offset per |
| 7 |
abbreviation, originally taken from a Wikipedia scrape. |
| 8 |
|
| 9 |
Some abbreviations are *not* ambiguous, though: every zone the tz database |
| 10 |
names with those letters agrees on a single UTC offset. When dateparser's table |
| 11 |
disagrees with that offset, its value is not "another valid reading" -- no zone |
| 12 |
goes by that name at that offset. That was the case for "BST", which resolved |
| 13 |
to +11 although the only zones named "BST" today are the UK ones, at +1 (see |
| 14 |
#1321 and #1322). This script finds every such case generically, so the table |
| 15 |
can be kept in sync without a one-off patch per abbreviation. |
| 16 |
|
| 17 |
Two kinds of conflict are reported: |
| 18 |
|
| 19 |
``unambiguous`` |
| 20 |
The tz database maps the abbreviation to exactly one offset and dateparser |
| 21 |
disagrees. The tz database offset should be preferred. |
| 22 |
``unsupported`` |
| 23 |
The tz database maps the abbreviation to several offsets and dateparser's |
| 24 |
value is none of them. There is no single offset to prefer, so this needs a |
| 25 |
human decision, but the current value is still unsupported by any zone. |
| 26 |
|
| 27 |
Zone data comes from ``pytz``, which is already a dateparser dependency and |
| 28 |
bundles its own copy of the tz database. That keeps results identical on every |
| 29 |
platform, unlike the standard library ``zoneinfo`` module, whose data comes |
| 30 |
from the operating system and is missing entirely on Windows unless the |
| 31 |
separate ``tzdata`` package is installed. |
| 32 |
|
| 33 |
What this can and cannot see |
| 34 |
---------------------------- |
| 35 |
|
| 36 |
Only the abbreviations the tz database still prints are checked, around 50 of |
| 37 |
the 400-odd in dateparser's table. The rest are invisible here and are left to |
| 38 |
dateparser: |
| 39 |
|
| 40 |
* Modern tz database releases name a zone after its offset ("+11", "-0930") |
| 41 |
unless the abbreviation is in common English use, so a zone can drop out of |
| 42 |
this comparison without the abbreviation falling out of use. "BST" for |
| 43 |
Bougainville is exactly that: the zone now prints "+11". What the check |
| 44 |
really shows is that no zone *is named* "BST" at +11 any more, which is the |
| 45 |
practical question for a parser. |
| 46 |
* Abbreviations no zone uses in the reference window at all, such as ``AHST`` |
| 47 |
or ``LMT``, are kept by dateparser so that historical text still parses. |
| 48 |
* pytz ships ``MET`` as a copy of ``CET``, so it prints "CET"/"CEST" and the |
| 49 |
``MET``/``MEST`` abbreviations never reach this comparison. |
| 50 |
|
| 51 |
Run it to print a report; it exits non-zero when it finds anything:: |
| 52 |
|
| 53 |
python -m dateparser_scripts.tz_abbreviation_conflicts |
| 54 |
""" |
| 55 |
|
| 56 |
from collections import namedtuple |
| 57 |
from datetime import datetime |
| 58 |
|
| 59 |
import pytz |
| 60 |
import regex as re |
| 61 |
|
| 62 |
from dateparser.timezones import timezone_info_list |
| 63 |
|
| 64 |
# Years sampled to determine what an abbreviation means *today*. Both bounds |
| 65 |
# are load-bearing, and the window is pinned deliberately, like the CLDR |
| 66 |
# version in ``dateparser_scripts/utils.py``. |
| 67 |
# |
| 68 |
# The upper bound stays in settled years: the tz database records future |
| 69 |
# daylight-saving rules as predictions that change between releases, so |
| 70 |
# sampling them would make the result depend on the installed tz database |
| 71 |
# version rather than on real usage. |
| 72 |
# |
| 73 |
# The lower bound keeps retired meanings out. Slide this window forward when |
| 74 |
# refreshing the table, and do not widen it backwards: reaching further back |
| 75 |
# re-admits abbreviations the tz database has since dropped and reports them as |
| 76 |
# if they were current. Reaching back to 1999 picks up Guam's old "GST", which |
| 77 |
# would report dateparser's "GST" (+4, Gulf Standard Time) as an unambiguous |
| 78 |
# conflict against +10, even though Guam has used "ChST" since 2000. |
| 79 |
REFERENCE_YEARS = range(2021, 2026) |
| 80 |
|
| 81 |
# Sampling January, April, July and October catches both the standard-time and |
| 82 |
# the daylight-saving abbreviation of every zone, in either hemisphere. |
| 83 |
REFERENCE_MONTHS = (1, 4, 7, 10) |
| 84 |
|
| 85 |
# The tz database uses names such as "+11" or "-0930" for zones that have no |
| 86 |
# real abbreviation. They are not abbreviations, and dateparser matches numeric |
| 87 |
# offsets through separate UTC/GMT patterns, so they are ignored here. |
| 88 |
_NUMERIC_ZONE_NAME = re.compile(r"[+-]\d{2,4}") |
| 89 |
|
| 90 |
Conflict = namedtuple( |
| 91 |
"Conflict", ["abbreviation", "kind", "dateparser_offset", "tz_database_offsets"] |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
def static_tz_abbreviations(): |
| 96 |
"""Return ``{abbreviation: offset_in_seconds}`` as dateparser resolves it. |
| 97 |
|
| 98 |
A few abbreviations are listed more than once in ``timezone_info_list`` |
| 99 |
(``LMT`` appears four times with four different offsets). Only the first |
| 100 |
entry can ever win: ``build_tz_offsets`` keeps the table order and |
| 101 |
``pop_tz_offset_from_string`` returns the first pattern that matches. So |
| 102 |
the first entry, not the set of all of them, is what this comparison has |
| 103 |
to use -- otherwise a wrong first entry would be masked by a correct later |
| 104 |
one. |
| 105 |
|
| 106 |
Keys are upper-cased because dateparser matches abbreviations |
| 107 |
case-insensitively, so a differently-cased entry such as ``ChST`` is the |
| 108 |
same abbreviation and has to be compared as one. |
| 109 |
""" |
| 110 |
effective = {} |
| 111 |
for group in timezone_info_list: |
| 112 |
for name, offset in group["timezones"]: |
| 113 |
effective.setdefault(name.upper(), offset) |
| 114 |
return effective |
| 115 |
|
| 116 |
|
| 117 |
def tz_database_abbreviations(): |
| 118 |
"""Return ``{abbreviation: {offset_in_seconds, ...}}`` from the tz database. |
| 119 |
|
| 120 |
Every zone is sampled, including the deprecated aliases (``US/Pacific``, |
| 121 |
``Asia/Calcutta``) that ``pytz.all_timezones`` lists alongside canonical |
| 122 |
zones. Aliases resolve to the same rules as the zones they link to, so they |
| 123 |
only ever repeat offsets a canonical zone already contributed; including |
| 124 |
them costs nothing and keeps the scan independent of how pytz classifies |
| 125 |
any individual zone. |
| 126 |
""" |
| 127 |
samples = [ |
| 128 |
datetime(year, month, 15, 12, 0, 0) |
| 129 |
for year in REFERENCE_YEARS |
| 130 |
for month in REFERENCE_MONTHS |
| 131 |
] |
| 132 |
abbreviations = {} |
| 133 |
for zone_name in pytz.all_timezones: |
| 134 |
zone = pytz.timezone(zone_name) |
| 135 |
for naive in samples: |
| 136 |
localized = zone.localize(naive) |
| 137 |
abbreviation = localized.tzname() |
| 138 |
if not abbreviation or _NUMERIC_ZONE_NAME.fullmatch(abbreviation): |
| 139 |
continue |
| 140 |
offset = int(localized.utcoffset().total_seconds()) |
| 141 |
abbreviations.setdefault(abbreviation.upper(), set()).add(offset) |
| 142 |
return abbreviations |
| 143 |
|
| 144 |
|
| 145 |
def find_conflicts(static=None, tz_database=None): |
| 146 |
"""Return the sorted list of :class:`Conflict` between both tables. |
| 147 |
|
| 148 |
``static`` and ``tz_database`` default to the real tables and are only |
| 149 |
meant to be passed in by the tests. |
| 150 |
|
| 151 |
Abbreviations the tz database no longer uses at all are skipped: dateparser |
| 152 |
deliberately keeps obsolete abbreviations such as ``AHST`` or ``LMT`` so |
| 153 |
that historical text still parses, and the tz database has nothing to say |
| 154 |
about them. |
| 155 |
""" |
| 156 |
static = static_tz_abbreviations() if static is None else static |
| 157 |
tz_database = tz_database_abbreviations() if tz_database is None else tz_database |
| 158 |
|
| 159 |
conflicts = [] |
| 160 |
for abbreviation, tz_database_offsets in tz_database.items(): |
| 161 |
if abbreviation not in static: |
| 162 |
continue |
| 163 |
dateparser_offset = static[abbreviation] |
| 164 |
if dateparser_offset in tz_database_offsets: |
| 165 |
continue |
| 166 |
kind = "unambiguous" if len(tz_database_offsets) == 1 else "unsupported" |
| 167 |
conflicts.append( |
| 168 |
Conflict(abbreviation, kind, dateparser_offset, sorted(tz_database_offsets)) |
| 169 |
) |
| 170 |
return sorted(conflicts) |
| 171 |
|
| 172 |
|
| 173 |
def main(): |
| 174 |
found = find_conflicts() |
| 175 |
if not found: |
| 176 |
print( |
| 177 |
"No conflicts: every abbreviation dateparser shares with the tz " |
| 178 |
"database resolves to an offset some zone actually uses." |
| 179 |
) |
| 180 |
return 0 |
| 181 |
for conflict in found: |
| 182 |
if conflict.kind == "unambiguous": |
| 183 |
detail = f"the tz database only ever uses {conflict.tz_database_offsets[0]}" |
| 184 |
else: |
| 185 |
detail = f"no zone uses it; options are {conflict.tz_database_offsets}" |
| 186 |
print( |
| 187 |
f"{conflict.abbreviation}: dateparser uses {conflict.dateparser_offset}, {detail}" |
| 188 |
) |
| 189 |
return 1 |
| 190 |
|
| 191 |
|
| 192 |
if __name__ == "__main__": |
| 193 |
raise SystemExit(main()) |