Skip to content

Rust (tzf-rs)

Reuse the Finder

Initializing a Finder, FuzzyFinder, or DefaultFinder is expensive — it loads and parses the timezone data. Always reuse a single instance, for example as a lazy_static global:

cargo add tzf-rs lazy_static
use lazy_static::lazy_static;
use tzf_rs::DefaultFinder;

lazy_static! {
    static ref FINDER: DefaultFinder = DefaultFinder::new();
}

fn main() {
    // Coordinates are in (longitude, latitude) order.
    print!("{:?}\n", FINDER.get_tz_name(116.3883, 39.9289));
    print!("{:?}\n", FINDER.get_tz_names(116.3883, 39.9289));
}

YStripes index (default since v1.2.0)

DefaultFinder::new() enables the YStripes spatial index by default, bringing single random lookup to ~1 µs on modern hardware. To opt out (e.g. to reduce memory or build time):

use tzf_rs::{DefaultFinder, FinderOptions};

fn main() {
    let finder = DefaultFinder::new_with_options(FinderOptions::no_index());
    println!("{}", finder.get_tz_name(139.767125, 35.681236));
}
Index modeBuild timeMemory
No index~40ms~70 MB
YStripes~50ms~110 MB

Full-precision mode (v1.3.0+)

By default tzf-rs uses topology-simplified data (~5.4 MB). For 100% accurate lookups, enable the full feature (full dataset ~17 MB; not on crates.io due to size limits):

[dependencies]
tzf-rs = { git = "https://github.com/ringsaturn/tzf-rs", tag = "v{X}.{Y}.{Z}", features = ["full"], default-features = false }
use tzf_rs::DefaultFinder;

fn main() {
    let finder = DefaultFinder::new_full();
    let tz_name = finder.get_tz_name(139.767125, 35.681236);
    println!("tz_name: {}", tz_name);
}

Full-precision mode uses significantly more memory (~560 MB with YStripes index).

Integration examples