UTC Datetime
Simple, fast and small UTC date, timestamp and datetime library for Rust.
UTC Datetime aims to be a user friendly date and time alternative, focused on core features. It prioritizes being space-optimal and efficient.
[]
= "0.1"
For extended/niche features and local time-zone support see chrono or time.
NOTE
Only capable of expressing times and dates SINCE the Unix Epoch (1970-01-01T00:00:00Z). This library takes advantage of this assumption to simplify the API and internal logic.
Documentation
See docs.rs for the API reference.
Features
- Create UTC timestamps and datetimes from
Durations, or directly from unsigned UTC sub-second measurements, or from the system time. - Determine the civil calendar date, time of day, weekday or the number of days since the Unix Epoch.
- Obtain information on a date or time, such as if it occurs within a leap year, or the number of days in the month.
- Format dates according to ISO 8601
(YYYY-MM-DD) - Format datetimes according to ISO 8601
(YYYY-MM-DDThh:mm:ssZ) - Provides constants useful for time transformations:
utc-dt::constants - Nanosecond resolution.
#![no_std]support.
Examples (exhaustive)
use Duration;
use UTCDatetime;
use ;
use UTCDate;
// An example duration.
// When a duration is used, it is assumed to be relative to the unix epoch.
let example_duration = from_millis;
// UTC Timestamp from a duration
let utc_timestamp = from;
// UTC timestamp from the local system time.
// Not available for #![no_std]
let utc_timestamp = try_from_system_time.unwrap;
// UTC Timestamp from a u64 measurement directly.
let utc_timestamp: UTCTimestamp = from_millis.into;
// Use UTC Timestamp to get time-of-day
let time_of_day_ns: u64 = utc_timestamp.as_time_of_day_ns;
// Use UTC Timestamp to get days since epoch (ie. UTC Day)
let utc_day: UTCDay = utc_timestamp.as_utc_day;
// Convert UTC Timestamp to a Duration to get millis since epoch.
let utc_millis = utc_timestamp.as_utc_duration.as_millis;
// UTC Day from an integer
let utc_day = from;
// Use UTC Day to get the weekday
let weekday = utc_day.as_utc_weekday;
// UTC Date directly from components
let utc_date = try_from_components.unwrap;
// UTC Date from UTC Day
let utc_date = from_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 = ; // OR
let = utc_date.as_components;
// UTC Day from UTC Date
let utc_day = utc_date.as_utc_day;
// 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!;
// UTC Datetime directly from raw components
let utc_datetime = try_from_raw_components.unwrap;
// UTC Datetime from date and time-of-day components
let utc_datetime = try_from_components.unwrap;
// Get date and time-of-day components
let = ;
let = utc_datetime.as_components;
// Get the time in hours, minutes and seconds
let = utc_datetime.as_hours_minutes_seconds;
// Get the sub-second component of the time of day, in nanoseconds
let subsec_ns = utc_datetime.as_subsec_ns;
// Get UTC datetime string formatted according to ISO 8601 `(YYYY-MM-DDThh:mm:ssZ)`
// Not available for #![no_std]
let iso_datetime = utc_datetime.as_iso_datetime;
assert_eq!;
References
- (Howard Hinnant, 2021)
chrono-Compatible Low-Level Date Algorithms - (W3C, 1997) ISO 8601 Standard for Date and Time Formats
License
This project is licensed under either of
- Apache License, Version 2.0
- MIT License at your option.