Skip to main content

Probably the fastest Python package to convert longitude/latitude to timezone name

Project description

tzfpy

  • PyPI FOSSA Status
  • PyPI - Python Version
  • PyPI - Downloads
  • Anaconda-Server Badge
  • Conda Downloads
  • Conda Platform

[!NOTE]

  1. It's probably the fastest Python package to convert longitude/latitude to timezone name.
  2. This package use a simplified polygon data and not so accurate around borders.
  3. Rust use lazy init, so first calling will be a little slow.
  4. Use about 70MB memory.
  5. It's tested under Python 3.10+.
  6. Try it online:

Usage

Please note that new timezone names may be added to tzfpy, which could be incompatible with old version package like pytz or tzdata. As an option, tzfpy supports install compatible version of those packages with extra params.

# Install just tzfpy
pip install tzfpy

# Install with pytz
pip install "tzfpy[pytz]"

# Install with tzdata. https://github.com/python/tzdata
pip install "tzfpy[tzdata]"

# Install via conda, see more in https://github.com/conda-forge/tzfpy-feedstock
conda install -c conda-forge tzfpy
>>> from tzfpy import get_tz, get_tzs
>>> get_tz(116.3883, 39.9289)  # in (longitude, latitude) order.
'Asia/Shanghai'
>>> get_tzs(87.4160, 44.0400)  # in (longitude, latitude) order.
['Asia/Shanghai', 'Asia/Urumqi']

Or you can try it via uvx:

uvx --with tzfpy python -c "from tzfpy import get_tz;tz = get_tz(116.3883,39.9289);print(tz)"
Asia/Shanghai

Index mode env vars

tzfpy follows current tzf-rs behavior: DefaultFinder enables y_stripes by default. If you need to disable y_stripes, use this environment variable:

export _TZFPY_DISABLE_Y_STRIPES=1

The index requires about 5MB memory, but can speed up query missing from pre-index, especially around borders.

Export to GeoJSON

For data visualization, you can get timezone polygon GeoJSON data from tzfpy:

from tzfpy import get_tz, get_tz_index_geojson, get_tz_polygon_geojson

lng = -74.0060
lat = 40.7128
tz = get_tz(lng, lat)
print(f"Timezone for ({lng}, {lat}): {tz}")

with open("tz_nyc_polygon.geojson", "w") as f:
    geojson_data = get_tz_polygon_geojson(tz)
    f.write(geojson_data)

with open("tz_nyc_index.geojson", "w") as f:
    geojson_data = get_tz_index_geojson(tz)
    f.write(geojson_data)

Best practices

  1. Always install tzfpy with tzdata extra: pip install tzfpy[tzdata]

  2. Use Python's zoneinfo package(import zoneinfo, aka tzdata in PyPI) to handle timezone names, even if you are using arrow:

    examples/tzfpy_with_datetime.py:

    from datetime import datetime, timezone
    from zoneinfo import ZoneInfo
    
    from tzfpy import get_tz
    
    tz = get_tz(139.7744, 35.6812)  # Tokyo
    
    now = datetime.now(timezone.utc)
    now = now.replace(tzinfo=ZoneInfo(tz))
    print(now)
    # 2025-04-29 01:33:56.325194+09:00
    

    examples/tzfpy_with_arrow.py:

    from zoneinfo import ZoneInfo
    
    import arrow
    from tzfpy import get_tz
    
    tz = get_tz(139.7744, 35.6812)  # Tokyo
    
    arrow_now = arrow.now(ZoneInfo(tz))
    print(arrow_now.format("YYYY-MM-DD HH:mm:ss ZZZ"))
    # 2025-04-29 01:33:56.325194+09:00
    

    If you are using whenever, since whenever use tzdata internally, so it's compatible with tzfpy:

    examples/tzfpy_with_whenever.py:

    from whenever import Instant
    from tzfpy import get_tz
    
    now = Instant.now()
    
    tz = get_tz(139.7744, 35.6812)  # Tokyo
    
    now = now.to_tz(tz)
    
    print(now)
    # 2025-04-29T10:33:28.427784+09:00[Asia/Tokyo]
    

Performance

Benchmark runs under v1.3.2 on my MacBook Pro with Apple M3 Max.

Benchmark with _TZFPY_DISABLE_Y_STRIPES=1
.

