tzfpy

1.1.3 · active · verified Thu Apr 09

tzfpy is a high-performance Python package for converting longitude and latitude coordinates to timezone names. Rewritten in Rust (using PyO3), it aims for speed over absolute precision, leveraging simplified polygon data. The current version is 1.1.3, and it maintains an active release cadence with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to retrieve a timezone name for given coordinates using `tzfpy.get_tz` and then apply it to a `datetime` object using Python's standard `zoneinfo` module. It is recommended to install `tzfpy` with the `tzdata` extra for optimal `zoneinfo` compatibility.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo
from tzfpy import get_tz

# Example coordinates for Tokyo, Japan
longitude = 139.7744
latitude = 35.6812

# Get the timezone name from coordinates
tz_name = get_tz(longitude, latitude)
print(f"Timezone for ({longitude}, {latitude}): {tz_name}")

# Use the timezone name with Python's built-in zoneinfo for datetime objects
if tz_name:
    try:
        tokyo_tz = ZoneInfo(tz_name)
        now_utc = datetime.now(timezone.utc)
        now_local = now_utc.replace(tzinfo=tokyo_tz)
        print(f"Current UTC time: {now_utc}")
        print(f"Current local time in {tz_name}: {now_local}")
    except Exception as e:
        print(f"Error creating ZoneInfo object for '{tz_name}': {e}")
else:
    print(f"Could not determine timezone for ({longitude}, {latitude})")

view raw JSON →