### Constructing and Using `UtcOffset` Objects Source: https://context7.com/time-rs/time/llms.txt Illustrates how to create `UtcOffset` values using `from_hms` and `from_whole_seconds`, and demonstrates accessing offset components. Includes examples of compile-time macros for offset creation. ```rust use time::UtcOffset; use time::macros::offset; // Construction let o1 = UtcOffset::from_hms(5, 30, 0)?; // India Standard Time let o2 = UtcOffset::from_hms(-5, 0, 0)?; // US Eastern Standard Time let o3 = UtcOffset::from_whole_seconds(19800)?; // 5*3600 + 30*60 assert_eq!(o1, o3); assert_eq!(o1.as_hms(), (5, 30, 0)); assert_eq!(o1.whole_hours(), 5); assert_eq!(o1.minutes_past_hour(), 30); assert_eq!(o1.seconds_past_minute(), 0); assert_eq!(o1.whole_seconds_unchecked(), 19800); // Constant assert_eq!(UtcOffset::UTC.whole_seconds_unchecked(), 0); assert!(UtcOffset::UTC.is_utc()); assert!(o2.is_negative()); assert!(o1.is_positive()); // Compile-time macros (requires `macros` feature) const IST: UtcOffset = offset!(+5:30); const EST: UtcOffset = offset!(-5); const UTC: UtcOffset = offset!(UTC); const UTC2: UtcOffset = offset!(utc); const FINE: UtcOffset = offset!(+1:30:59); // Local offset (requires `local-offset` feature) // let local = UtcOffset::local_offset_at(OffsetDateTime::now_utc())?; ``` -------------------------------- ### OffsetDateTime Construction and Usage Source: https://context7.com/time-rs/time/llms.txt Demonstrates how to construct and use OffsetDateTime for timezone-aware date and time values. Includes examples of current time, manual construction, compile-time constants, offset conversions, and UNIX timestamp interop. ```rust use time::{OffsetDateTime, Date, Time, Month, UtcOffset}; use time::macros::{datetime, offset}; // Current time let now_utc = OffsetDateTime::now_utc(); assert!(now_utc.year() >= 2024); assert_eq!(now_utc.offset(), offset!(UTC)); // Construction let dt = OffsetDateTime::new_in_offset( Date::from_calendar_date(2024, Month::June, 15)?, Time::from_hms(9, 0, 0)?, UtcOffset::from_hms(-4, 0, 0)?, ); assert_eq!(dt.year(), 2024); assert_eq!(dt.month(), Month::June); assert_eq!(dt.day(), 15); assert_eq!(dt.hour(), 9); assert_eq!(dt.offset(), offset!(-4)); // Compile-time constant const LAUNCH: OffsetDateTime = datetime!(2024-01-15 08:00:00 UTC); // Offset conversion let sydney = datetime!(2000-01-01 0:00 +11); let new_york = sydney.to_offset(offset!(-5)); let los_angeles = sydney.to_offset(offset!(-8)); assert_eq!(sydney.hour(), 0); assert_eq!(new_york.hour(), 8); // Sydney midnight = 8am prior day in NYC assert_eq!(new_york.year(), 1999); assert_eq!(los_angeles.hour(), 5); // Safe checked conversion let maybe = sydney.checked_to_offset(offset!(-5)); assert!(maybe.is_some()); // Convert to UtcDateTime let utc_dt = dt.to_utc(); // panics if out of range let utc_maybe = dt.checked_to_utc(); // returns Option // UNIX timestamp interop let epoch = OffsetDateTime::UNIX_EPOCH; assert_eq!(epoch.unix_timestamp(), 0); assert_eq!(epoch.unix_timestamp_nanos(), 0); let from_unix = OffsetDateTime::from_unix_timestamp(1_718_000_000)?; assert_eq!(from_unix.year(), 2024); // Arithmetic let later = dt + time::Duration::hours(3); assert_eq!(later.hour(), 12); let diff: time::Duration = later - dt; assert_eq!(diff, time::Duration::hours(3)); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Formatting Dates and Times Source: https://context7.com/time-rs/time/llms.txt Demonstrates formatting dates and times using well-known formats like RFC3339 and RFC2822, as well as custom format descriptions parsed at runtime or compile time. Also shows formatting into a writer and using strftime-style formats. ```rust use time::format_description::well_known::{Rfc3339, Rfc2822}; use time::format_description; use time::macros::{datetime, format_description as fd}; let dt = datetime!(1985-04-12 23:20:50.52 +00:00); // Well-known formats let rfc3339 = dt.format(&Rfc3339)?; assert_eq!(rfc3339, "1985-04-12T23:20:50.52Z"); let rfc2822 = dt.format(&Rfc2822)?; assert_eq!(rfc2822, "Fri, 12 Apr 1985 23:20:50 +0000"); // Custom format description (runtime-parsed, requires alloc) let fmt = format_description::parse_borrowed::<2>("[year]-[month]-[day]")?; let date_str = dt.format(&fmt)?; assert_eq!(date_str, "1985-04-12"); // Custom format description (compile-time macro) const MY_FMT: &[time::format_description::BorrowedFormatItem<'_>] = fd!("[hour]:[minute]:[second]"); let time_str = dt.format(MY_FMT)?; assert_eq!(time_str, "23:20:50"); // Format to a writer (no allocation) let mut buf = Vec::new(); dt.format_into(&mut buf, &Rfc3339)?; assert_eq!(buf, b"1985-04-12T23:20:50.52Z"); // strftime-style format (runtime) let fmt2 = format_description::parse_strftime_borrowed::<2>("%Y-%m-%d %H:%M:%S")?; let s = dt.format(&fmt2)?; assert_eq!(s, "1985-04-12 23:20:50"); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Constructing and Using `Time` Objects Source: https://context7.com/time-rs/time/llms.txt Demonstrates various ways to construct `Time` objects with different precision levels (hours, minutes, seconds, milliseconds, microseconds, nanoseconds). Includes error handling for invalid values and accessing time components. ```rust use time::Time; use time::macros::time; // Construction let t1 = Time::from_hms(14, 30, 0)?; // 14:30:00 let t2 = Time::from_hms_milli(14, 30, 0, 500)?; // 14:30:00.500 let t3 = Time::from_hms_micro(14, 30, 0, 500_000)?; // 14:30:00.500000 let t4 = Time::from_hms_nano(14, 30, 0, 500_000_000)?; // 14:30:00.5 assert_eq!(t2, t3); assert_eq!(t3, t4); // Errors on invalid values assert!(Time::from_hms(24, 0, 0).is_err()); // hour out of range assert!(Time::from_hms(0, 60, 0).is_err()); // minute out of range // Accessors assert_eq!(t4.hour(), 14); assert_eq!(t4.minute(), 30); assert_eq!(t4.second(), 0); assert_eq!(t4.nanosecond(), 500_000_000); assert_eq!(t4.millisecond(), 500); assert_eq!(t4.microsecond(), 500_000); // Constants assert_eq!(Time::MIDNIGHT, time!(0:00)); assert_eq!(Time::MAX, time!(23:59:59.999_999_999)); // Compile-time macro (requires `macros` feature) const NOON: Time = time!(12:00); // AM/PM macro syntax let am = time!(9:30 am); let pm = time!(9:30 pm); assert_eq!(am, Time::from_hms(9, 30, 0)?); assert_eq!(pm, Time::from_hms(21, 30, 0)?); // Arithmetic — wraps around midnight, returns date adjustment let (t5, day_adj) = t4 + time::Duration::hours(10); // wraps past midnight assert_eq!(day_adj, time::util::DateAdjustment::Next); assert_eq!(t5, time!(0:30)); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Construct and Manipulate Date Objects Source: https://context7.com/time-rs/time/llms.txt Demonstrates various methods for constructing `Date` objects from calendar, ordinal, ISO week, and Julian day values. Includes navigation and arithmetic operations. ```rust use time::{Date, Month, Weekday}; use time::macros::date; // Construct from year-month-day let d1 = Date::from_calendar_date(2024, Month::March, 15)?; assert_eq!(d1.year(), 2024); assert_eq!(d1.month(), Month::March); assert_eq!(d1.day(), 15); assert_eq!(d1.weekday(), Weekday::Friday); // Construct from ordinal day (day-of-year) let d2 = Date::from_ordinal_date(2024, 75)?; assert_eq!(d2, d1); assert_eq!(d2.ordinal(), 75); // Construct from ISO week date let d3 = Date::from_iso_week_date(2024, 11, Weekday::Friday)?; assert_eq!(d3, d1); let (iso_year, iso_week, iso_weekday) = d3.to_iso_week_date(); assert_eq!((iso_year, iso_week, iso_weekday), (2024, 11, Weekday::Friday)); // Construct from Julian day number let d4 = Date::from_julian_day(2_460_385)?; assert_eq!(d4, d1); assert_eq!(d4.to_julian_day(), 2_460_385); // Compile-time macro (requires `macros` feature) const EPOCH: Date = date!(1970-01-01); assert_eq!(EPOCH.to_calendar_date(), (1970, Month::January, 1)); // Navigation let next = d1.next_day().unwrap(); assert_eq!(next, date!(2024-03-16)); let prev = d1.previous_day().unwrap(); assert_eq!(prev, date!(2024-03-14)); // Find next/previous occurrence of a weekday let next_monday = d1.next_occurrence(Weekday::Monday); assert_eq!(next_monday, date!(2024-03-18)); // Arithmetic let later = d1 + time::Duration::days(10); assert_eq!(later, date!(2024-03-25)); let diff = later - d1; assert_eq!(diff, time::Duration::days(10)); // Week numbers assert_eq!(d1.sunday_based_week(), 10); assert_eq!(d1.monday_based_week(), 10); assert_eq!(d1.iso_week(), 11); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Date - Calendar Date Source: https://context7.com/time-rs/time/llms.txt Demonstrates the usage of the `Date` type for representing calendar dates. It covers construction from year-month-day, ordinal day, ISO week date, Julian day, and compile-time macros. It also shows navigation, arithmetic, and week number calculations. ```APIDOC ## Date - Calendar Date `Date` represents a date in the proleptic Gregorian calendar. Years between ±9,999 are supported by default (±999,999 with `large-dates`). ```rust use time::{Date, Month, Weekday}; use time::macros::date; // Construct from year-month-day let d1 = Date::from_calendar_date(2024, Month::March, 15)?; assert_eq!(d1.year(), 2024); assert_eq!(d1.month(), Month::March); assert_eq!(d1.day(), 15); assert_eq!(d1.weekday(), Weekday::Friday); // Construct from ordinal day (day-of-year) let d2 = Date::from_ordinal_date(2024, 75)?; assert_eq!(d2, d1); assert_eq!(d2.ordinal(), 75); // Construct from ISO week date let d3 = Date::from_iso_week_date(2024, 11, Weekday::Friday)?; assert_eq!(d3, d1); let (iso_year, iso_week, iso_weekday) = d3.to_iso_week_date(); assert_eq!((iso_year, iso_week, iso_weekday), (2024, 11, Weekday::Friday)); // Construct from Julian day number let d4 = Date::from_julian_day(2_460_385)?; assert_eq!(d4, d1); assert_eq!(d4.to_julian_day(), 2_460_385); // Compile-time macro (requires `macros` feature) const EPOCH: Date = date!(1970-01-01); assert_eq!(EPOCH.to_calendar_date(), (1970, Month::January, 1)); // Navigation let next = d1.next_day().unwrap(); assert_eq!(next, date!(2024-03-16)); let prev = d1.previous_day().unwrap(); assert_eq!(prev, date!(2024-03-14)); // Find next/previous occurrence of a weekday let next_monday = d1.next_occurrence(Weekday::Monday); assert_eq!(next_monday, date!(2024-03-18)); // Arithmetic let later = d1 + time::Duration::days(10); assert_eq!(later, date!(2024-03-25)); let diff = later - d1; assert_eq!(diff, time::Duration::days(10)); // Week numbers assert_eq!(d1.sunday_based_week(), 10); assert_eq!(d1.monday_based_week(), 10); assert_eq!(d1.iso_week(), 11); # Ok::<_, time::Error>(()) ``` ``` -------------------------------- ### Constructing and Using `Duration` Objects Source: https://context7.com/time-rs/time/llms.txt Shows how to create `Duration` objects using static methods and the `NumericalDuration` trait for ergonomic construction. Covers negative durations and accessing duration components. ```rust use time::{Duration, ext::NumericalDuration}; // Construction via static methods let d1 = Duration::seconds(90); let d2 = Duration::minutes(1) + Duration::seconds(30); assert_eq!(d1, d2); // Ergonomic construction via NumericalDuration trait let d3 = 2.hours() + 30.minutes() + 15.seconds(); assert_eq!(d3.whole_hours(), 2); assert_eq!(d3.whole_minutes(), 150); assert_eq!(d3.whole_seconds(), 9015); // Negative durations let neg = (-5).seconds(); assert!(neg.is_negative()); assert_eq!(neg.abs(), 5.seconds()); assert_eq!(neg.whole_seconds(), -5); // All construction methods let _ = Duration::weeks(2); let _ = Duration::days(14); let _ = Duration::hours(336); let _ = Duration::milliseconds(1_209_600_000); let _ = Duration::microseconds(1_209_600_000_000); let _ = Duration::nanoseconds(1_209_600_000_000_000); let _ = Duration::nanoseconds_i128(1_209_600_000_000_000_i128); let _ = Duration::seconds_f64(1.5); // panics on NaN or overflow let _ = Duration::seconds_f32(1.5); let _ = Duration::saturating_seconds_f64(f64::INFINITY); // → Duration::MAX let _ = Duration::checked_seconds_f64(f64::NAN); // → None // Component accessors let d = 1.days() + 2.hours() + 3.minutes() + 4.seconds() + 5.milliseconds(); assert_eq!(d.whole_days(), 1); assert_eq!(d.whole_hours(), 26); assert_eq!(d.whole_minutes(), 1563); assert_eq!(d.whole_seconds(), 93784); assert_eq!(d.whole_milliseconds(), 93_784_005); assert_eq!(d.whole_microseconds(), 93_784_005_000); assert_eq!(d.whole_nanoseconds(), 93_784_005_000_000); assert_eq!(d.subsec_milliseconds(), 5); assert_eq!(d.subsec_microseconds(), 5_000); assert_eq!(d.subsec_nanoseconds(), 5_000_000); // Constants assert_eq!(Duration::ZERO.whole_seconds(), 0); assert!(Duration::ZERO.is_zero()); let _ = Duration::NANOSECOND; let _ = Duration::MICROSECOND; let _ = Duration::MILLISECOND; let _ = Duration::SECOND; let _ = Duration::MINUTE; let _ = Duration::HOUR; let _ = Duration::DAY; let _ = Duration::WEEK; let _ = Duration::MIN; let _ = Duration::MAX; // Arithmetic operators: +, -, *, /, Neg let doubled = 5.seconds() * 2; assert_eq!(doubled, 10.seconds()); let halved = 10.seconds() / 2; assert_eq!(halved, 5.seconds()); ``` -------------------------------- ### UtcDateTime Construction and Usage Source: https://context7.com/time-rs/time/llms.txt Shows how to create and use UtcDateTime for UTC-only date and time values. Covers current time, construction, compile-time macros, accessors, UNIX timestamp conversion, and conversion to OffsetDateTime. ```rust use time::UtcDateTime; use time::macros::{date, time, utc_datetime}; // Current time in UTC let now = UtcDateTime::now(); assert!(now.year() >= 2024); // Construction let dt = UtcDateTime::new(date!(2024-06-15), time!(9:00)); // Compile-time macro const EPOCH: UtcDateTime = utc_datetime!(1970-01-01 0:00); assert_eq!(UtcDateTime::UNIX_EPOCH, EPOCH); // Accessors (same as PrimitiveDateTime / OffsetDateTime) assert_eq!(dt.year(), 2024); assert_eq!(dt.month(), time::Month::June); assert_eq!(dt.day(), 15); assert_eq!(dt.hour(), 9); assert_eq!(dt.minute(), 0); // UNIX timestamp assert_eq!(UtcDateTime::UNIX_EPOCH.unix_timestamp(), 0); let from_unix = UtcDateTime::from_unix_timestamp(1_718_000_000)?; assert!(from_unix.year() == 2024); // Convert to OffsetDateTime with an offset let odt = dt.assume_offset(time::UtcOffset::from_hms(5, 30, 0)?); assert_eq!(odt.hour(), 9); assert_eq!(odt.offset().whole_hours(), 5); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Calendar Utilities: Days and Weeks Calculation Source: https://context7.com/time-rs/time/llms.txt Provides utility functions for calculating the number of days in a specific month and year, and the number of weeks in an ISO year. Also includes a function to check if a year is a leap year. These functions are useful for calendar-related computations. ```rust use time::util; // Days in a month assert_eq!(util::days_in_month(time::Month::February, 2024), 29); // leap year assert_eq!(util::days_in_month(time::Month::February, 2023), 28); assert_eq!(util::days_in_month(time::Month::January, 2024), 31); // Days in a year assert_eq!(util::days_in_year(2024), 366); assert_eq!(util::days_in_year(2023), 365); // Weeks in an ISO year assert_eq!(util::weeks_in_year(2020), 53); assert_eq!(util::weeks_in_year(2019), 52); // Leap year check assert!(util::is_leap_year(2024)); assert!(!util::is_leap_year(2023)); ``` -------------------------------- ### Parsing Dates and Times Source: https://context7.com/time-rs/time/llms.txt Illustrates parsing date and time strings using well-known formats (RFC3339, RFC2822) and custom format descriptions. Supports parsing into OffsetDateTime, Date, Time, and PrimitiveDateTime, including strftime-compatible parsing. ```rust use time::format_description::well_known::{Rfc3339, Rfc2822}; use time::{OffsetDateTime, Date, Time, PrimitiveDateTime}; use time::format_description; use time::macros::{datetime, format_description as fd}; // Well-known formats let dt = OffsetDateTime::parse("1985-04-12T23:20:50.52Z", &Rfc3339)?; assert_eq!(dt, datetime!(1985-04-12 23:20:50.52 +00:00)); let dt2 = OffsetDateTime::parse("Sat, 12 Jun 1993 13:25:19 GMT", &Rfc2822)?; assert_eq!(dt2, datetime!(1993-06-12 13:25:19 +00:00)); // Custom format (runtime) let fmt = format_description::parse_borrowed::<2>("[year]-[month]-[day]")?; let d = Date::parse("2024-06-15", &fmt)?; assert_eq!(d, time::macros::date!(2024-06-15)); // Custom format (compile-time macro) const FMT: &[time::format_description::BorrowedFormatItem<'_>] = fd!("[hour]:[minute]:[second]"); let t = Time::parse("14:30:59", FMT)?; assert_eq!(t, time::macros::time!(14:30:59)); // Parse PrimitiveDateTime let pdt_fmt = format_description::parse_borrowed::<2>("[year]-[month]-[day] [hour]:[minute]:[second]")?; let pdt = PrimitiveDateTime::parse("2024-06-15 14:30:59", &pdt_fmt)?; assert_eq!(pdt.year(), 2024); // strftime-compatible parsing let fmt2 = format_description::parse_strftime_borrowed::<2>("%Y-%m-%d")?; let d2 = Date::parse("2024-06-15", &fmt2)?; assert_eq!(d2, time::macros::date!(2024-06-15)); # Ok::<_, time::Error>(()) ``` -------------------------------- ### Configure time Crate Features Source: https://context7.com/time-rs/time/llms.txt Enable desired features for the time crate in your Cargo.toml file to control API availability. ```toml [dependencies] time = { version = "0.3", features = ["macros", "formatting", "parsing", "local-offset", "serde-human-readable"] } ``` -------------------------------- ### Serde Integration with RFC 3339 Timestamps Source: https://context7.com/time-rs/time/llms.txt Demonstrates serializing and deserializing Rust structs containing `OffsetDateTime` using the `serde` and `serde-human-readable` features. Ensure `time` is configured with `serde-human-readable` and `serde` with the `derive` feature. The `#[serde(with = "time::serde::rfc3339")]` attribute is crucial for correct formatting. ```rust // Cargo.toml: // time = { version = "0.3", features = ["macros", "serde-human-readable"] } // serde = { version = "1", features = ["derive"] } // serde_json = "1" use time::OffsetDateTime; use time::macros::datetime; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, PartialEq)] struct Event { name: String, #[serde(with = "time::serde::rfc3339")] timestamp: OffsetDateTime, } let event = Event { name: "launch".to_string(), timestamp: datetime!(2024-06-15 14:30:00 UTC), }; let json = serde_json::to_string(&event).unwrap(); // {"name":"launch","timestamp":"2024-06-15T14:30:00Z"} let decoded: Event = serde_json::from_str(&json).unwrap(); assert_eq!(decoded, event); // Optional timestamps via serde::rfc3339::option #[derive(Serialize, Deserialize)] struct OptEvent { #[serde(with = "time::serde::rfc3339::option")] timestamp: Option, } ``` -------------------------------- ### PrimitiveDateTime Construction and Arithmetic Source: https://context7.com/time-rs/time/llms.txt Illustrates the creation and manipulation of PrimitiveDateTime, which represents a date and time without timezone information. Includes construction, compile-time constants, accessors, timezone attachment, and arithmetic operations. ```rust use time::PrimitiveDateTime; use time::macros::{date, time, datetime}; // Construction let dt = PrimitiveDateTime::new(date!(2024-06-15), time!(14:30:00)); // Compile-time macro const BOOT: PrimitiveDateTime = datetime!(2024-01-01 0:00); // Accessors assert_eq!(dt.date(), date!(2024-06-15)); assert_eq!(dt.time(), time!(14:30:00)); assert_eq!(dt.year(), 2024); assert_eq!(dt.month(), time::Month::June); assert_eq!(dt.day(), 15); assert_eq!(dt.hour(), 14); assert_eq!(dt.minute(), 30); // Attach a timezone let utc = dt.assume_utc(); // → OffsetDateTime with UTC offset let odt = dt.assume_offset(time::macros::offset!(+5:30)); // → OffsetDateTime with IST let utc_dt = dt.as_utc(); // → UtcDateTime // Arithmetic let later = dt + time::Duration::days(7); assert_eq!(later.day(), 22); let diff = later - dt; assert_eq!(diff, time::Duration::days(7)); ``` -------------------------------- ### Month and Weekday Enum Operations Source: https://context7.com/time-rs/time/llms.txt Demonstrates arithmetic and conversion operations for Month and Weekday enums. Month enum supports next, previous, and length calculations. Weekday enum supports numbering and conversion. ```rust use time::{Month, Weekday}; // Month arithmetic let jan = Month::January; assert_eq!(jan.next(), Month::February); assert_eq!(jan.previous(), Month::December); assert_eq!(jan as u8, 1); // Length of month assert_eq!(Month::February.length(2024), 29); // 2024 is a leap year assert_eq!(Month::February.length(2023), 28); assert_eq!(Month::January.length(2024), 31); // Weekday numbering let mon = Weekday::Monday; assert_eq!(mon.number_from_monday(), 1); assert_eq!(mon.number_from_sunday(), 2); assert_eq!(mon.number_days_from_monday(), 0); assert_eq!(mon.number_days_from_sunday(), 1); let fri = Weekday::Friday; assert_eq!(fri.next(), Weekday::Saturday); assert_eq!(fri.previous(), Weekday::Thursday); // Conversion from number let w = Weekday::from_number_from_monday(5); // Friday (1=Mon ... 7=Sun) assert_eq!(w, Weekday::Friday); ``` -------------------------------- ### Compile-Time Date and Time Macros Source: https://context7.com/time-rs/time/llms.txt Utilizes compile-time macros for creating date, time, datetime, and offset values. These macros validate inputs at compile time and produce const-compatible expressions. ```rust use time::macros::{date, time, datetime, offset, utc_datetime, format_description}; // date! — three syntaxes const D1: time::Date = date!(2024-06-15); // year-month-day const D2: time::Date = date!(2024-167); // year-ordinal const D3: time::Date = date!(2024-W24-6); // year-ISO week-weekday // time! — with subseconds and AM/PM const T1: time::Time = time!(14:30); const T2: time::Time = time!(14:30:59.123_456_789); const T3: time::Time = time!(2:30 pm); // = 14:30 // datetime! — PrimitiveDateTime or OffsetDateTime const PDT: time::PrimitiveDateTime = datetime!(2024-06-15 14:30); const ODT: time::OffsetDateTime = datetime!(2024-06-15 14:30 UTC); const ODT2: time::OffsetDateTime = datetime!(2024-06-15 14:30 +5:30); // utc_datetime! const UDT: time::UtcDateTime = utc_datetime!(2024-06-15 14:30); // offset! const UTC_OFF: time::UtcOffset = offset!(UTC); const IST: time::UtcOffset = offset!(+5:30); const NY_OFF: time::UtcOffset = offset!(-5); const FINE: time::UtcOffset = offset!(+1:30:59); // format_description! — compile-time format parsing (requires formatting or parsing feature) const FMT: &[time::format_description::BorrowedFormatItem<'_>] = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); ``` -------------------------------- ### UtcOffset - UTC Offset Source: https://context7.com/time-rs/time/llms.txt The `UtcOffset` struct represents an offset from UTC, ranging from ±25:59:59. It can be constructed from hours, minutes, and seconds, or from whole seconds. It provides accessors for its components and constants like `UTC`. Macros are available for compile-time construction. ```APIDOC ## `UtcOffset` — UTC Offset `UtcOffset` stores an offset from UTC in the range ±25:59:59. ```rust use time::UtcOffset; use time::macros::offset; // Construction let o1 = UtcOffset::from_hms(5, 30, 0)?; // India Standard Time let o2 = UtcOffset::from_hms(-5, 0, 0)?; // US Eastern Standard Time let o3 = UtcOffset::from_whole_seconds(19800)?; // 5*3600 + 30*60 assert_eq!(o1, o3); assert_eq!(o1.as_hms(), (5, 30, 0)); assert_eq!(o1.whole_hours(), 5); assert_eq!(o1.minutes_past_hour(), 30); assert_eq!(o1.seconds_past_minute(), 0); assert_eq!(o1.whole_seconds_unchecked(), 19800); // Constant assert_eq!(UtcOffset::UTC.whole_seconds_unchecked(), 0); assert!(UtcOffset::UTC.is_utc()); assert!(o2.is_negative()); assert!(o1.is_positive()); // Compile-time macros (requires `macros` feature) const IST: UtcOffset = offset!(+5:30); const EST: UtcOffset = offset!(-5); const UTC: UtcOffset = offset!(UTC); const UTC2: UtcOffset = offset!(utc); const FINE: UtcOffset = offset!(+1:30:59); // Local offset (requires `local-offset` feature) // let local = UtcOffset::local_offset_at(OffsetDateTime::now_utc())?; ``` ``` -------------------------------- ### Time - Wall-Clock Time Source: https://context7.com/time-rs/time/llms.txt The `Time` struct represents the time of day with nanosecond precision. It supports construction from hours, minutes, seconds, milliseconds, microseconds, and nanoseconds. It also provides accessors for these components and constants like `MIDNIGHT` and `MAX`. Macros are available for compile-time construction and AM/PM syntax. ```APIDOC ## `Time` — Wall-Clock Time `Time` stores the time of day with nanosecond precision. All methods assume 60 seconds per minute (no leap seconds). ```rust use time::Time; use time::macros::time; // Construction let t1 = Time::from_hms(14, 30, 0)?; // 14:30:00 let t2 = Time::from_hms_milli(14, 30, 0, 500)?; // 14:30:00.500 let t3 = Time::from_hms_micro(14, 30, 0, 500_000)?; // 14:30:00.500000 let t4 = Time::from_hms_nano(14, 30, 0, 500_000_000)?; // 14:30:00.5 assert_eq!(t2, t3); assert_eq!(t3, t4); // Errors on invalid values assert!(Time::from_hms(24, 0, 0).is_err()); // hour out of range assert!(Time::from_hms(0, 60, 0).is_err()); // minute out of range // Accessors assert_eq!(t4.hour(), 14); assert_eq!(t4.minute(), 30); assert_eq!(t4.second(), 0); assert_eq!(t4.nanosecond(), 500_000_000); assert_eq!(t4.millisecond(), 500); assert_eq!(t4.microsecond(), 500_000); // Constants assert_eq!(Time::MIDNIGHT, time!(0:00)); assert_eq!(Time::MAX, time!(23:59:59.999_999_999)); // Compile-time macro (requires `macros` feature) const NOON: Time = time!(12:00); // AM/PM macro syntax let am = time!(9:30 am); let pm = time!(9:30 pm); assert_eq!(am, Time::from_hms(9, 30, 0)?); assert_eq!(pm, Time::from_hms(21, 30, 0)?); // Arithmetic — wraps around midnight, returns date adjustment let (t5, day_adj) = t4 + time::Duration::hours(10); // wraps past midnight assert_eq!(day_adj, time::util::DateAdjustment::Next); assert_eq!(t5, time!(0:30)); # Ok::<_, time::Error>(()) ``` ``` -------------------------------- ### OffsetDateTime Source: https://context7.com/time-rs/time/llms.txt Represents a datetime with timezone information. Equality and ordering are based on the UTC instant. ```APIDOC ## `OffsetDateTime` — Timezone-Aware Datetime `OffsetDateTime` combines a `PrimitiveDateTime` with a `UtcOffset`. Equality and ordering are based on the UTC instant, not the local representation. ### Construction ```rust use time::{OffsetDateTime, Date, Time, Month, UtcOffset}; use time::macros::{datetime, offset}; // Current time let now_utc = OffsetDateTime::now_utc(); // Construction with specific date, time, and offset let dt = OffsetDateTime::new_in_offset( Date::from_calendar_date(2024, Month::June, 15)?, Time::from_hms(9, 0, 0)?, UtcOffset::from_hms(-4, 0, 0)?, ); // Compile-time constant const LAUNCH: OffsetDateTime = datetime!(2024-01-15 08:00:00 UTC); // Offset conversion let sydney = datetime!(2000-01-01 0:00 +11); let new_york = sydney.to_offset(offset!(-5)); // Safe checked conversion let maybe = sydney.checked_to_offset(offset!(-5)); // Convert to UtcDateTime let utc_dt = dt.to_utc(); // panics if out of range let utc_maybe = dt.checked_to_utc(); // returns Option // UNIX timestamp interop let epoch = OffsetDateTime::UNIX_EPOCH; let from_unix = OffsetDateTime::from_unix_timestamp(1_718_000_000)?; // Arithmetic let later = dt + time::Duration::hours(3); let diff: time::Duration = later - dt; ``` ``` -------------------------------- ### UtcDateTime Source: https://context7.com/time-rs/time/llms.txt Represents a datetime that is guaranteed to be in UTC. It is ABI-compatible with PrimitiveDateTime. ```APIDOC ## `UtcDateTime` — UTC-Only Datetime `UtcDateTime` is a `PrimitiveDateTime` that is guaranteed to be in UTC. It is ABI-compatible with `PrimitiveDateTime`. ### Construction ```rust use time::UtcDateTime; use time::macros::{date, time, utc_datetime}; // Current time in UTC let now = UtcDateTime::now(); // Construction with date and time let dt = UtcDateTime::new(date!(2024-06-15), time!(9:00)); // Compile-time macro const EPOCH: UtcDateTime = utc_datetime!(1970-01-01 0:00); // UNIX timestamp let from_unix = UtcDateTime::from_unix_timestamp(1_718_000_000)?; // Convert to OffsetDateTime with an offset let odt = dt.assume_offset(time::UtcOffset::from_hms(5, 30, 0)?); ``` ``` -------------------------------- ### PrimitiveDateTime Source: https://context7.com/time-rs/time/llms.txt Represents a combined date and time without timezone information (naive datetime). ```APIDOC ## `PrimitiveDateTime` — Naive Datetime `PrimitiveDateTime` is a combined date and time without timezone information. ### Construction ```rust use time::PrimitiveDateTime; use time::macros::{date, time, datetime}; // Construction with date and time let dt = PrimitiveDateTime::new(date!(2024-06-15), time!(14:30:00)); // Compile-time macro const BOOT: PrimitiveDateTime = datetime!(2024-01-01 0:00); // Attach a timezone let utc = dt.assume_utc(); // → OffsetDateTime with UTC offset let odt = dt.assume_offset(time::macros::offset!(+5:30)); // → OffsetDateTime with IST let utc_dt = dt.as_utc(); // → UtcDateTime // Arithmetic let later = dt + time::Duration::days(7); let diff = later - dt; ``` ``` -------------------------------- ### Extension Traits for Constructing `time::Duration` Source: https://context7.com/time-rs/time/llms.txt Utilizes `NumericalDuration` and `NumericalStdDuration` extension traits to construct `time::Duration` and `std::time::Duration` from integer and float literals. Supports various units from nanoseconds to weeks. Note that floating-point seconds are truncated. ```rust use time::ext::{NumericalDuration, NumericalStdDuration}; use time::Duration; // NumericalDuration: construct time::Duration from integer/float literals assert_eq!(5i64.nanoseconds(), Duration::nanoseconds(5)); assert_eq!(5i64.microseconds(), Duration::microseconds(5)); assert_eq!(5i64.milliseconds(), Duration::milliseconds(5)); assert_eq!(5i64.seconds(), Duration::seconds(5)); assert_eq!(5i64.minutes(), Duration::minutes(5)); assert_eq!(5i64.hours(), Duration::hours(5)); assert_eq!(5i64.days(), Duration::days(5)); assert_eq!(5i64.weeks(), Duration::weeks(5)); // Negative values work too assert_eq!((-5i64).seconds(), Duration::seconds(-5)); // Floating-point: fractional seconds are truncated assert_eq!(0.5f64.seconds(), Duration::nanoseconds(500_000_000)); // NumericalStdDuration: construct std::time::Duration from integer literals use std::time::Duration as StdDuration; assert_eq!(5u64.std_seconds(), StdDuration::from_secs(5)); assert_eq!(5u64.std_milliseconds(), StdDuration::from_millis(5)); assert_eq!(5u64.std_microseconds(), StdDuration::from_micros(5)); assert_eq!(5u64.std_nanoseconds(), StdDuration::from_nanos(5)); ``` -------------------------------- ### Duration - Signed Span of Time Source: https://context7.com/time-rs/time/llms.txt The `Duration` struct represents a signed time span with nanosecond precision, capable of being negative. It can be constructed using static methods or the `NumericalDuration` trait for ergonomic syntax. It provides accessors for whole components and sub-second components, along with constants and arithmetic operators. ```APIDOC ## `Duration` — Signed Span of Time `Duration` represents a signed time span with nanosecond precision. Unlike `std::time::Duration`, it can be negative. ```rust use time::{Duration, ext::NumericalDuration}; // Construction via static methods let d1 = Duration::seconds(90); let d2 = Duration::minutes(1) + Duration::seconds(30); assert_eq!(d1, d2); // Ergonomic construction via NumericalDuration trait let d3 = 2.hours() + 30.minutes() + 15.seconds(); assert_eq!(d3.whole_hours(), 2); assert_eq!(d3.whole_minutes(), 150); assert_eq!(d3.whole_seconds(), 9015); // Negative durations let neg = (-5).seconds(); assert!(neg.is_negative()); assert_eq!(neg.abs(), 5.seconds()); assert_eq!(neg.whole_seconds(), -5); // All construction methods let _ = Duration::weeks(2); let _ = Duration::days(14); let _ = Duration::hours(336); let _ = Duration::milliseconds(1_209_600_000); let _ = Duration::microseconds(1_209_600_000_000); let _ = Duration::nanoseconds(1_209_600_000_000_000); let _ = Duration::nanoseconds_i128(1_209_600_000_000_000_i128); let _ = Duration::seconds_f64(1.5); // panics on NaN or overflow let _ = Duration::seconds_f32(1.5); let _ = Duration::saturating_seconds_f64(f64::INFINITY); // → Duration::MAX let _ = Duration::checked_seconds_f64(f64::NAN); // → None // Component accessors let d = 1.days() + 2.hours() + 3.minutes() + 4.seconds() + 5.milliseconds(); assert_eq!(d.whole_days(), 1); assert_eq!(d.whole_hours(), 26); assert_eq!(d.whole_minutes(), 1563); assert_eq!(d.whole_seconds(), 93784); assert_eq!(d.whole_milliseconds(), 93_784_005); assert_eq!(d.whole_microseconds(), 93_784_005_000); assert_eq!(d.whole_nanoseconds(), 93_784_005_000_000); assert_eq!(d.subsec_milliseconds(), 5); assert_eq!(d.subsec_microseconds(), 5_000); assert_eq!(d.subsec_nanoseconds(), 5_000_000); // Constants assert_eq!(Duration::ZERO.whole_seconds(), 0); assert!(Duration::ZERO.is_zero()); let _ = Duration::NANOSECOND; let _ = Duration::MICROSECOND; let _ = Duration::MILLISECOND; let _ = Duration::SECOND; let _ = Duration::MINUTE; let _ = Duration::HOUR; let _ = Duration::DAY; let _ = Duration::WEEK; let _ = Duration::MIN; let _ = Duration::MAX; // Arithmetic operators: +, -, *, /, Neg let doubled = 5.seconds() * 2; assert_eq!(doubled, 10.seconds()); let halved = 10.seconds() / 2; assert_eq!(halved, 5.seconds()); ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.