-
-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathgenerate_windows_to_olson_mapping.py
More file actions
58 lines (46 loc) · 2.1 KB
/
Copy pathgenerate_windows_to_olson_mapping.py
File metadata and controls
58 lines (46 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Generate the Windows to Olson timezone mapping module windows_to_olson.py."""
# Source - https://stackoverflow.com/a/16157049
# Posted by unutbu, modified by community. See post 'Timeline' for change history
# Retrieved 2026-07-16, License - CC BY-SA 3.0
# Source - https://stackoverflow.com/a/10469748
# Posted by mzjn, modified by community. See post 'Timeline' for change history
# Retrieved 2026-07-16, License - CC BY-SA 3.0
import json
import urllib.request as req
import xml.etree.ElementTree as ET
from pathlib import Path
# Get the XML data from the CLDR on GitHub and save to a file.
# Unfortunately, the CLDR does not have a latest or stable tag
# that points to the stable version.
# Use the `main` branch, which is the development branch.
url = "https://raw.githubusercontent.com/unicode-org/cldr/refs/heads/main/common/supplemental/windowsZones.xml"
with req.urlopen(url) as response: # noqa: S310
xml_content = response.read()
# Parse the XML file and extract the timezone mapping.
result = {}
tree = ET.fromstring(xml_content) # noqa: S314
for zone in tree.findall(".//mapZone"):
attrib = zone.attrib
if attrib["territory"] == "001":
result[attrib["other"]] = attrib["type"]
HERE = Path(__file__).parent
DEST = HERE / "src" / "icalendar" / "timezone"
file = "windows_to_olson.py"
filepath = Path(DEST, file)
print(f"Writing {filepath}") # noqa: T201
with Path.open(filepath, "w") as f:
f.write("""\"\"\"
This module contains mappings from Windows timezone identifiers to
Olson timezone identifiers.
This file is automatically generated by generate_windows_to_olson_mapping.py.
Do not edit manually.
The data is taken from the Unicode Consortium [0].
The proposal and rationale for this mapping is also available at the
Unicode Consortium [1].
[0] https://raw.githubusercontent.com/unicode-org/cldr/refs/heads/main/common/supplemental/windowsZones.xml
[1] https://cldr.unicode.org/development/development-process/design-proposals/extended-windows-olson-zid-mapping
\"\"\"
""")
f.write("WINDOWS_TO_OLSON = ")
f.write(json.dumps(dict(result), indent=4, sort_keys=True))
f.write("\n")