---------------------------------------------- benchmark: 1 tests ----------------------------------------------
Name (time in us)        Min     Max    Mean  StdDev  Median     IQR  Outliers  OPS (Kops/s)  Rounds  Iterations
----------------------------------------------------------------------------------------------------------------
test_tzfpy            1.2447  1.6922  1.3719  0.0555  1.3669  0.0643    133;11      728.9229     500       10000
----------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean
Results (8.51s):
         1 passed
Benchmark with default index mode
.

---------------------------------------------------- benchmark: 1 tests ----------------------------------------------------
Name (time in ns)          Min         Max      Mean   StdDev    Median      IQR  Outliers  OPS (Mops/s)  Rounds  Iterations
----------------------------------------------------------------------------------------------------------------------------
test_tzfpy            564.2528  1,250.2377  662.9543  87.4857  636.0787  70.2642     86;52        1.5084     500       16129
----------------------------------------------------------------------------------------------------------------------------

Legend:
  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
  OPS: Operations Per Second, computed as 1 / Mean
Results (6.63s):
         1 passed

Or you can view more benchmark results on GitHub Action summary page.

More benchmarks compared with other packages can be found in ringsaturn/tz-benchmark.

Background

tzfpy was originally written in Go named tzf and use CGO compiled to .so to be used by Python. Since v0.11.0 it's rewritten in Rust built on PyO3 and tzf-rs, a tzf's Rust port.

I have written an article about the history of tzf, its Rust port, and its Rust port's Python binding; you can view it here.

Also, see Project tzf for more information.

Compare with other packages

Please note that directly compare with other packages is not fair, because they have different use cases and design goals, for example, the precise.

TimezoneFinder

I got lots of inspiration from it. Timezonefinder is a very good package and it's mostly written in Python, so it's easy to use. And it's much more widely used compared with tzfpy if you care about that.

However, it's slower than tzfpy, especially around the borders, and I have lots of API requests from there. That's the reason I created tzf originally. And then tzf-rs and tzfpy.

pytzwhere

I recommend to read timezonefinder's Comparison to pytzwhere since it's very detailed.

Contributing

Install:

Available commands:
  build            - Build the project using uv
  build-ext        - Rebuild and install local Rust extension into venv
  fmt              - Format the code using ruff
  lint             - Lint the code using ruff
  sync             - Sync and compile the project using uv
  lock             - Lock dependencies using uv
  upgrade          - Upgrade dependencies using uv
  all              - Run lock, sync, fmt, lint, and test
  test             - Run non-benchmark tests
  test-all         - Run all tests including benchmark
  test-bench       - Run benchmark test with current env
  test-bench-index - Run benchmark in default/disable-y-stripes modes
make all

LICENSE

This project is licensed under the MIT license and Anti CSDN License1. The data is licensed under the ODbL license, same as evansiroky/timezone-boundary-builder

