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 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.

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 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.

FOSSA Status

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.1.tar.gz (435.3 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.1-cp314-cp314t-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.14tWindows x86-64

tzfpy-1.3.1-cp314-cp314t-musllinux_1_2_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

tzfpy-1.3.1-cp314-cp314t-musllinux_1_2_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ ARM64

tzfpy-1.3.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ x86-64

tzfpy-1.3.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.17+ ARM64

tzfpy-1.3.1-cp314-cp314t-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.14tmacOS 11.0+ ARM64

tzfpy-1.3.1-cp314-cp314t-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.14tmacOS 10.12+ x86-64

tzfpy-1.3.1-cp310-abi3-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.10+Windows x86-64

tzfpy-1.3.1-cp310-abi3-musllinux_1_2_x86_64.whl (4.3 MB view details)

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

tzfpy-1.3.1-cp310-abi3-musllinux_1_2_aarch64.whl (4.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

tzfpy-1.3.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

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

tzfpy-1.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (4.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

tzfpy-1.3.1-cp310-abi3-macosx_11_0_arm64.whl (4.1 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

tzfpy-1.3.1-cp310-abi3-macosx_10_12_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: tzfpy-1.3.1.tar.gz
  • Upload date:
  • Size: 435.3 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.1.tar.gz
Algorithm Hash digest
SHA256 35b3f4f39509564847afee36d407b991b4b23778caa560ec780bb88e19412df5
MD5 8fc0b99b608ba17e3e8792bd257c525e
BLAKE2b-256 77c2ac5db7c566549f795f85ad38506fc66e230ee98773cdc84721204fd29b02

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tzfpy-1.3.1-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 3.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.3.1-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 304e2e117439473ead0b16d1511180984557dad7af9b0f992f305c7c3fae4339
MD5 6b1d1289cd78a515c2e30eb660091bbd
BLAKE2b-256 72c58d5f3c7a19b97f01825f5554f9010fbf99cab89e1b3c5f9c21ea1bed57e2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cd14f929eb1d266b2b3a16f45a1fb4f56586bb514217698644f2b6c39a1f4a56
MD5 4e81ae9a326c1121460540ab25f9f81b
BLAKE2b-256 92de81c9f02c4acbfa8deb15e08c4fbde7f2ead4b87442c15ccbf98f3ba45029

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8306de50e5a7c001706a76f51a3a23bfba498a7255aa9a2622f579b50be11201
MD5 ce53e76394c022355391cc1eec6c595f
BLAKE2b-256 61a8fe09723255489078ab5086edcbb748478e1efbcf48a91e92e1836eeb2583

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5c87bc9d7adea6bd24f3b7823d0d2f013495370cd5266b86748fdcaf34e3ea29
MD5 64c4a1bf347e9a3419fc95a97dd986c8
BLAKE2b-256 efab10b16f53d66b6bc3f39e02a7b71d86183a9bcc3031be19ce62fe7639b031

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 380e65e1871e21bd43bec1035d95aa359a387c3eb323cb364bb95a5826f7e518
MD5 de14c14f3288e9ce314cd4d753f0fe04
BLAKE2b-256 817f5cc25d64f53ecbb5de521165ab18c52e7828270d35953c5279d0be9554ba

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 30d45c8d57e9aaed9590da127fa303e87e790c3a14469d27232aeb5d50ed2fd6
MD5 91136f7a8283521fdab39face75ad69b
BLAKE2b-256 53a3956865d8a9b2e690a33b9f5f208220c0d425905ccea5234092c79e5e7954

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp314-cp314t-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c90f6713b93225cd4dde80a686743fe089d1544d67624ef5c856e01553dfc405
MD5 3ad3c678e90b54a87b3708e1d26f5806
BLAKE2b-256 365f81735d11b622cc3b678d8910bbcdab0c738cb8b142316b3da0502a9c40e1

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tzfpy-1.3.1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 3.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.3.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 134c47dc892188c8666b65d9dd64a1c5a3317b41b527c6bf02b1de0ed0a12091
MD5 74bf8d565f9bd81534864971abfe638a
BLAKE2b-256 9c160a4c77e91c3e21c764748a7b4ebbfd13d3c61a0ef71965d7f221cbfdaa39

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 739fbf51229208fcab50527556457c7335ad5e6ed5d0d761d28c16d714ae4394
MD5 fa08bfb5cf60ef71f766192277850087
BLAKE2b-256 85b0db030a395bf68f41011d532fa9f15b13e734bbcddd85188ca52f3049c25d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 423adf12efc6f917c32efb559d50fea7fca8a8f2ecd545e04bd372aa15655fb2
MD5 a824ec125583923a6fed1a2505c7c078
BLAKE2b-256 284d483fd8763a1cc9f2d48e2d4aaba782ac1373301b1c8f5aa2323ca4cf4e19

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 465361d63cb659f6db4f5a1f47e6b11bacf8846e7c2b00744356b7b565378e48
MD5 3c4ffec83de12d7019c4ab987794f275
BLAKE2b-256 1662bbdff92580d89d8ce31d710714b0e6a0e76dc71546a466fb50fd1ef84d6d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c2e2a1fd961c7241b30ef683b84515d6f1ef147f6074aeb593705e3c975a0f1b
MD5 9a60c73afe1fdd8076c5e94263489176
BLAKE2b-256 9c00c3000c8fe1f44cde8645ccb23eec37b441e964da353560bd6f5deb5d3127

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b2881b65f04f1cd4b3b81b7751b090f0b4b9b7df90ca064d700e98074aec1dfe
MD5 37d53131e4998695f54a5fcbb0df0d6e
BLAKE2b-256 45c72af6b99a5df0e17ee724941577345926b97f1221356b751b72374a934cc8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for tzfpy-1.3.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0a37688c4cb7f97704d6d7e082cf43553dd89cfd2a6f180f5315e05b7e0db29e
MD5 777297c70ea3d3385506201c631a254a
BLAKE2b-256 e9d568a03ee515487c59850c49b675a5b913ae7d143b53cd6b29a9320778760d

See more details on using hashes here.

Provenance

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