### Install and Use tzf Command Line Interface Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Provides instructions for installing the tzf binary globally using Cargo and demonstrates its usage for single coordinate lookups and batch processing. It covers specifying coordinate order (lng-lat or lat-lng) and handling different input delimiters when processing from standard input. ```bash # Install CLI globally cargo install --path . --no-default-features # Single coordinate lookup (longitude, latitude) tzf --lng 116.3883 --lat 39.9289 # Output: "Asia/Shanghai" # Alternative spelling for longitude tzf --lon 116.3883 --lat 39.9289 # Batch processing from stdin (lng-lat format) echo "116.3883 39.9289" | tzf --stdin-order lng-lat echo -e "116.3883 39.9289\n139.6917 35.6895" | tzf --stdin-order lng-lat # Batch processing with lat-lng format echo "39.9289 116.3883" | tzf --stdin-order lat-lng # Multiple delimiters supported (space, tab, comma, semicolon) echo "116.3883, 39.9289" | tzf --stdin-order lng-lat echo "116.3883;39.9289" | tzf --stdin-order lng-lat # Process file with coordinates cat coordinates.txt | tzf --stdin-order lng-lat > timezones.txt # Build and run CLI from source cargo build cargo run -- --lng 116.3883 --lat 39.9289 # Build library only without CLI cargo build --no-default-features ``` -------------------------------- ### Development Tools for tzf-rs Source: https://github.com/ringsaturn/tzf-rs/blob/main/CLAUDE.md Commands for managing development tools related to the tzf-rs project. Includes generating license information and installing the CLI binary globally. ```bash # Generate license information make THIRDPARTY.yml # Install CLI globally cargo install --path . --no-default-features ``` -------------------------------- ### Run Tests and Benchmarks for tzf-rs Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Explains how to execute the test suite and benchmarks for the tzf-rs project. The tests validate the accuracy of timezone data, while benchmarks measure performance using real-world city datasets. It also shows how to run specific test files and examples, including those requiring specific Cargo features. ```bash # Run all tests cargo test # Run specific test file cargo test --test basic_test # Run benchmarks (requires nightly toolchain) cargo bench # View benchmark results # Results are saved in target/criterion/report/index.html # Run example with GeoJSON export cargo run --example query_tokyo --features export-geojson ``` -------------------------------- ### Global Finder instance for tzf-rs Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md Provides a Rust code example demonstrating the best practice of creating a single, lazily initialized global instance of `DefaultFinder` to avoid expensive re-initializations. It shows how to query timezone names using this global instance. ```rust use lazy_static::lazy_static; use tzf_rs::DefaultFinder; lazy_static! { static ref FINDER: DefaultFinder = DefaultFinder::new(); } fn needless_main() { // Please note coords are lng-lat. print!("{:?}\n", FINDER.get_tz_name(116.3883, 39.9289)); print!("{:?}\n", FINDER.get_tz_names(116.3883, 39.9289)); } ``` -------------------------------- ### Use tzf-rs CLI for Timezone Lookups Source: https://github.com/ringsaturn/tzf-rs/blob/main/CLAUDE.md Examples of using the tzf-rs command-line interface (CLI) for timezone lookups. Supports single coordinate lookups and batch processing from standard input with different coordinate orderings. ```bash # Single coordinate lookup cargo run -- --lng 116.3883 --lat 39.9289 # Multiple coordinates from stdin echo "116.3883 39.9289" | cargo run -- --stdin-order lng-lat ``` -------------------------------- ### Build and Test Rust Library Source: https://github.com/ringsaturn/tzf-rs/blob/main/CLAUDE.md Commands for building the tzf-rs library, with or without the CLI binary, and running tests and benchmarks. Uses Cargo for build management. ```bash cargo build --no-default-features cargo build cargo test cargo bench ``` -------------------------------- ### Load and use full timezone data with tzf-rs Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md Illustrates how to load a full, accurate timezone data set (approx. 90MB) and use it with tzf-rs's `Finder` for precise timezone lookups. This requires downloading the data file separately and including it in the project. ```rust use tzf_rs::Finder; use tzf_rs::pbgen::tzf::v1::Timezones; pub fn load_full() -> Vec { include_bytes!("./combined-with-oceans.bin").to_vec() } fn main() { println!("Hello, world!"); let file_bytes: Vec = load_full(); let finder = Finder::from_pb(Timezones::try_from(file_bytes).unwrap_or_default()); let tz_name = finder.get_tz_name(139.767125, 35.681236); println!("tz_name: {}", tz_name); } ``` -------------------------------- ### Build tzf-rs without default features Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md Demonstrates how to build the tzf-rs binary without default features, useful if the binary is not required. This command can be used directly in the terminal. ```bash cargo build --no-default-features ``` -------------------------------- ### Add tzf-rs without default features to project Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md Shows how to add the tzf-rs crate to a project with default features omitted. This is done using the cargo add command. ```bash cargo add tzf-rs --no-default-features ``` -------------------------------- ### Create and Use Finder Instance in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Demonstrates creating a 'Finder' instance for accurate timezone lookups using polygon geometry. Initialization is expensive, so reuse is recommended. It takes longitude and latitude as input and returns timezone names. ```rust use tzf_rs::Finder; fn main() { // Create a new Finder instance (expensive operation - reuse in production) let finder = Finder::new(); // Single timezone lookup (lng, lat format) let tz_name = finder.get_tz_name(116.3883, 39.9289); assert_eq!(tz_name, "Asia/Shanghai"); // Lookup in Tokyo assert_eq!(finder.get_tz_name(139.4382, 36.4432), "Asia/Tokyo"); // Lookup in London assert_eq!(finder.get_tz_name(-0.9671, 52.0152), "Europe/London"); // Get all timezones for overlapping areas let tz_names = finder.get_tz_names(116.3883, 39.9289); println!("{:?}", tz_names); // Get all available timezone names let all_timezones = finder.timezonenames(); println!("Total timezones: {}", all_timetimes.len()); // Check data version println!("Data version: {}", finder.data_version()); } ``` -------------------------------- ### Create and Use DefaultFinder Instance in Rust (Recommended) Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Illustrates creating a 'DefaultFinder', a hybrid approach that prioritizes speed with FuzzyFinder and falls back to the accurate Finder. This is recommended for most production applications. It accepts coordinates and returns timezone information. ```rust use tzf_rs::DefaultFinder; fn main() { // Create DefaultFinder (recommended for production) let finder = DefaultFinder::new(); // Lookup timezone (tries FuzzyFinder first, falls back to Finder) let tz_name = finder.get_tz_name(116.3883, 39.9289); assert_eq!(tz_name, "Asia/Shanghai"); // Works accurately even near borders let border_tz = finder.get_tz_name(8.61231565, 47.66148548); println!("Timezone near border: {}", border_tz); // Get all timezone names at location let tz_names = finder.get_tz_names(116.3883, 39.9289); println!("{:?}", tz_names); // List all available timezones let all_timezones = finder.timezonenames(); println!("Total timezones: {}", all_timezones.len()); // Check data version println!("Data version: {}", finder.data_version()); } ``` -------------------------------- ### Load Full Accuracy Timezone Data in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Demonstrates how to load the complete, high-accuracy timezone dataset for tzf-rs. This involves including binary data from a separate repository and deserializing it into a `Timezones` protobuf object for use with the `Finder`. ```rust use tzf_rs::Finder; use tzf_rs::pbgen::Timezones; // Include the full accuracy binary data (download from tzf-rel repository) pub fn load_full() -> Vec { include_bytes!("./combined-with-oceans.bin").to_vec() } fn main() { // Load full accuracy data let file_bytes: Vec = load_full(); // Create Finder from protobuf data let finder = Finder::from_pb( Timezones::try_from(file_bytes).unwrap_or_default() ); // Now lookups are 100% accurate let tz_name = finder.get_tz_name(139.767125, 35.681236); println!("tz_name: {}", tz_name); // All other methods work the same let all_names = finder.get_tz_names(139.767125, 35.681236); println!("All timezones: {:?}", all_names); } ``` -------------------------------- ### Time Zone Lookup using tzf Command Line Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md This snippet demonstrates how to use the tzf command-line tool for timezone lookups. It shows how to specify coordinates as parameters for a single lookup and how to pipe multiple coordinate pairs for efficient processing. The tool supports different input formats for coordinates. ```shell tzf --lng 116.3883 --lat 39.9289 echo -e "116.3883 39.9289\n116.3883, 39.9289" | tzf --stdin-order lng-lat ``` -------------------------------- ### Create and Reuse Global Finder Instance in Rust (Best Practice) Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Demonstrates the best practice of creating a single, global instance of DefaultFinder using `lazy_static` to avoid expensive re-initialization. This global instance can then be reused across multiple functions for efficient timezone lookups. ```rust use lazy_static::lazy_static; use tzf_rs::DefaultFinder; lazy_static! { static ref FINDER: DefaultFinder = DefaultFinder::new(); } fn main() { // Reuse the global FINDER instance println!("{:?}", FINDER.get_tz_name(116.3883, 39.9289)); println!("{:?}", FINDER.get_tz_names(116.3883, 39.9289)); // Call from multiple functions without recreating process_location(139.6917, 35.6895); process_location(-74.0060, 40.7128); } fn process_location(lng: f64, lat: f64) { let tz = FINDER.get_tz_name(lng, lat); println!("Location ({}, {}) is in timezone: {}", lng, lat, tz); } ``` -------------------------------- ### Convert Coordinates to Map Tiles in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Shows how to use the `deg2num` function from tzf-rs to convert longitude and latitude coordinates into Slippy map tile numbers at a specified zoom level. This is useful for tile-based indexing and visualization. ```rust use tzf_rs::deg2num; fn main() { // Convert coordinates to tile numbers at zoom level 7 let (x, y) = deg2num(116.3883, 39.9289, 7); assert_eq!((105, 48), (x, y)); // Different zoom levels give different tile coordinates let (x1, y1) = deg2num(116.3883, 39.9289, 1); println!("Zoom 1: tile ({}, {})", x1, y1); let (x8, y8) = deg2num(116.3883, 39.9289, 8); println!("Zoom 8: tile ({}, {})", x8, y8); // Use for custom preindexing logic for zoom in 0..10 { let (tx, ty) = deg2num(-74.0060, 40.7128, zoom); println!("New York at zoom {}: tile ({}, {})", zoom, tx, ty); } } ``` -------------------------------- ### Benchmark DefaultFinder with Random Cities in Rust Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md This Rust code snippet sets up a benchmark test for the `DefaultFinder` in `tzf-rs`. It uses the `test` crate and nightly features to measure the average time taken to find the timezone for a random city. This helps in evaluating the performance of the library. ```rust #![feature(test)] #[cfg(test)] mod benches_default { use tzf_rs::DefaultFinder; extern crate test; use test::Bencher; #[bench] fn bench_default_finder_random_city(b: &mut Bencher) { let finder: DefaultFinder = DefaultFinder::default(); b.iter(|| { let city = cities_json::get_random_cities(); let _ = finder.get_tz_name(city.lng, city.lat); }); } } ``` -------------------------------- ### Create and Use FuzzyFinder Instance in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Shows how to create a 'FuzzyFinder' instance for extremely fast timezone lookups using a preindexed HashMap. It's suitable for high-throughput APIs but may have accuracy issues near borders. Input is longitude and latitude. ```rust use tzf_rs::FuzzyFinder; fn main() { // Create a new FuzzyFinder (uses preindex HashMap) let finder = FuzzyFinder::new(); // Fast timezone lookup let tz_name = finder.get_tz_name(116.3883, 39.9289); assert_eq!(tz_name, "Asia/Shanghai"); // Get all timezone names at this location let tz_names = finder.get_tz_names(116.3883, 39.9289); println!("Timezones found: {:?}", tz_names); // Check data version println!("Data version: {}", finder.data_version()); // Note: FuzzyFinder may not work accurately near borders // Consider using DefaultFinder for production applications } ``` -------------------------------- ### Export Specific Timezones to GeoJSON Files in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Exports individual timezone boundaries to separate GeoJSON files, with error handling for invalid timezones. This function allows exporting both precise boundaries using Finder and tile-based approximations using FuzzyFinder. It creates an output directory 'tmp' and saves each timezone's GeoJSON to a file named after the timezone. ```rust use std::fs; use tzf_rs::{Finder, FuzzyFinder}; fn main() { // Create output directory fs::create_dir_all("tmp").expect("Failed to create tmp directory"); let timezones = vec!["Asia/Tokyo", "America/New_York", "Europe/London", "Invalid/Timezone"]; // Export precise boundaries using Finder let finder = Finder::new(); for tz_name in &timezones { match finder.get_tz_geojson(tz_name) { Some(collection) => { let json_string = collection.to_string_pretty(); let filename = format!("tmp/{}_finder.geojson", tz_name.replace('/', "_")); fs::write(&filename, &json_string).expect("Failed to write file"); let polygon_count: usize = collection.features.iter() .map(|f| f.geometry.coordinates.len()) .sum(); println!("✓ {} -> {} ({} bytes, {} feature(s), {} polygon(s))", tz_name, filename, json_string.len(), collection.features.len(), polygon_count); }, None => { println!("✗ {} not found", tz_name); } } } // Export tile-based approximations using FuzzyFinder let fuzzy_finder = FuzzyFinder::new(); for tz_name in &timezones { if let Some(feature) = fuzzy_finder.get_tz_geojson(tz_name) { let json_string = feature.to_string_pretty(); let filename = format!("tmp/{}_fuzzy.geojson", tz_name.replace('/', "_")); fs::write(&filename, &json_string).expect("Failed to write file"); println!("✓ {} -> {} ({} bytes, {} tiles)", tz_name, filename, json_string.len(), feature.geometry.coordinates.len()); } } } ``` -------------------------------- ### Query Timezone and GeoJSON Boundary with tzf-rs in Rust Source: https://github.com/ringsaturn/tzf-rs/blob/main/README.md This Rust code snippet demonstrates how to use the `tzf-rs` crate to find the timezone name for a given longitude and latitude. It also shows how to retrieve and process the GeoJSON boundary data for the found timezone, along with its index polygon boundaries. Requires the 'export-geojson' feature. ```rust // examples/query_tokyo.rs use tzf_rs::DefaultFinder; fn main() { let default_finder = DefaultFinder::new(); let lng = 139.6917; let lat = 35.6895; let tz_name = default_finder.get_tz_name(lng, lat).to_owned(); println!( "The timezone at longitude {}, latitude {} is: {}", lng, lat, tz_name ); // Get the Polygon boundary for the timezone if let Some(boundary_file) = default_finder.finder.get_tz_geojson(&tz_name) { // It's GeoJSON Feature Collection, and the features contains "MultiPolygon" geometry for the timezone. println!("Found GeoJSON feature for timezone: {}", tz_name); let mut polygons: usize = 0; for feature in boundary_file.features { polygons += feature.geometry.coordinates.len(); } println!( "Total number of polygons in feature collection: {}", polygons ); } // Get the Index polygon boundary for the timezone if let Some(index_boundary_file) = default_finder.fuzzy_finder.get_tz_geojson(&tz_name) { // It's GeoJSON Feature, and the geometry contains "MultiPolygon" for the timezone index. // But the Polygons are actually map tiles. println!("Found Index GeoJSON feature for timezone: {}", tz_name); let mut polygons: usize = 0; for polygon in index_boundary_file.geometry.coordinates { polygons += polygon.len(); } println!( "Total number of tile polygons in index feature: {}", polygons ); } } ``` -------------------------------- ### Export Timezone Boundaries as GeoJSON in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Illustrates how to export timezone polygon boundaries into GeoJSON format using tzf-rs. This requires enabling the `export-geojson` feature and utilizes `DefaultFinder` to retrieve both precise timezone boundaries and map tile boundaries. ```rust // Add to Cargo.toml: tzf-rs = { version = "1.1.4", features = ["export-geojson"]} use tzf_rs::DefaultFinder; fn main() { let default_finder = DefaultFinder::new(); let lng = 139.6917; let lat = 35.6895; // Find timezone name let tz_name = default_finder.get_tz_name(lng, lat).to_owned(); println!("The timezone at longitude {}, latitude {} is: {}", lng, lat, tz_name); // Get precise polygon boundary for the timezone if let Some(boundary_file) = default_finder.finder.get_tz_geojson(&tz_name) { // Returns GeoJSON FeatureCollection with MultiPolygon geometry println!("Found GeoJSON feature for timezone: {}", tz_name); let mut polygons: usize = 0; for feature in boundary_file.features { polygons += feature.geometry.coordinates.len(); } println!("Total number of polygons in feature collection: {}", polygons); // Convert to JSON string let json_string = boundary_file.to_string(); println!("GeoJSON size: {} bytes", json_string.len()); // Save to file // std::fs::write("tokyo.geojson", json_string).unwrap(); } // Get preindex tile boundary for the timezone if let Some(index_boundary_file) = default_finder.fuzzy_finder.get_tz_geojson(&tz_name) { // Returns GeoJSON Feature with MultiPolygon geometry (map tiles) println!("Found Index GeoJSON feature for timezone: {}", tz_name); let tile_count = index_boundary_file.geometry.coordinates.len(); println!("Total number of tile polygons: {}", tile_count); // Convert to pretty JSON string let json_string = index_boundary_file.to_string_pretty(); println!("Pretty JSON preview:\n{}", &json_string[..json_string.len().min(500)]); } } ``` -------------------------------- ### Export All Timezones to GeoJSON in Rust Source: https://context7.com/ringsaturn/tzf-rs/llms.txt Exports all timezone boundaries from the tzf-rs dataset into a GeoJSON FeatureCollection format. This requires the 'export-geojson' feature to be enabled. It demonstrates exporting from Finder, FuzzyFinder, and DefaultFinder, and includes options for serializing to a JSON string and saving to a file. ```rust // Requires export-geojson feature use tzf_rs::{Finder, FuzzyFinder, DefaultFinder}; fn main() { // Export from Finder (precise polygon boundaries) let finder = Finder::new(); let geojson = finder.to_geojson(); println!("Type: {}", geojson.collection_type); println!("Number of features: {}", geojson.features.len()); if let Some(first_feature) = geojson.features.first() { println!("First timezone: {}", first_feature.properties.tzid); println!("Number of polygons: {}", first_feature.geometry.coordinates.len()); } // Serialize to JSON string let json_string = geojson.to_string(); println!("Total JSON size: {} bytes", json_string.len()); // Save to file // std::fs::write("all_timezones.geojson", json_string).unwrap(); // Export from FuzzyFinder (tile-based preindex) let fuzzy_finder = FuzzyFinder::new(); let fuzzy_geojson = fuzzy_finder.to_geojson(); println!("\nFuzzyFinder features: {}", fuzzy_geojson.features.len()); // Export from DefaultFinder (uses Finder data) let default_finder = DefaultFinder::new(); let default_geojson = default_finder.to_geojson(); println!("DefaultFinder features: {}", default_geojson.features.len()); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.