#日期-时间 #UTC #日期 #纪元 #时间测量 #系统时间

无 std utc-dt

为 Rust 提供简单、快速且小巧的 UTC 日期、时间戳和日期时间库

6 个版本

0.2.1 2023年8月10日
0.2.0 2023年8月4日
0.1.4 2023年7月10日
0.1.3 2023年6月21日

#120日期和时间 类别中

MIT/Apache 许可协议

79KB
1K SLoC

UTC Datetime

crates.io license build status codecov

为 Rust 提供简单、快速且小巧的 UTC 日期、时间戳和日期时间库。

UTC Datetime 致力于提供易用和用户友好的体验,专注于核心功能。它优先考虑空间优化和效率。

[dependencies]
utc-dt = "0.2"

对于扩展/专业功能以及本地时区支持,请查看 chronotime

unsigned 仅支持!

UTC Datetime 将只表示自 Unix 纪元 (1970-01-01T00:00:00Z) 以来的时间和日期。库利用这个假设来简化 API 和内部逻辑。

文档

请参阅 docs.rs 以获取 API 参考。

功能

  • Duration 或直接从无符号 UTC 亚秒级测量或系统时间创建 UTC 时间戳和日期时间。
  • 确定公历日期、一天中的时间、星期几或自 Unix 纪元以来的天数。
  • 获取有关日期或时间的信息,例如它是否发生在闰年或该月的天数。
  • 高效且优雅地转换时间表示。
  • 尽可能在编译时进行 `const` 评估。
  • 根据 ISO 8601 (YYYY-MM-DDThh:mm:ssZ) 格式化和解析日期、时间和日期时间。
  • 提供有用的常量,用于时间转换:utc-dt::constants
  • 纳秒级分辨率。
  • 支持标准数学运算符的时间戳(core::ops
  • #![no_std] 支持。

示例(详尽)

   use core::time::Duration;

   use utc_dt::UTCDatetime;
   use utc_dt::time::{
       UTCTimestamp,
       UTCDay,
       UTCTimeOfDay,
   };
   use utc_dt::date::UTCDate;

   // An example duration.
   // When a duration is used, it is assumed to be relative to the unix epoch.
   // Thursday, 15 June 2023 10:18:08.903
   let example_duration = Duration::from_millis(1686824288903);

   // UTC Timestamp from a duration
   let utc_timestamp = UTCTimestamp::from(example_duration); // OR
   let utc_timestamp = UTCTimestamp::from_duration(example_duration);
   // UTC timestamp from the local system time.
   // Not available for #![no_std]
   let utc_timestamp = UTCTimestamp::try_from_system_time().unwrap();
   // UTC Timestamp from a time measurement (for secs, millis, micros, nanos)
   let utc_timestamp = UTCTimestamp::from_millis(1686824288903);
   // Use UTC Timestamp to get a time measurement since the epoch (for secs, millis, micros, nanos)
   let utc_millis = utc_timestamp.as_millis();
   // Use UTC Timestamp to get time-of-day
   let utc_tod: UTCTimeOfDay = utc_timestamp.as_tod();
   // Use UTC Timestamp to get days since epoch (ie. UTC Day)
   let utc_day: UTCDay = utc_timestamp.as_day();
   // UTC Timestamp from UTC Day and time-of-day components
   let utc_timestamp = UTCTimestamp::from_day_and_tod(utc_day, utc_tod);
   // Manipulate UTC Timestamps with standard math operators
   assert_eq!(utc_timestamp + utc_timestamp, utc_timestamp * 2);
   assert_eq!(utc_timestamp - example_duration, UTCTimestamp::ZERO);
   // Easily apply offsets of various measurements to timestamps
   let utc_timestamp_plus_1s = utc_timestamp.saturating_add_millis(1000);
   let utc_timestamp_minus_1s = utc_timestamp.saturating_sub_secs(1);

   // UTC Day from an integer
   let utc_day = UTCDay::try_from_u64(19523).unwrap();
   // Integer from UTC Day
   let day_u64 = utc_day.as_u64(); // OR
   let day_u64 = utc_day.to_u64();
   // Use UTC Day to get the weekday
   let weekday = utc_day.as_weekday();
   // Manipulate UTC Days with standard math operators
   assert_eq!(utc_day - utc_day, utc_day / u64::MAX);
   assert_eq!(utc_day + 19523, utc_day * 2);

   // UTC Time of Day from a time measurement (for secs, millis, micros, nanos)
   let utc_tod = UTCTimeOfDay::try_from_millis(37088903).unwrap(); // OR
   let utc_tod = unsafe { UTCTimeOfDay::from_millis_unchecked(37088903) };
   // UTC Time of Day from hours, minutes, seconds and subseconds
   let utc_tod = UTCTimeOfDay::try_from_hhmmss(10, 18, 08, 903_000_000).unwrap(); // OR
   let utc_tod = unsafe { UTCTimeOfDay::from_hhmmss_unchecked(10, 18, 08, 903_000_000) };
   // UTC Time of Day as a time measurement (for secs, millis, micros, nanos)
   let utc_tod_us = utc_tod.as_micros();
   // UTC Time of Day as hours, minutes and seconds
   let (hrs, mins, secs) = utc_tod.as_hhmmss();
   // UTC Time of Day subsecond component (in nanoseconds)
   let subsec_ns = utc_tod.as_subsec_ns();
   // Parse a UTC Time of Day from an ISO 8601 time string `(Thh:mm:ssZ)`
   // Not available for #![no_std]
   let utc_tod = UTCTimeOfDay::try_from_iso_tod("T10:18:08.903Z").unwrap();
   // Get a time of day string formatted according to ISO 8601 `(Thh:mm:ssZ)`
   // Not available for #![no_std]
   let precision = Some(6);
   let iso_tod = utc_tod.as_iso_tod(precision);
   assert_eq!(iso_tod, "T10:18:08.903000Z");

   // UTC Date directly from components
   let utc_date = UTCDate::try_from_components(2023, 6, 15).unwrap(); // OR
   let utc_date = unsafe { UTCDate::from_components_unchecked(2023, 6, 15) };
   // UTC Date from UTC Day
   let utc_date = UTCDate::from_day(utc_day);
   // Check whether date occurs within leap year
   let is_leap_year: bool = utc_date.is_leap_year();
   // Get number of days within date's month
   let days_in_month: u8 = utc_date.days_in_month();
   // Get the date in integer forms
   let (year, month, day) = utc_date.as_components();
   // UTC Day from UTC Date
   let utc_day = utc_date.as_day();
   // Parse a UTC Date from an ISO 8601 date string `(YYYY-MM-DD)`
   // Not available for #![no_std]
   let utc_date = UTCDate::try_from_iso_date("2023-06-15").unwrap();
   // Get date string formatted according to ISO 8601 `(YYYY-MM-DD)`
   // Not available for #![no_std]
   let iso_date = utc_date.as_iso_date();
   assert_eq!(iso_date, "2023-06-15");

   // UTC Datetime from date and time-of-day components
   let utc_datetime = UTCDatetime::from_components(utc_date, utc_tod);
   // Get date and time-of-day components
   let (utc_date, time_of_day_ns) = (utc_datetime.as_date(), utc_datetime.as_tod()); // OR
   let (utc_date, time_of_day_ns) = utc_datetime.as_components();
   // Parse a UTC Datetime from an ISO 8601 datetime string `(YYYY-MM-DDThh:mm:ssZ)`
   // Not available for #![no_std]
   let utc_datetime = UTCDatetime::try_from_iso_datetime("2023-06-15T10:18:08.903Z").unwrap();
   // Get UTC datetime string formatted according to ISO 8601 `(YYYY-MM-DDThh:mm:ssZ)`
   // Not available for #![no_std]
   let precision = None;
   let iso_datetime = utc_datetime.as_iso_datetime(precision);
   assert_eq!(iso_datetime, "2023-06-15T10:18:08Z");

   {
       // `UTCTransformations` can be used to create shortcuts to the desired type!
       use utc_dt::time::UTCTransformations;

       // Example shortcuts using `UTCTransformations`
       // UTC Day / UTC Date / UTC Datetime from a duration
       let utc_day = UTCDay::from_duration(example_duration); // OR
       let utc_day = UTCDay::from(example_duration);
       let utc_date = UTCDate::from_duration(example_duration); // OR
       let utc_date = UTCDate::from(example_duration);
       let utc_datetime = UTCDatetime::from_duration(example_duration); // OR
       let utc_datetime = UTCDatetime::from(example_duration);

       // UTC Day / UTC Date / UTC Datetime from a timestamp
       let utc_day = UTCDay::from_timestamp(utc_timestamp); // OR
       let utc_day = UTCDay::from(utc_timestamp);
       let utc_date = UTCDate::from_timestamp(utc_timestamp); // OR
       let utc_date = UTCDate::from(utc_timestamp);
       let utc_datetime = UTCDatetime::from_timestamp(utc_timestamp); // OR
       let utc_datetime = UTCDatetime::from(utc_timestamp);

       // UTC Day / UTC Date / UTC Datetime from local system time
       // Not available for #![no_std]
       let utc_day = UTCDay::try_from_system_time().unwrap();
       let utc_date = UTCDate::try_from_system_time().unwrap();
       let utc_datetime = UTCDatetime::try_from_system_time().unwrap();

       // UTC Day / UTC Date / UTC Datetime from u64 epoch measurements
       let utc_day = UTCDay::from_secs(1686824288);
       let utc_date = UTCDate::from_millis(1686824288_000);
       let utc_datetime = UTCDate::from_micros(1686824288_000_000);

       // Convert from UTC Day / UTC Date / UTC Datetime back to various types
       let utc_duration: Duration = utc_day.as_duration();
       let utc_timestamp: UTCTimestamp = utc_date.as_timestamp();
       let utc_secs: u64 = utc_date.as_secs();
       let utc_millis: u128 = utc_datetime.as_millis();
       let utc_micros: u128 = utc_day.as_micros();
       let utc_nanos: u128 = utc_date.as_nanos();
   }

参考

许可证

本项目可在以下许可证下使用

依赖项

约130KB