From 05ed407c264121802568b88c2d73ee10f4866164 Mon Sep 17 00:00:00 2001 From: Xavier Olive Date: Sun, 10 Nov 2024 16:01:40 +0100 Subject: [PATCH] fix for tzdata missing in windows --- src/pyopensky/__init__.py | 10 ++++++++ src/pyopensky/tzdata.py | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/pyopensky/tzdata.py diff --git a/src/pyopensky/__init__.py b/src/pyopensky/__init__.py index 1e856e6..17ec0b7 100644 --- a/src/pyopensky/__init__.py +++ b/src/pyopensky/__init__.py @@ -1,3 +1,13 @@ +import sys from importlib.metadata import version +from .tzdata import download_tzdata_windows + __version__ = version("pyopensky") + +# Despite the 'win32' string, this value is returned on all versions of Windows, +# including both 32-bit and 64-bit. The 'win32' identifier is a historical +# artifact that has been retained for compatibility with older versions of +# Python. +if sys.platform == "win32": + download_tzdata_windows(year=2022) diff --git a/src/pyopensky/tzdata.py b/src/pyopensky/tzdata.py new file mode 100644 index 0000000..4c1120f --- /dev/null +++ b/src/pyopensky/tzdata.py @@ -0,0 +1,48 @@ +# While Arrow uses the OS-provided timezone database on Linux and macOS, it +# requires a user-provided database on Windows. You must download and extract +# the text version of the IANA timezone database and add the Windows timezone +# mapping XML. +# https://arrow.apache.org/docs/cpp/build_system.html#runtime-dependencies + +from __future__ import annotations + +import os +import tarfile +from pathlib import Path + +import httpx + + +def download_tzdata_windows( + year: int = 2022, + *, + name: str = "tzdata", + base_dir: None | Path = None, +) -> None: + folder = ( + base_dir if base_dir else Path(os.path.expanduser("~")) / "Downloads" + ) + + if (folder / name).is_dir(): + return + + tz_path = folder / "tzdata.tar.gz" + + c = httpx.get( + f"https://data.iana.org/time-zones/releases/tzdata{year}f.tar.gz" + ) + c.raise_for_status() + tz_path.write_bytes(c.content) + + extracted_folder = folder / name + + if not extracted_folder.exists(): + extracted_folder.mkdir(parents=True) + + tarfile.open(tz_path).extractall(extracted_folder) + + c = httpx.get( + "https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml" + ) + c.raise_for_status() + (extracted_folder / "windowsZone.xml").write_bytes(c.content)