### Rust: Handling Daylight Saving Time differences Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Illustrates how different timezones (London and New York) can have different offsets during daylight saving transitions. This example highlights a 4-hour difference on specific dates. ```rust use chrono::TimeZone; use chrono_tz::Europe::London; use chrono_tz::America::New_York; let london_time = London.ymd(2016, 3, 18).and_hms(3, 0, 0); let ny_time = london_time.with_timezone(&New_York); assert_eq!(ny_time, New_York.ymd(2016, 3, 17).and_hms(23, 0, 0)); ``` -------------------------------- ### Rust: Convert local time to Unix timestamp Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Provides an example of converting a local time in a specific timezone (Asia Kolkata) to a Unix timestamp. This is useful for systems that rely on a universal time representation. ```rust use chrono::TimeZone; use chrono_tz::Asia::Kolkata; let dt = Kolkata.ymd(2000, 1, 1).and_hms(0, 0, 0); let timestamp = dt.timestamp(); assert_eq!(timestamp, 946665000); ``` -------------------------------- ### TOML: Enable optimizations for limited program space Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Configuration snippet for `Cargo.toml` to enable Link Time Optimization (LTO) and optimization levels for development and release profiles. This is recommended for `no_std` environments with limited program space to minimize binary size. ```toml [profile.dev] opt-level = 2 lto = true [profile.release] lto = true ``` -------------------------------- ### Initialize Git Submodules for Chrono-tz Development Source: https://github.com/chronotope/chrono-tz/blob/main/README.md This command is necessary for local development of the Chrono-tz project. Chrono-tz utilizes git submodules, and running these commands ensures that all necessary submodules are properly initialized and updated before building the project locally. ```shell git submodule init git submodule update ``` -------------------------------- ### Rust: Pretty-printing timezone-aware datetime Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Demonstrates how pretty-printing a timezone-aware datetime (London) using `to_string()` and `to_rfc3339()` automatically includes the correct timezone abbreviation (BST) and offset. ```rust use chrono::TimeZone; use chrono_tz::Europe::London; let dt = London.ymd(2016, 5, 10).and_hms(12, 0, 0); assert_eq!(dt.to_string(), "2016-05-10 12:00:00 BST"); assert_eq!(dt.to_rfc3339(), "2016-05-10T12:00:00+01:00"); ``` -------------------------------- ### Rust: Convert time to UTC Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Demonstrates how to create a time in a specific timezone (US Pacific) and then convert it to Coordinated Universal Time (UTC). This is useful for standardizing time representations. ```rust use chrono::{TimeZone, Utc}; use chrono_tz::US::Pacific; let pacific_time = Pacific.ymd(1990, 5, 6).and_hms(12, 30, 45); let utc_time = pacific_time.with_timezone(&Utc); assert_eq!(utc_time, Utc.ymd(1990, 5, 6).and_hms(19, 30, 45)); ``` -------------------------------- ### Rust: Convert naive datetime to timezone-aware Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Shows how to convert a naive datetime (without timezone information) into a timezone-aware datetime for a specific timezone (Africa Johannesburg). This is crucial for accurately representing local times. ```rust use chrono::{TimeZone, NaiveDate}; use chrono_tz::Africa::Johannesburg; let naive_dt = NaiveDate::from_ymd(2038, 1, 19).and_hms(3, 14, 08); let tz_aware = Johannesburg.from_local_datetime(&naive_dt).unwrap(); assert_eq!(tz_aware.to_string(), "2038-01-19 03:14:08 SAST"); ``` -------------------------------- ### Parse IANA Zoneinfo Database Files in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Parse raw IANA timezone database text files to extract zone rules and transitions using the `parse_zoneinfo` crate. This allows for programmatic understanding and processing of timezone definitions, supporting the creation of custom timezone tables or validation logic. It handles various line types like Zone, Rule, and Link. ```rust use parse_zoneinfo::line::{Line, LineParser}; use parse_zoneinfo::table::TableBuilder; let lines = vec![ "Zone Europe/London -0:01:15 - LMT 1847 Dec 1 0:00s", " 0:00 GB-Eire %s 1968 Oct 27", "Rule GB-Eire 1981 1995 - Mar lastSun 1:00u 1:00 BST", ]; let mut builder = TableBuilder::new(); for line_str in lines { match line_str.parse::() { Ok(Line::Zone(zone)) => { builder.add_zone_line(zone).unwrap(); } Ok(Line::Continuation(cont)) => { builder.add_continuation_line(cont).unwrap(); } Ok(Line::Rule(rule)) => { builder.add_rule_line(rule).unwrap(); } Ok(Line::Link(link)) => { builder.add_link_line(link).unwrap(); } _ => {} } } let table = builder.build(); ``` -------------------------------- ### TOML: Enable no_std support for chrono-tz Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Configuration snippet for `Cargo.toml` to enable `no_std` support for both `chrono` and `chrono-tz` libraries. This is essential for environments without the Rust standard library, reducing binary size. ```toml [dependencies] chrono = { version = "0.4", default-features = false } chrono-tz = { version = "0.5", default-features = false } ``` -------------------------------- ### Handle DST Transitions in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Calculates time adjustments across Daylight Saving Time (DST) boundaries, including clock shifts. It allows access to DST offset information for detailed analysis. ```rust use chrono::{TimeZone, Duration}; use chrono_tz::Europe::London; // Adding 24 hours across DST boundary results in different local time let dt = London.ymd(2016, 10, 29).and_hms(12, 0, 0); let later = dt + Duration::hours(24); assert_eq!(later, London.ymd(2016, 10, 30).and_hms(11, 0, 0)); // Access DST offset information use chrono_tz::OffsetComponents; let summer_time = London.ymd(2016, 5, 10).and_hms(12, 0, 0); assert_eq!(summer_time.offset().base_utc_offset(), Duration::hours(0)); assert_eq!(summer_time.offset().dst_offset(), Duration::hours(1)); ``` -------------------------------- ### Filter Timezones with Regex using CHRONO_TZ_TIMEZONE_FILTER Source: https://github.com/chronotope/chrono-tz/blob/main/README.md This command demonstrates how to build the Chrono-tz project while filtering the generated timezone database. It uses the CHRONO_TZ_TIMEZONE_FILTER environment variable with a regular expression to select specific timezones. This can reduce the size of the generated database. The filtering is applied liberally, including linked timezone entries. ```shell CHRONO_TZ_TIMEZONE_FILTER="(Europe/London|US/.*)" cargo build ``` -------------------------------- ### Rust: Accessing timezone offset components Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Demonstrates how to retrieve the base UTC offset and the daylight saving time (DST) offset for a given time in a specific timezone (London). Requires importing the `OffsetComponents` trait. ```rust use chrono::{Duration, TimeZone}; use chrono_tz::Europe::London; use chrono_tz::OffsetComponents; let london_time = London.ymd(2016, 5, 10).and_hms(12, 0, 0); // London typically has zero offset from UTC, but has a 1h adjustment forward // when summer time is in effect. assert_eq!(london_time.offset().base_utc_offset(), Duration::hours(0)); assert_eq!(london_time.offset().dst_offset(), Duration::hours(1)); ``` -------------------------------- ### Rust: Time addition across DST change Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Shows how adding a fixed duration (24 hours) across a daylight saving time change can result in a different local time. This highlights the non-uniform nature of time progression during DST transitions. ```rust use chrono::{TimeZone, Duration}; use chrono_tz::Europe::London; let dt = London.ymd(2016, 10, 29).and_hms(12, 0, 0); let later = dt + Duration::hours(24); assert_eq!(later, London.ymd(2016, 10, 30).and_hms(11, 0, 0)); ``` -------------------------------- ### Create Timezone-Aware DateTime from Naive DateTime in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Converts a naive datetime (without timezone info) into a timezone-aware datetime, applying the correct offset and abbreviation for the specified timezone. Handles potential errors during conversion. ```rust use chrono::{TimeZone, NaiveDate}; use chrono_tz::Africa::Johannesburg; let naive_dt = NaiveDate::from_ymd(2038, 1, 19).and_hms(3, 14, 08); let tz_aware = Johannesburg.from_local_datetime(&naive_dt).unwrap(); assert_eq!(tz_aware.to_string(), "2038-01-19 03:14:08 SAST"); assert_eq!(tz_aware.to_rfc3339(), "2038-01-19T03:14:08+02:00"); ``` -------------------------------- ### Filter Timezones to Reduce Binary Size with Regex Source: https://context7.com/chronotope/chrono-tz/llms.txt Minimize binary size in Rust applications by including only specific timezones at build time. This is achieved by setting the `CHRONO_TZ_TIMEZONE_FILTER` environment variable with a regex pattern before running `cargo build`. This requires enabling the `filter-by-regex` feature for the `chrono-tz` dependency. ```bash # Only include European and US timezones CHRONO_TZ_TIMEZONE_FILTER="(Europe/.*|US/.*)" cargo build # Only include a few specific cities CHRONO_TZ_TIMEZONE_FILTER="(Europe/London|America/New_York|Asia/Tokyo)" cargo build ``` -------------------------------- ### Format DateTime with Timezone Abbreviations in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Formats datetimes to include timezone abbreviations that accurately reflect the current DST status. Supports standard formatting and RFC3339 output. ```rust use chrono::TimeZone; use chrono_tz::Europe::London; // Summer time shows BST abbreviation let summer_dt = London.ymd(2016, 5, 10).and_hms(12, 0, 0); assert_eq!(summer_dt.to_string(), "2016-05-10 12:00:00 BST"); assert_eq!(summer_dt.to_rfc3339(), "2016-05-10T12:00:00+01:00"); // Winter time shows GMT abbreviation let winter_dt = London.ymd(2016, 1, 10).and_hms(12, 0, 0); assert_eq!(winter_dt.format("%Z").to_string(), "GMT"); ``` -------------------------------- ### Detect Non-Existent Times During DST Spring-Forward in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Identify and handle times that do not exist due to clock advancement during Daylight Saving Time transitions. This is crucial for validating user input or internal calculations where time consistency is important. It uses the `chrono` and `chrono_tz` crates. ```rust use chrono::TimeZone; use chrono_tz::Europe::London; // 01:30 doesn't exist on March 27, 2016 (clocks jump from 01:00 to 02:00) assert!(London.with_ymd_and_hms(2016, 3, 27, 1, 30, 0).single().is_none()); // Times before and after the gap are valid assert!(London.with_ymd_and_hms(2016, 3, 27, 0, 30, 0).single().is_some()); assert!(London.with_ymd_and_hms(2016, 3, 27, 2, 0, 0).single().is_some()); ``` -------------------------------- ### Configure No-Std Embedded Systems with chrono-tz Source: https://context7.com/chronotope/chrono-tz/llms.txt Utilize `chrono-tz` in embedded or `no_std` Rust environments by disabling default features. This involves specifying `default-features = false` for both `chrono` and `chrono-tz` in `Cargo.toml` and potentially configuring build profiles for optimizations like link-time optimization (LTO). ```toml [dependencies] chrono = { version = "0.4", default-features = false } chrono-tz = { version = "0.11", default-features = false } [profile.dev] opt-level = 2 lto = true [profile.release] lto = true ``` -------------------------------- ### Convert Time Between Timezones in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Converts a datetime from one timezone to another, ensuring DST awareness and historical accuracy. It utilizes chrono-tz's timezone implementations for accurate conversions. ```rust use chrono::{TimeZone, Utc}; use chrono_tz::US::Pacific; use chrono_tz::Europe::London; // Convert Pacific time to UTC let pacific_time = Pacific.ymd(1990, 5, 6).and_hms(12, 30, 45); let utc_time = pacific_time.with_timezone(&Utc); assert_eq!(utc_time, Utc.ymd(1990, 5, 6).and_hms(19, 30, 45)); // Convert London time to New York time across DST boundary use chrono_tz::America::New_York; let london_time = London.ymd(2016, 3, 18).and_hms(3, 0, 0); let ny_time = london_time.with_timezone(&New_York); assert_eq!(ny_time, New_York.ymd(2016, 3, 17).and_hms(23, 0, 0)); ``` -------------------------------- ### Filter Timezones to Reduce Binary Size with Regex (Cargo.toml) Source: https://context7.com/chronotope/chrono-tz/llms.txt Enable the `filter-by-regex` feature for the `chrono-tz` dependency in `Cargo.toml` to allow filtering timezones using regular expressions at build time. This works in conjunction with the `CHRONO_TZ_TIMEZONE_FILTER` environment variable. ```toml [dependencies] chrono-tz = { version = "0.11", features = ["filter-by-regex"] } ``` -------------------------------- ### Iterate Over All Timezones in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Access all available timezone variants for enumeration and validation. This allows checking for specific timezones, counting available ones, or creating selection interfaces. It uses the `TZ_VARIANTS` static collection provided by the `chrono_tz` crate. ```rust use chrono_tz::{TZ_VARIANTS, Tz}; // Check if a specific timezone exists assert!(TZ_VARIANTS.iter().any(|v| *v == Tz::UTC)); assert!(TZ_VARIANTS.iter().any(|v| *v == Tz::America__New_York)); // Count available timezones let timezone_count = TZ_VARIANTS.len(); println!("Available timezones: {}", timezone_count); // Create a timezone picker for tz in TZ_VARIANTS.iter().take(5) { println!("{}", tz.name()); } ``` -------------------------------- ### Rust: Parse timezone string to Tz object Source: https://github.com/chronotope/chrono-tz/blob/main/README.md Shows how to parse a timezone string (e.g., 'Antarctica/South_Pole') into a `Tz` object using the `FromStr` trait. This allows dynamic selection of timezones based on string input. ```rust use chrono::TimeZone; use chrono_tz::Tz; use chrono_tz::UTC; let tz: Tz = "Antarctica/South_Pole".parse().unwrap(); let dt = tz.ymd(2016, 10, 22).and_hms(12, 0, 0); let utc = dt.with_timezone(&UTC); assert_eq!(utc.to_string(), "2016-10-21 23:00:00 UTC"); ``` -------------------------------- ### Handle Ambiguous Times During DST Fallback in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Resolve ambiguous local times that occur twice when clocks fall back during Daylight Saving Time transitions. This function helps in selecting either the earlier or later occurrence of a time that happens twice in a day. It requires the `chrono` and `chrono_tz` crates. ```rust use chrono::{TimeZone, NaiveDate}; use chrono_tz::Europe::London; // 01:30 occurs twice on Oct 30, 2016 in London let ambiguous = London.with_ymd_and_hms(2016, 10, 30, 1, 30, 0); // Get the earlier occurrence (still in BST) let earliest_utc = NaiveDate::from_ymd_opt(2016, 10, 30) .unwrap() .and_hms_opt(0, 30, 0) .unwrap(); assert_eq!( ambiguous.earliest().unwrap(), London.from_utc_datetime(&earliest_utc) ); // Get the later occurrence (now in GMT) let latest_utc = NaiveDate::from_ymd_opt(2016, 10, 30) .unwrap() .and_hms_opt(1, 30, 0) .unwrap(); assert_eq!( ambiguous.latest().unwrap(), London.from_utc_datetime(&latest_utc) ); ``` -------------------------------- ### Parse Timezone from String in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Parses timezone identifiers from string representations and uses them to create timezone-aware datetimes. It also allows retrieval of the timezone name. ```rust use chrono::TimeZone; use chrono_tz::Tz; use chrono_tz::UTC; let tz: Tz = "Antarctica/South_Pole".parse().unwrap(); let dt = tz.ymd(2016, 10, 22).and_hms(12, 0, 0); let utc = dt.with_timezone(&UTC); assert_eq!(utc.to_string(), "2016-10-21 23:00:00 UTC"); // Get timezone name and display let london = "Europe/London".parse::().unwrap(); assert_eq!(london.name(), "Europe/London"); assert_eq!(format!("{}", london), "Europe/London"); ``` -------------------------------- ### Convert Timezone-Aware DateTime to Unix Timestamp in Rust Source: https://context7.com/chronotope/chrono-tz/llms.txt Converts timezone-aware datetimes into Unix timestamps, suitable for data storage or API compatibility. This conversion is independent of the timezone, representing the absolute point in time. ```rust use chrono::TimeZone; use chrono_tz::Asia::Kolkata; let dt = Kolkata.ymd(2000, 1, 1).and_hms(0, 0, 0); let timestamp = dt.timestamp(); assert_eq!(timestamp, 946665000); // Timestamps preserve absolute time across timezone conversions use chrono_tz::US::Eastern; let eastern_dt = Eastern.ymd(1999, 12, 31).and_hms(19, 0, 0); assert_eq!(eastern_dt.timestamp(), 946665000); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.