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']

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

pytest --benchmark-warmup=on --benchmark-warmup-iterations=100 tests/test_bench.py
-------------------------------------------------------------- benchmark: 1 tests --------------------------------------------------------------
Name (time in ns)                 Min          Max        Mean      StdDev      Median         IQR    Outliers  OPS (Kops/s)  Rounds  Iterations
------------------------------------------------------------------------------------------------------------------------------------------------
test_tzfpy_random_cities     895.7926  11,420.8087  2,597.6093  1,331.8472  2,337.5032  1,587.5907  11611;1000      384.9694   33614          10
------------------------------------------------------------------------------------------------------------------------------------------------

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 (2.03s):
         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
  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 tests using pytest
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.1.3.tar.gz (121.5 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.1.3-cp314-cp314t-win_amd64.whl (6.8 MB view details)

Uploaded CPython 3.14tWindows x86-64

tzfpy-1.1.3-cp314-cp314t-win32.whl (6.8 MB view details)

Uploaded CPython 3.14tWindows x86

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

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_armv7l.whl (7.3 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARMv7l

tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_aarch64.whl (7.1 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARM64

tzfpy-1.1.3-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.1.3-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.0 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARMv7l

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

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

tzfpy-1.1.3-cp314-cp314t-macosx_11_0_arm64.whl (7.0 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

tzfpy-1.1.3-cp314-cp314t-macosx_10_12_x86_64.whl (6.9 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

tzfpy-1.1.3-cp310-abi3-win_amd64.whl (6.8 MB view details)

Uploaded CPython 3.10+Windows x86-64

tzfpy-1.1.3-cp310-abi3-win32.whl (6.8 MB view details)

Uploaded CPython 3.10+Windows x86

tzfpy-1.1.3-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.1.3-cp310-abi3-musllinux_1_2_armv7l.whl (7.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

tzfpy-1.1.3-cp310-abi3-musllinux_1_2_aarch64.whl (7.1 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tzfpy-1.1.3-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.1.3-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (7.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

tzfpy-1.1.3-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.1.3-cp310-abi3-macosx_11_0_arm64.whl (7.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tzfpy-1.1.3-cp310-abi3-macosx_10_12_x86_64.whl (6.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: tzfpy-1.1.3.tar.gz
  • Upload date:
  • Size: 121.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for tzfpy-1.1.3.tar.gz
Algorithm Hash digest
SHA256 3307ec64fde01e1c9467eef5a3c2ba5bac5613f2767d1c05f80f604ac778a31d
MD5 e7ccce0a8c8de5b30b96c6e2dee6c981
BLAKE2b-256 dcd59159c597d3383c48bc1c7404d55c72c4dbc0b8e2574ebdff016531ea481f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tzfpy-1.1.3-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 6.8 MB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 5392377211dd0e1af26a5d2bc2bc97748990e465712a1742bacd5139a10b2689
MD5 5e84fb3f2ecb693e69b370f86754fdc9
BLAKE2b-256 73f7e7e704314fa4df439795605d976af4d251245a5de88f123ed84d6bbd0b00

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp314-cp314t-win32.whl.

File metadata

  • Download URL: tzfpy-1.1.3-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 6.8 MB
  • Tags: CPython 3.14t, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 8fecb6d1180a6fc78ea9243ae04e72a042bf23d04fbe93d86eb51b1efce4fd0e
MD5 5c0047a8916cae83ea429814f579d9e8
BLAKE2b-256 1bac8f579f5ce511bae0db235e7bba6e8eb7dbd50cffde3b0c0e4c0738ac1d2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b23f89b4d85bca9cc159028455ddd75c45c0d6315d06d416ccd72ae11cd1a45d
MD5 071caefdf9a77b942c8aaa581a5cf565
BLAKE2b-256 1d2de03f83c52b19ba80de2b204276b18e1ba16aab0489675fc5c8e5ef97a439

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 09b5c6b6daa03f56cfa9acb87339328535c30a5babc22b0aae5eda7082401d2e
MD5 b910ce65cc575c88266e68f6d6467f49
BLAKE2b-256 4ed1c36d4896640692ee3b757400391d2e20e13a43cecaf455bf6c7dfdebd18b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 aa94b09269996b8ff01f41ad6cce8e05d71a5d424fe7ba0e2ce04f74968c026d
MD5 ef0c97ef6ccf5016622289ef22d5b0fc
BLAKE2b-256 d6db3a1ec09ac8fc189292e7b3c19955a28b41ba7252f9dd0da23091b6da5e9a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 76ebc48576ee8c990951d286b7396f2e4896fc26cd10cb7cf022b41f833668b4
MD5 5343815fd6f6aeb6dbaad3026a6091c6
BLAKE2b-256 4cb98deeffc5b7b605a14212d840f75e15e0d8c22542022b0a2479a32afcdb25

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 eb18b3aebcedef390d974ff095d7ad23266fc531ebf7f873ad36daa09b2e1d1f
MD5 33c7546ec4c377a2d54a8f76096e49db
BLAKE2b-256 d35c43186dc1c97a70153c1aaa5d0c584cbbe2cf6bccb1252ffe137fec2fb917

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b7eb309600ad14d045cf83925615ab3942b8b958e466ce04f0a3dc6d11efda35
MD5 2b81574df7ea4deef9a42e5b6faaee0b
BLAKE2b-256 7174b63f5d6ff4a6393a56aa30d08232fa5e8ffa2a9c7af78ace04bf044c1cd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1811b8072d7213334aad906d4f9a606057e07411c060251b0226d4893fe7bfaa
MD5 2e528028ef71156e214101566bb8df74
BLAKE2b-256 ad5897496d181cc5694f58738c592d6f68088ce1324019d9372d6400b4d9a6da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6d5d240725ed7980fa0c16d835626d0750c1a233d04c6294ae2c861cd95e6cfc
MD5 7dd19b804563bb46eb369224e30574e3
BLAKE2b-256 59bf1685b5e694daad0779dd342597b6cd91c02a35724a4cf764d4253bd8d038

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tzfpy-1.1.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 6.8 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 363fa56b4b8980abbdb26e036a969c2c94fb58a2dc80d122cb93abd74ce32ab8
MD5 e7d1f0669243a6223e737b5919777b3e
BLAKE2b-256 8bb7cd0fca9577547198250a6be135f9a91549598ce9859cf829bbcb5d211925

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp310-abi3-win32.whl.

File metadata

  • Download URL: tzfpy-1.1.3-cp310-abi3-win32.whl
  • Upload date:
  • Size: 6.8 MB
  • Tags: CPython 3.10+, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: maturin/1.12.6

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-win32.whl
Algorithm Hash digest
SHA256 4cbf2239ae8cb5caeeddb2805ec7a3912c303ed61b4549d8068efbc84ad8d428
MD5 3878a8fc33025a017ddf0c7869b3bebb
BLAKE2b-256 6f0c75b59727b1c273dac804ffe0f2df75a33458195f0f5792f03349c28e871c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 08bf16a0f305d7fae399abd004212a2529670a8a424189b351da8da275900555
MD5 b84cb3c9d6181a35b48f2dfd996557e6
BLAKE2b-256 cbe3a3f9151c06672665ad44a83fd368bdbeec9c09e160dbe35dd4ddeeaa99cf

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 12d25d95cff5a9acaaf273253afe1ad7a5b1d720e7d13c18dfd4843ee2a3e114
MD5 ebab163a7d528b73a2e826a03e1d0a2f
BLAKE2b-256 9ceea89d9b4cfb7b91bf9c63d2fd59a7394be9e82d6e1cc706f72ad74ba517dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 0781a26e332b49f213f510913f5088d8922d678bf70e73dbf34ca8ce9f74574c
MD5 d476fece57176a827f5335cb0630308d
BLAKE2b-256 e8de82ab6f1bccf80373274d780e2591855c951b1344feffedb13d9de317a159

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 32198daa2e24ed0e6bf1c2835a482e3502d3f32f1b077e66b55109fa4c39020d
MD5 2e4eb11857c3bfa01d25e9276dc60534
BLAKE2b-256 1ce5a3e00eaaff0061470310c3860ff92b6856ac79aac5d027ceeb0e91795442

See more details on using hashes here.

File details

Details for the file tzfpy-1.1.3-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 efa4db8d14021296e804219d0215490a8d91e456fbc37ab0516e766c038578d7
MD5 658fade5a89f679315bc4526a05dbb7f
BLAKE2b-256 3b184603bfa80420ea8a7107046330f229cc66594d615ba8825229b2dd35b052

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a090a3f0a710e917fcd77d7c67e7202b785f8ad44b80f60a6023d3543a51e8d6
MD5 2e45ed037de833e5d5972f6779ae69d9
BLAKE2b-256 237056ab2aae39d38fa18cf703a1ba768499ba706e72cf6bd003140293436171

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 77d82e07dbf640473fb0a03bdf5b7146d53c119d60ddcbdd2388bd64594e5649
MD5 cfa6d3e870d37482516febcbec42c50b
BLAKE2b-256 a5b66769c365ed01914f1b5fb8190d5f6285107f5142512c3470a8530bcf1871

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tzfpy-1.1.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4d68b253fbf021f13ea4e745050cc161d826d07ac6954a66f3a0bd1e3dee9d91
MD5 5a49e41908cafbd3bd5246b32f3b0c6d
BLAKE2b-256 19dd4f96a5f280f0b83643146ed19774439ae79612ce4bf4b1218dd9a77c9d3c

See more details on using hashes here.

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