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 120MB 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

The index requires about 120MB 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.2.0 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            2.3709  2.9560  2.5338  0.0794  2.5213  0.0728     86;24      394.6709     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 (14.42s):
         1 passed
Benchmark with default index mode
.

---------------------------------------------- benchmark: 1 tests ----------------------------------------------
Name (time in us)        Min     Max    Mean  StdDev  Median     IQR  Outliers  OPS (Kops/s)  Rounds  Iterations
----------------------------------------------------------------------------------------------------------------
test_tzfpy            1.7190  2.4869  1.8321  0.1139  1.7934  0.0627     53;56      545.8076     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.35s):
         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.3.0.tar.gz (433.7 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.0-cp314-cp314t-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.14tWindows x86-64

tzfpy-1.3.0-cp314-cp314t-musllinux_1_2_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

tzfpy-1.3.0-cp314-cp314t-musllinux_1_2_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARM64

tzfpy-1.3.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

tzfpy-1.3.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (3.9 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

tzfpy-1.3.0-cp314-cp314t-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

tzfpy-1.3.0-cp314-cp314t-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

tzfpy-1.3.0-cp310-abi3-win_amd64.whl (3.8 MB view details)

Uploaded CPython 3.10+Windows x86-64

tzfpy-1.3.0-cp310-abi3-musllinux_1_2_x86_64.whl (4.2 MB view details)

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

tzfpy-1.3.0-cp310-abi3-musllinux_1_2_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tzfpy-1.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.0 MB view details)

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

tzfpy-1.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.0 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tzfpy-1.3.0-cp310-abi3-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tzfpy-1.3.0-cp310-abi3-macosx_10_12_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: tzfpy-1.3.0.tar.gz
  • Upload date:
  • Size: 433.7 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.0.tar.gz
Algorithm Hash digest
SHA256 53c2d3c0a1e439f98e0195c5dd6a3fb3d551d9a48ab425f8659d6992d543d688
MD5 630cacb12a2998ede7f20c4ece61e94d
BLAKE2b-256 a2447a21da2b11f523c5a288e1ad2647182f2acabe3d681667e757765dc4a318

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tzfpy-1.3.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 3.8 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.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 5faa278b96e17246e26b8ed4ca8ec2a3e93cbd3a38d6e10d95d48c46d039c7ea
MD5 b1f61a15a4f42a61c2dded0cab4ae5ac
BLAKE2b-256 d0a732ca0cc9b0da4a7b616208543f0c62cfbc63bb54e9c26fefb7f6a75fd4e7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7badb9359254b57f8c9ac725bb7112f638fa73a0aab710ebf5b3408823ea033c
MD5 ffae3e27361f3ee486dce7ba9b22fab5
BLAKE2b-256 65f16b77a4f53d44b67e8a6636cd2bab9f84844388e4119271661b43b1e0e59c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 cdeeadc5df5555b63b3c738b69d3cdf9a64204003ec3b0d6d26b362f3bbc639e
MD5 f72dcabac6b2531200c87f8edecb6fa4
BLAKE2b-256 cf9c49a1f62666f2f1c0c376578a6e752c8ff7efbbd3ee089449c1247a5dec2f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b3e6d0256bcc5970a53e80b9d6531a951e3e95060a78279f5292b230ac6fcb9
MD5 0a18f8bd4d54a9ce34ee0db3915def3c
BLAKE2b-256 74bb627341048aa9d766698ed9269f3f94f1a2dc8b4ed5ff8287ed294bea6f02

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 aa697db81e825dd4074ceb4ad9a01941258e4ba1d29f2de6032c78f59d9bcbc5
MD5 a94c0ed3b05b64a946e133ab55322708
BLAKE2b-256 1d53c8ae399f3e6d004a562458258b8824929470599490ee13e5784e5001b9f2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c908e2bbf43fed75c04a7bd81063c66466b73f2b77a3fccc8a27f47e0944312f
MD5 cdc6cfddb463b2a1fadd61db1d77e909
BLAKE2b-256 68fdaa979171c20110b25dbcbe08d147195bdfd8bcc3c6d8966ab2cbcf02bcee

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 95b6ff641d4f92c90391dfc674ff8390744baa4d3dbc09b5fc1f25e9298c42d2
MD5 7c546f2d9c819ca6ff1e08ac60724d4a
BLAKE2b-256 f4b6842a45964a1000b25749e617507146466692d07becd3ba0fa9809d2469c7

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tzfpy-1.3.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.8 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.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0d5dc5cea44c5f07fbea74a682ca0ac2e4d1c745c6fc323509a4d0c55b03e940
MD5 fa1d6a68642b221c0bc496b9359bf04e
BLAKE2b-256 57030775b9e63e99611507d026f1adb8537aba8f751a7aa7aa9ba2335da73a2b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 edf664fe0fb77decaad818f34dbd1487c59e984752ac207786fe8bc2cbb51a89
MD5 9d4595499d720c05677480692fde5038
BLAKE2b-256 f8727b92a1a6e5b1b61f623447452523bbb9286b7c5a90333150f2c804e54dc7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5b50bb2bc6b099d482af6dc6a63afdb3bf2dd1fa795b5b4d567c4739dacdb7e4
MD5 497ba851b6ebd6910ac0da3e0b5aaabd
BLAKE2b-256 c4c7d0593f969ada5945b7a577b1580ec61efd509d00dc9f7ec8e98ec5343d4b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f590dd19121da274aecffc7adab26c7abef27e3598c3ecd91d46a94cfe143c9
MD5 1e7fa1178f7d1a0dc9bd8b6f88270d0d
BLAKE2b-256 8cf1d8c753ed3353d5bffe1350b267c1579892eb562137793f23186faef33b07

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1c22d3b93a070186c9a14f11b0c59f3dacf774f0947328b75cdee09cf8386041
MD5 18af0cc59954e0f5eae52ebc92d2a4ae
BLAKE2b-256 a43b88584172f84ac2da3e50f5dbedfe105aa19ebcf586f3fc44931c406ee333

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 caaf6775815f13bcf57f7e272ceb5b5461e5216664f06cc52c0a9992363982b2
MD5 534a8bcd56ce6bbdf5a7d574a98861bd
BLAKE2b-256 51e941d7543a0ac70e021a69fe3fa4bbe5f339ad505b18853da729c81f7e8334

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7203d8677bdbd1a6d36443f80420b80ce46e6b1ccbf8578dc608e044eb0889a4
MD5 bccf098abf417d81738af59796580f24
BLAKE2b-256 5974b02d02866023a147977fb0cd8da91e851c924415c4edecc32de2a46fe5a9

See more details on using hashes here.

Provenance

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