tzf-rs: a fast timezone finder for Rust.


[!NOTE]
This package uses simplified shape data so it is not entirely accurate around the border.
- Released documentation: docs.rs/tzf-rs
- Latest documentation(not released yet): ringsaturn.github.io/tzf-rs
- Try it online: tzf-web
Build options
By default, the binary is built as well. If you don't want/need it, you can omit the default features and build like this:
Or add in the below way:
Best Practices
It's expensive to init tzf-rs's Finder/FuzzyFinder/DefaultFinder, so
please consider reusing instances or creating one as a global variable. Below is
a global variable example:
use lazy_static;
use DefaultFinder;
lazy_static!
For reuse,
racemap/rust-tz-service provides
a good example.
A Redis protocol demo could be used here:
ringsaturn/redizone.
Setup 100% Accurate Lookup
By default, tzf-rs uses a simplified shape data. If you need 100% accurate lookup, you can use the following code to setup.
- Download full data set, about 90MB.
- Use the following code to setup.
use Finder;
use Timezones;
A full example can be found here.
Advanced Usage - Export GeoJSON
[!NOTE]
This feature is designed for data visualization purposes and I can't guarantee the performance when using it in high-performance scenarios. Please do proper performance tests and necessary optimizations before using it in high performace production, for example caching the exported GeoJSON data or push to CDN.
It's a common use case make some visualization of timezone boundaries. For this purpose, tzf-rs provides methods to export the preindex tile data or specific timezone polygons as GeoJSON format.
To enable this feature, you need to build tzf-rs with export-geojson feature:
# Please note that >= 1.1.1 is required to have full GeoJSON functionality.
= { = "{version}", = ["export-geojson"]}
Then you can use the following methods:
// examples/query_tokyo.rs
use DefaultFinder;
The timezone at longitude 139.6917, latitude 35.6895 is: Asia/Tokyo
Found GeoJSON feature for timezone: Asia/Tokyo
Total number of polygons in feature collection: 24
Found Index GeoJSON feature for timezone: Asia/Tokyo
For now, tzf-rs' binding in Wasm, named tzf-wasm), has exported this feature and it has been deployed to the tzf-web for online usage.
Performance
The tzf-rs package is intended for high-performance geospatial query services,
such as weather forecasting APIs. Most queries can be returned within a very
short time, averaging around 3,000 nanoseconds (about 1,000ns slower than with
Go repo tzf. I will continue improving this - you can track progress
here).
Here is what has been done to improve performance:
- Using pre-indexing to handle most queries takes approximately 1000 nanoseconds.
- Using a finely-tuned Ray Casting algorithm package
ringsaturn/geometry-rsto verify whether a polygon contains a point.
That's all. There are no black magic tricks inside the tzf-rs.
Below is a benchmark run on global cities(about 14K), and avg time is about 3,000 ns per query:
// require toolchain.channel=nightly
test benches_default::bench_default_finder_random_city ... bench: 1,220.19 ns/iter (+/- 54.36)
| Criterion result | Pic |
|---|---|
![]() |
|
| Regression | ![]() |
You can view more details from latest benchmark from GitHub Actions logs.
References
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.
- Original Go repo:
ringsaturn/tzf - Binary timezone data:
ringsaturn/tzf-rel - Geometry: use
ringsaturn/geometry-rswhich istidwall/geometry's Rust port. - Continuous Benchmark compared with other packages:
ringsaturn/tz-benchmark
Bindings
- Ruby, see
HarlemSquirrel/tzf-rb - Python, see
ringsaturn/tzfpy - Wasm, see
ringsaturn/tzf-wasm - PostgreSQL extension, see
ringsaturn/pg-tzf
Command line
The binary helps in debugging tzf-rs and using it in (scripting) languages without bindings. Either specify the coordinates as parameters to get a single time zone, or to look up multiple coordinates efficiently specify the ordering and pipe them to the binary one pair of coordinates per line.
tzf --lng 116.3883 --lat 39.9289
echo -e "116.3883 39.9289\n116.3883, 39.9289" | tzf --stdin-order lng-lat
If you are using Nixpkgs, you can install the tzf command line tool via
unstable channel, please see more in
Nixpkgs.
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.