FOSSA Status

  1. This license is to prevent the use of this project by CSDN, has no effect on other use cases.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tzfpy-1.3.2.tar.gz (424.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tzfpy-1.3.2-cp314-cp314t-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.14tWindows x86-64

tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_aarch64.whl (4.7 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARM64

tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.5 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

tzfpy-1.3.2-cp314-cp314t-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

tzfpy-1.3.2-cp314-cp314t-macosx_10_12_x86_64.whl (4.4 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

tzfpy-1.3.2-cp310-abi3-win_amd64.whl (4.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

tzfpy-1.3.2-cp310-abi3-musllinux_1_2_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

tzfpy-1.3.2-cp310-abi3-musllinux_1_2_aarch64.whl (4.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tzfpy-1.3.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

tzfpy-1.3.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tzfpy-1.3.2-cp310-abi3-macosx_11_0_arm64.whl (4.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tzfpy-1.3.2-cp310-abi3-macosx_10_12_x86_64.whl (4.5 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file tzfpy-1.3.2.tar.gz.

File metadata

  • Download URL: tzfpy-1.3.2.tar.gz
  • Upload date:
  • Size: 424.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tzfpy-1.3.2.tar.gz
Algorithm Hash digest
SHA256 5f4dbe9be6957ba6eb16fb4deb67f0c14e8da2e46674ead2bd38dffa9c58da08
MD5 1d1d982714575080f25160c62876b136
BLAKE2b-256 7b167311f6449ba0b391e146a52684f3b32a18dd4775f6890ca08ef3f3f103aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2.tar.gz:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: tzfpy-1.3.2-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 408af2823d88c4e709dc264d8025941829f04cffcb8613e7093bc567a2f21349
MD5 bf1593b410203c9587eb872c9c523e15
BLAKE2b-256 8fb1b4344e06435c001ce20078751cab7190d6fa180c2d62ae756f788c6620be

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-win_amd64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 716898e6e024b5c9d09ca8fd9d9fa3abb05eb515dbd3e5443b3de88dfa290587
MD5 e54a238472c26da5ebdce8a3696afc2f
BLAKE2b-256 5199bd762e903c116b6ffdecd479f5ac91977f80e857079bba0cd278e4668f3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d3432bedb6ced68a8da045e526d9e77bcc51a46c2b50bb8308ebd3f0db887def
MD5 30dec5de0e9d999c230fc3b6cf5890ea
BLAKE2b-256 7aab75d5adc5a47d86da4637bc77b3ea6d86302e735b9f8efd18782b78a9b25a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-musllinux_1_2_aarch64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 670b02462e81cf0a63013d801e677e14da115e851aef80c4f1870b75f4cd873e
MD5 53181e7a02b51c01d325d0afcb30b917
BLAKE2b-256 14b51193cc98390877bac735ad5467dd1b561e0314d959fc61225ad1a9d1cf8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ee4e782b6ccb7c58eb22e9d668c1fd2aad698d31241d3f54d02d2b9007079d90
MD5 f7d15581b24ab8e3abed5f77de0846c9
BLAKE2b-256 45638db24f761f84f2352d2aaf3e3e187f33f1d33cf9ba39aa888be2b68e1e21

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4e9baecc74295fff884d063b8fdda683d9e723b81d27ed620be4b71551866d64
MD5 eaec23adbdbc19a5303f5d7153bff470
BLAKE2b-256 d0a0839e0669a513127d94248c6bc0471a644f6d91a202ab4359b05f62585177

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-macosx_11_0_arm64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a92706caa3ee760dee55d73c0a9c92af6bde9d73a4748711b798800e2e2f5de2
MD5 c2848ba2fe2da2faaf796f1b4d70e70d
BLAKE2b-256 a11a100a12c4608beff8d4cd388e4ec91458ef7cef5b9d93e4f6363b779d4946

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp314-cp314t-macosx_10_12_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: tzfpy-1.3.2-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 47f5853af4d7759a0ca10a4f036b0f4f20873089c124d38d79a4018c380182fc
MD5 eb19f2773553e99c6e93c4d3d8d1c24f
BLAKE2b-256 e7ebe5f6dcda9d44f228ae5e1a27ed9e5c28fd70b0442ac98516e44e0c554842

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-win_amd64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0b3ec412c2e4b4e9799091d09210c858ee6dfa3b1edda747c2b3d82a9ca8ff80
MD5 84ee97d674335f6e499f318721335a09
BLAKE2b-256 dee536960c3c78bab5c1864ebd1d7331d63fab3d10092fc2ec4bd5b6ab106cbe

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f1d21ad1be0c98ee54bc98e7a34d14ab26dcad8870d347db51e5285033fca6dc
MD5 7f91ebad890f5c3e8af8b0a14edbd593
BLAKE2b-256 aabf47efc11c2bb816f7e65d173b93286279ffdfff0ac6dfefef54f95be50e0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c16fe691c99390b6f1ca5245a11c846eaa336eb21276ce6ea3cc8d2d639a89a
MD5 bfff9cfa9094e3683512ef067d84bc99
BLAKE2b-256 a3e1670b2cadb15b75c53d42ba828533eee07bf0297113373825475291851d36

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b604acdf93d0fe6959d2242bc94857889e17c180a5b73e19f2052adb1d6a98b2
MD5 2cb61a2079e897c80d14d008bd7852a7
BLAKE2b-256 1d2968cea2d25ecfbf3a0c13f17c820d90e12729568c395128d1d4bea0a835ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f8ff5e13bd88259064678b1aeed3de0892e0f22c462173c49adc938ceb6496e
MD5 d8635d30168ee2862ad6bb8956a180b0
BLAKE2b-256 aecf38d031b1e796439bf713323fa917859c2fbb45e6e73bec514f8646202baa

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tzfpy-1.3.2-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.3.2-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5e240beda40a3de8e16ede62c61bf568ba59bf63e375940a9c7e770f001aff41
MD5 b86a5939388bc11fbde2064b51b727bf
BLAKE2b-256 609f2363f8c714ea66c8d6e1368a20db39cdcc13cda8ed19ec614cea9eb05bdb

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.3.2-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: CI.yml on ringsaturn/tzfpy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page