Skip to main content

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

Project description

tzfpy

  • PyPI
  • 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 40MB 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']

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

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.2.0 on my MacBook Pro with Apple M3 Max.

pytest --benchmark-warmup-iterations=500 --benchmark-min-rounds=500  --benchmark-min-time=0.01 tests/test_bench.py
---------------------------------------------- benchmark: 1 tests ----------------------------------------------
Name (time in us)        Min     Max    Mean  StdDev  Median     IQR  Outliers  OPS (Kops/s)  Rounds  Iterations
----------------------------------------------------------------------------------------------------------------
test_tzfpy            1.7801  2.3277  1.8855  0.0759  1.8648  0.0483     68;52      530.3520     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 (10.62s):
         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.

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 License[^anti_csdn]. The data is licensed under the ODbL license, same as evansiroky/timezone-boundary-builder

[^anti_csdn]: 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.2.0.tar.gz (431.4 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.2.0-cp314-cp314t-win_amd64.whl (6.9 MB view details)

Uploaded CPython 3.14tWindows x86-64

tzfpy-1.2.0-cp314-cp314t-musllinux_1_2_x86_64.whl (7.2 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

tzfpy-1.2.0-cp314-cp314t-musllinux_1_2_aarch64.whl (7.2 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARM64

tzfpy-1.2.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.0 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

tzfpy-1.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.0 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

tzfpy-1.2.0-cp314-cp314t-macosx_11_0_arm64.whl (7.1 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

tzfpy-1.2.0-cp314-cp314t-macosx_10_12_x86_64.whl (7.0 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

tzfpy-1.2.0-cp310-abi3-win_amd64.whl (6.9 MB view details)

Uploaded CPython 3.10+Windows x86-64

tzfpy-1.2.0-cp310-abi3-musllinux_1_2_x86_64.whl (7.2 MB view details)

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

tzfpy-1.2.0-cp310-abi3-musllinux_1_2_aarch64.whl (7.2 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tzfpy-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.0 MB view details)

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

tzfpy-1.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tzfpy-1.2.0-cp310-abi3-macosx_11_0_arm64.whl (7.1 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tzfpy-1.2.0-cp310-abi3-macosx_10_12_x86_64.whl (7.0 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for tzfpy-1.2.0.tar.gz
Algorithm Hash digest
SHA256 0acd0f69de697ec8e7e1d6429d784c200cc5caa7a75d45edb503781bdd5e2faf
MD5 a5026818ee47d4264ca7bbfacdb9768e
BLAKE2b-256 6255bce1ab8308a33678d0821e9ed9dd9ad05b2f44df6a66faf7de141ca12abf

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0.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.2.0-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: tzfpy-1.2.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 6.9 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.2.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 cf7fa75944b5141a1e8818427bad91849ccc33f95267504bec45e749c6787905
MD5 f762a72c907ddee7871ca29ada73e966
BLAKE2b-256 5a2d6655e0308e383c4bc574f456cbfe49857c4bd194d04e756d6dde9e218d66

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b9ba4b70c2a12cb7e4997d3cc4819c794a10c1047f2c869541759a959327df72
MD5 ce42fc30003c0794be8908036ea2b7f6
BLAKE2b-256 a6b5c96983c5e48cca1a042e96f2af5b932485abedf51b8783ac3aa9466d41c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 af3509a704bb5e706d8ecc92288b0f7baa964460cbac133f238666ba4ef2872d
MD5 1d88e60a6ecd62371eb2151ba6325676
BLAKE2b-256 9dc4d82af1ae23bdbe16b3c27aab2f00110397b79098131c4a8aff0a71999561

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 51032677521c757d04b33bd38d812e9f78de41a1c098a8ef2b55583397564c34
MD5 f05f89a5a6dfd6c0013aee9ff1c4f328
BLAKE2b-256 3f0345da49a26e4ef3919210f1dc47215f656c72e895460a2c15fed442369ae3

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9fce706d0e82976897eae29524b39d5086e26d7affb91c7dac8113242c8bb79d
MD5 8a7d515b120839bdbdc17873274ffe43
BLAKE2b-256 9940a500611e68a88a238b13646362582613f496f02fa92216c806f1a5e8a76a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1308b5e03eacc53ad7553449280e03e10ba436b66d8354e64da1e27c9e616f0e
MD5 5823641b9754937218699346ed1ede58
BLAKE2b-256 fefd50956ebaa4f88a0cb06e4d0bf84197b0c06d3b5f20a1b0d47937fedbe347

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp314-cp314t-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7552ae0a0968a40a2c93039d8e4be158f2640d08c85e71097ebcf293d3b3d1d4
MD5 2e0b8f024098dd65ff41618434bb0181
BLAKE2b-256 4faea9100e19cb1e16f8b32fb11dcd0914bc0e5079f920f49d625ec570e3e325

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: tzfpy-1.2.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 6.9 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.2.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 da9fc7557db03bf6f221e0a32fecf58d674b3c38238e40e1f626933b50ac0f09
MD5 8b79a1d8c35f71436bc9419b96fa9e22
BLAKE2b-256 269cc3b96df3502e9a8e1190e1d29c9f04d7c0baf30b3d9aca053ac84161fb84

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 174cdf0ecb7ac5cfaff78e3929ee4b02fb8db1a4930e6370ae224c60f24e818b
MD5 4b331fb8fc47b5ec3321dbbd4665f02f
BLAKE2b-256 bc4fa81289c8b17b61a1edd0af97c2d68d4402dbab42368167f717690adbcfa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4511a38bb318e8557294ddf9d7143e535c702420a53012d1a094fb20d42f007e
MD5 fe6af85798d62e4a6f783bc63e81e975
BLAKE2b-256 afe1c47ddf0002e153ce35d13b849bd3d334496bfc5cb6db707152e83c9b577d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 44b5ec963b14b873fd326ffe9ffbcdeb7cc5afdf8e3c71e50e52ed8edc6d3cee
MD5 1a6445c899674d1dbf1e9c039e867158
BLAKE2b-256 02761cb5fae630c716e5c916e1661cacba6efe7b6823e9fc9a4036799252f323

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ab054a9af1ecda830599f3b7d5214914490699ee6f589c2ba2faba4a8434bfa2
MD5 ba0f316c73c5925023ac0ee087ba0e02
BLAKE2b-256 b4983ac67aa636faea20c712e3552164fa3e7fde5622abbcd5a4c5183e35fb4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a87bdcab0d8b91a090b4c75ab8ecc39471626c055829e17d917fa3fb3708d4df
MD5 e344dbc901295fd72c7fdb4b16b9c51f
BLAKE2b-256 555c430ee5be5af5795d7023440799bf622239506f078d1170fbcffe492662f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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.2.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for tzfpy-1.2.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 43efafdbf674447a3a89762810c16ad5b44d22aca7d5cbe07c9390306c7d203e
MD5 a9157becb31ace4302d4ae58a0e2b275
BLAKE2b-256 5f214fd634b306c5d32a19b97bce487b0f08bb7cc709512480cb743e37bf4a7f

See more details on using hashes here.

Provenance

The following attestation bundles were made for tzfpy-1.2.0-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