pub struct Utc2k { /* private fields */ }Expand description
§UTC2K.
This is a lightweight date/time object for UTC date ranges within the
current century (i.e. 2000-01-01 00:00:00..=2099-12-31 23:59:59).
Values outside this range are saturated to fit, unless using methods like
Utc2k::checked_from_ascii or Utc2k::checked_from_unixtime.
To manually construct from individual parts, you can just call Utc2k::new.
A Utc2k object can be turned back into its constituent parts via
Utc2k::parts, or the individual methods like Utc2k::year, Utc2k::month,
etc.
It can be converted into a unix timestamp with Utc2k::unixtime.
§Examples
use utc2k::Utc2k;
let date = Utc2k::default(); // 2000-01-01 00:00:00
let date = Utc2k::now(); // The current time.
let date = Utc2k::from(4_102_444_799_u32); // 2099-12-31 23:59:59
// String parsing is fallible, but flexible. So long as the numbers we
// need are in the right place, it will be fine.
assert!(Utc2k::try_from("2099-12-31 23:59:59").is_ok()); // Fine.
assert!(Utc2k::try_from("2099-12-31").is_ok()); // Also fine, but midnight.
assert!(Utc2k::try_from("2099-12-31T23:59:59.1234").is_ok()); // Also fine, but floored.
assert!(Utc2k::try_from("January 1, 2010").is_err()); // Nope!Implementations§
Source§impl Utc2k
§Min/Max.
impl Utc2k
§Min/Max.
Sourcepub const MIN: Self
pub const MIN: Self
§Minimum Date/Time.
assert_eq!(
utc2k::Utc2k::MIN.to_string(),
"2000-01-01 00:00:00",
);Sourcepub const MAX: Self
pub const MAX: Self
§Maximum Date/Time.
assert_eq!(
utc2k::Utc2k::MAX.to_string(),
"2099-12-31 23:59:59",
);Sourcepub const MIN_UNIXTIME: u32 = 946_684_800u32
pub const MIN_UNIXTIME: u32 = 946_684_800u32
§Minimum Unix Timestamp.
use utc2k::Utc2k;
assert_eq!(Utc2k::MIN_UNIXTIME, 946_684_800);
assert_eq!(
Utc2k::MIN.unixtime(),
Utc2k::MIN_UNIXTIME,
);Sourcepub const MAX_UNIXTIME: u32 = 4_102_444_799u32
pub const MAX_UNIXTIME: u32 = 4_102_444_799u32
§Maximum Unix Timestamp.
use utc2k::Utc2k;
assert_eq!(Utc2k::MAX_UNIXTIME, 4_102_444_799);
assert_eq!(
Utc2k::MAX.unixtime(),
Utc2k::MAX_UNIXTIME,
);Source§impl Utc2k
§Instantiation.
impl Utc2k
§Instantiation.
Sourcepub const fn new(y: u16, m: u8, d: u8, hh: u8, mm: u8, ss: u8) -> Self
pub const fn new(y: u16, m: u8, d: u8, hh: u8, mm: u8, ss: u8) -> Self
§New (From Parts).
This will create a new instance from individual year, month, etc., parts.
Overflowing units will be carried over where appropriate, so for example, 13 months becomes 1 year and 1 month.
Dates prior to 2000 or after 2099 will be saturated to fit.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 5, 5, 16, 30, 1);
assert_eq!(date.to_string(), "2010-05-05 16:30:01");Sourcepub const fn from_ascii(src: &[u8]) -> Option<Self>
pub const fn from_ascii(src: &[u8]) -> Option<Self>
§From ASCII Date/Time Slice.
Try to parse a date/time value from an ASCII slice, returning a
Utc2k instance if successful, None if not.
Note that this method will automatically clamp dates outside the
supported 2000..=2099 range to Utc2k::MIN/Utc2k::MAX.
If you’d rather out-of-range values “fail” instead, use
Utc2k::checked_from_ascii.
§Supported Formats.
This method can be used to parse dates and datetimes — but not times by themselves — from formats that A) order the components biggest to smallest; and B) use four digits to express the year, and two digits for everything else.
Digits can either be squished together like YYYYMMDD or
YYYYMMDDhhmmss, or separated by single non-digit bytes, like
YYYY/MM/DD or YYYY-MM-DD hh:mm:ss.
(Times can technically end …ss.ffff, but Utc2k doesn’t support
fractional seconds; they’re ignored if present.)
Complete datetimes can optionally end with “Z”, “ UT“, “ UTC“, or
“ GMT“ — all of which are ignored — or a fixed UTC offset of the
±hhmm/±hh:mm varieties which, if present, will be parsed and
factored into the result. (Fixed offsets can also be written like
“GMT±hhmm” or “UTC±hhmm”.)
Parsing will fail for sources containing any other random trailing data, including things like “CST”-style time zone abbreviations.
§Examples
use utc2k::Utc2k;
// Separators are flexible.
let dates: [&[u8]; 5] = [
b"20250615", // Squished.
b"2025 06 15", // Spaced.
b"2025/06/15", // Slashed.
b"2025-06-15", // Dashed.
b"2025#06#15", // Hashed? Haha.
];
for raw in dates {
assert_eq!(
Utc2k::from_ascii(raw).unwrap().parts(),
(2025, 6, 15, 0, 0, 0),
// ^ ^ ^ Time defaults to midnight.
);
}
// Same for datetimes.
let datetimes: [&[u8]; 14] = [
b"20250615123001",
b"2025-06-15 12:30:01",
b"2025-06-15T12:30:01Z",
b"2025/06/15:12:30:01 GMT",
b"2025/06/15:12:30:01 GMT+0000", // Redundant.
b"2025/06/15:12:30:01gmt", // Space/case-insensitive.
b"2025/06/15:12:30:01 UT",
b"2025/06/15:12:30:01 UTC",
b"2025/06/15:12:30:01 UTC+0000", // Redundant.
b"2025/06/15 12:30:01.000 +0000",
b"2025/06/15 12:30:01+0000",
b"2025/06/15T16:30:01+04:00", // Colon in offset
b"2025/06/15T16:30:01UTC+04:00",
b"2025/06/15T05:30:01-07:00",
];
for raw in datetimes {
assert_eq!(
Utc2k::from_ascii(raw).unwrap().parts(),
(2025, 6, 15, 12, 30, 1),
);
}
// Not everything will work out…
let bad: [&[u8]; 9] = [
b"2025-06-15 123001", // Formats cannot mix-and-match
b"2025-06-15123001", // squished/separated.
b"20250615 12:30:01",
b"2025061512:30:01",
b"2025-01-01Z", // Date-only strings cannot contain
b"2025-01-01 +0000", // tz/offset details.
b"2025-01-01 UTC",
b"2025-01-01 00:00:00 PDT", // Only UTC-related identifiers are
b"2025-01-01 00:00:00 EST", // supported.
];
for raw in bad {
assert!(Utc2k::from_ascii(raw).is_none());
}
// UTC offsets will get factored in accordingly.
assert_eq!(
Utc2k::from_ascii(b"2025-06-15 12:30:01 +0330")
.unwrap()
.parts(),
(2025, 6, 15, 9, 0, 1),
);
assert_eq!(
Utc2k::from_ascii(b"2025-06-15T12:30:01.54321-0700")
.unwrap() // ^----^ Ignored.
.parts(),
(2025, 6, 15, 19, 30, 1),
);
// The input doesn't have to follow calendar/clock grouping
// conventions, but the output always will.
assert_eq!(
Utc2k::from_ascii(b"2000-13-10 24:60:61")
.unwrap() // ^ ^ ^ ^ Logical "overflow".
.parts(),
(2001, 1, 11, 1, 1, 1),
// ^ ^ ^ ^ ^ ^ Realigned.
);
assert_eq!(
Utc2k::from_ascii(b"2050-02-00")
.unwrap() // ^ Logical "underflow".
.parts(),
(2050, 1, 31, 0, 0, 0),
// ^ ^ Realigned.
);
// Returned values are clamped to the `2000..=2099` range.
assert_eq!(
Utc2k::from_ascii(b"1994 04 08"),
Some(Utc2k::MIN), // 2000-01-01 00:00:00
);
assert_eq!(
Utc2k::from_ascii(b"3000/01/01"),
Some(Utc2k::MAX), // 2099-12-31 23:59:59
);Sourcepub const fn from_rfc2822(src: &[u8]) -> Option<Self>
pub const fn from_rfc2822(src: &[u8]) -> Option<Self>
§From RFC2822 Date/Time Slice.
Try to parse a date/time value from a RFC2822-formatted
byte slice, returning a Utc2k instance if successful, None if
not.
§Examples
use utc2k::Utc2k;
// This spec tolerates a lot of variation…
let dates: [&[u8]; 7] = [
b"Tue, 1 Jul 2003 10:52:37 +0000", // Single-digit day.
b"Tue, 1 Jul 2003 10:52:37 +0000", // Digit/space substitution.
b"Tue, 01 Jul 2003 10:52:37 +0000", // Leading zero.
b"1 Jul 2003 10:52:37", // No weekday or offset.
b"01 Jul 2003 10:52:37", // Same, but w/ leading zero.
b"Tue, 01 Jul 2003 03:52:37 -0700", // Negative UTC offset.
b"Tue, 1 Jul 2003 15:22:37 +0430", // Positive UTC offset.
];
for raw in dates {
assert_eq!(
Utc2k::from_rfc2822(raw).unwrap().parts(),
(2003, 7, 1, 10, 52, 37),
);
}
// The same variation exists for date-only representations too.
let dates: [&[u8]; 5] = [
b"Tue, 1 Jul 2003", // Single-digit day.
b"Tue, 1 Jul 2003", // Digit/space substitution.
b"Tue, 01 Jul 2003", // Leading zero.
b"1 Jul 2003", // No weekday or offset.
b"01 Jul 2003", // Same, but w/ leading zero.
];
for raw in dates {
assert_eq!(
Utc2k::from_rfc2822(raw).unwrap().parts(),
(2003, 7, 1, 0, 0, 0), // Default time is midnight.
);
}Sourcepub const fn from_unixtime(src: u32) -> Self
pub const fn from_unixtime(src: u32) -> Self
§From Timestamp.
Initialize a new Utc2k from a unix timestamp, saturating to
Utc2k::MIN_UNIXTIME or Utc2k::MAX_UNIXTIME if out of range.
For a non-saturating alternative, see Utc2k::checked_from_unixtime.
§Examples
use utc2k::Utc2k;
assert_eq!(
Utc2k::from_unixtime(1_748_672_925).to_string(),
"2025-05-31 06:28:45",
);
// Same as the above, but using the `From<u32>` impl.
assert_eq!(
Utc2k::from(1_748_672_925_u32).to_string(),
"2025-05-31 06:28:45",
);
// Out of range values will saturate to the boundaries of the
// century.
assert_eq!(
Utc2k::from_unixtime(0).to_string(),
"2000-01-01 00:00:00",
);
assert_eq!(
Utc2k::from_unixtime(u32::MAX).to_string(),
"2099-12-31 23:59:59",
);Source§impl Utc2k
§Get Parts.
impl Utc2k
§Get Parts.
Sourcepub const fn parts(self) -> (u16, u8, u8, u8, u8, u8)
pub const fn parts(self) -> (u16, u8, u8, u8, u8, u8)
§Parts.
Return the individual numerical components of the datetime, from years down to seconds.
Alternatively, if you only want the date bits, use Utc2k::ymd, or
if you only want the time bits, use Utc2k::hms.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 5, 4, 16, 30, 1);
assert_eq!(date.parts(), (2010, 5, 4, 16, 30, 1));Sourcepub const fn ymd(self) -> (u16, u8, u8)
pub const fn ymd(self) -> (u16, u8, u8)
§Date Parts.
Return the year, month, and day.
If you want the time too, call Utc2k::parts instead.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 5, 5, 16, 30, 1);
assert_eq!(date.ymd(), (2010, 5, 5));Sourcepub const fn hms(self) -> (u8, u8, u8)
pub const fn hms(self) -> (u8, u8, u8)
§Time Parts.
Return the hours, minutes, and seconds.
If you want the date too, call Utc2k::parts instead.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 5, 5, 16, 30, 1);
assert_eq!(date.hms(), (16, 30, 1));Sourcepub const fn hour_12(self) -> u8
pub const fn hour_12(self) -> u8
§12-Hour Hour.
Return the hour in unholy 12-hour format.
Utc2k::hour_period can be used to disambiguate the result.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 5, 15, 4, 30, 1);
assert_eq!(date.hour_12(), 4); // AM.
let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.hour_12(), 4); // PM.Sourcepub const fn hour_period(self) -> Period
pub const fn hour_period(self) -> Period
Source§impl Utc2k
§Other Getters.
impl Utc2k
§Other Getters.
Sourcepub const fn month_size(self) -> u8
pub const fn month_size(self) -> u8
§Month Size (Days).
This method returns the “size” of the datetime’s month, or its last day, whichever way you prefer to think of it.
The value will always be between 28..=31, with leap Februaries
returning 29.
§Examples
use utc2k::Utc2k;
let date = Utc2k::try_from("2021-07-08 13:22:01").unwrap();
assert_eq!(date.month_size(), 31);
let date = Utc2k::try_from("2020-02-01").unwrap();
assert_eq!(date.month_size(), 29); // Leap!Sourcepub const fn ordinal(self) -> u16
pub const fn ordinal(self) -> u16
§Ordinal.
Return the day-of-year value. This will be between 1..=365 (or 1..=366
for leap years).
§Examples
use utc2k::Utc2k;
let date = Utc2k::try_from("2020-05-10 00:00:00").unwrap();
assert_eq!(date.ordinal(), 131);
let date = Utc2k::try_from("2021-01-15 00:00:00").unwrap();
assert_eq!(date.ordinal(), 15);Sourcepub const fn seconds_from_midnight(self) -> u32
pub const fn seconds_from_midnight(self) -> u32
§Seconds From Midnight.
Return the number of seconds since (the current day’s) midnight. In other words, this adds up all of the time bits.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2010, 11, 30, 0, 0, 0);
assert_eq!(date.seconds_from_midnight(), 0);
let date = Utc2k::new(2010, 11, 30, 0, 0, 30);
assert_eq!(date.seconds_from_midnight(), 30);
let date = Utc2k::new(2010, 11, 30, 0, 1, 30);
assert_eq!(date.seconds_from_midnight(), 90);
let date = Utc2k::new(2010, 11, 30, 12, 30, 10);
assert_eq!(date.seconds_from_midnight(), 45_010);Source§impl Utc2k
§Conversion.
impl Utc2k
§Conversion.
Sourcepub const fn to_midnight(self) -> Self
pub const fn to_midnight(self) -> Self
Sourcepub fn to_rfc2822(&self) -> String
pub fn to_rfc2822(&self) -> String
§To RFC2822.
Return a string formatted according to RFC2822.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2003, 7, 1, 10, 52, 37);
assert_eq!(
date.to_rfc2822(),
"Tue, 01 Jul 2003 10:52:37 +0000",
// ^ This implementation zero-pads short day
// numbers rather than truncating them…
);
let date = Utc2k::new(2036, 12, 15, 16, 30, 55);
assert_eq!(
date.to_rfc2822(),
"Mon, 15 Dec 2036 16:30:55 +0000",
// ^-----------------------------^ …to keep the output
// length consistent.
);Sourcepub fn to_rfc3339(&self) -> String
pub fn to_rfc3339(&self) -> String
§To RFC3339.
Return a string formatted according to RFC3339.
§Examples
use utc2k::Utc2k;
let date = Utc2k::new(2021, 12, 13, 11, 56, 1);
assert_eq!(date.to_rfc3339(), "2021-12-13T11:56:01Z");
// The reverse operation — parsing an RFC3339 datetime string into
// a Utc2k — can be done using `Utc2k::from_ascii`.
assert_eq!(
Utc2k::from_ascii(date.to_rfc3339().as_bytes()).unwrap(),
date,
);Sourcepub const fn with_time(self, hh: u8, mm: u8, ss: u8) -> Self
pub const fn with_time(self, hh: u8, mm: u8, ss: u8) -> Self
§With a New Time.
Return a new Utc2k instance with the original date — unless there
is carry-over needed — and a new time.
§Examples
use utc2k::Utc2k;
let date = Utc2k::default();
assert_eq!(date.to_string(), "2000-01-01 00:00:00");
// Change the time bits.
assert_eq!(date.with_time(13, 14, 15).to_string(), "2000-01-01 13:14:15");Source§impl Utc2k
§Fancy Formatting.
impl Utc2k
§Fancy Formatting.
Sourcepub fn formatted_custom(self, fmt: &str) -> Result<String, Utc2kFormatError>
pub fn formatted_custom(self, fmt: &str) -> Result<String, Utc2kFormatError>
§Arbitrarily-Formatted Date/Time.
This method can be used to format a Utc2k date/time value just
about any which way.
§Syntax
The formatting syntax is similar to the one used by the time crate,
but a bit more compact and consistent.
Date/time “components” are specified as []-enclosed tags with
optional space-separated “modifiers”. Everything else is treated as
a literal and passed through as-is.
To include a literal "[", escape it with another, i.e. "[[".
Note: format strings must be valid ASCII.
§Components
| Component | Modifier | Description | Example |
|---|---|---|---|
year | Four-digit year. | "2000" | |
@2 | Two-digit year. | "00" | |
@2 @space | Two-digit year, space-padded. | " 0" | |
@2 @trim | Two-digit year, unpadded. | "0" | |
month | Two-digit month. | "01" | |
@space | Two-digit month, space-padded. | " 1" | |
@trim | Two-digit month, unpadded. | "1" | |
@name | Month name. | "January" | |
@abbr | Month abbreviation. | "Jan" | |
day | Two-digit day. | "01" | |
@space | Two-digit day, space-padded. | " 1" | |
@trim | Two-digit day, unpadded. | "1" | |
@name | Weekday. | "Saturday" | |
@abbr | Weekday abbreviation. | "Sat" | |
hour | Two-digit hour (24). | "00" | |
@space | Two-digit hour (24), space-padded. | " 0" | |
@trim | Two-digit hour (24), unpadded. | "0" | |
@12 | Two-digit hour (12). | "12" | |
@12 @space | Two-digit hour (12), space-padded. | "12" | |
@12 @trim | Two-digit hour (12), unpadded. | "12" | |
minute | Two-digit minute. | "00" | |
@space | Two-digit minute, space-padded. | " 0" | |
@trim | Two-digit minute, unpadded. | "0" | |
second | Two-digit second. | "00" | |
@space | Two-digit second, space-padded. | " 0" | |
@trim | Two-digit second, unpadded. | "0" | |
ordinal | Ordinal. | "001" | |
@space | Ordinal, space-padded. | " 1" | |
@trim | Ordinal, unpadded. | "1" | |
period | Lowercase. | "am" | |
@ap | AP Style (punctuated). | "a.m." | |
@upper | UPPERCASE. | "AM" | |
unixtime | Unix timestamp. | "946684800" |
§Examples
use utc2k::Utc2k;
// Same as `Utc2k::to_rfc2822`, but slower and fallible. Haha.
assert_eq!(
Utc2k::MAX.formatted_custom(
"[day @abbr], [day] [month @abbr] [year] [hour]:[minute]:[second] +0000"
).unwrap(),
Utc2k::MAX.to_rfc2822(),
);
// Literal '[' require escape.
assert_eq!(
Utc2k::MAX.formatted_custom("[[[year]]").unwrap(),
"[2099]",
);
// Whitespace between the '[]' doesn't matter, but outside it's literal.
assert_eq!(
Utc2k::MAX.formatted_custom(" +[ period @upper ]+ ").unwrap(),
" +PM+ ",
);
// Arbitrary to the extreme…
assert_eq!(
Utc2k::MAX.formatted_custom("Hello World").unwrap(),
"Hello World",
);
// Format strings must be ASCII, however.
assert!(Utc2k::MAX.formatted_custom("Björk like it's [year]!").is_err());§Errors
This method will return an error if the format string contains non-ASCII characters or is otherwise malformed.
Source§impl Utc2k
§Checked Operations.
impl Utc2k
§Checked Operations.
Sourcepub const fn checked_add(self, secs: u32) -> Option<Self>
pub const fn checked_add(self, secs: u32) -> Option<Self>
§Checked Add.
Return a new Utc2k instance set n seconds into the future from
this one, returning none (rather than saturating) on overflow.
If you’d rather saturate addition, you can just use std::ops::Add.
§Examples
use utc2k::Utc2k;
let date = Utc2k::MAX;
assert!(date.checked_add(1).is_none());
let date = Utc2k::new(2010, 1, 1, 0, 0, 0);
let added = date.checked_add(86_413).unwrap();
assert_eq!(added.to_string(), "2010-01-02 00:00:13");Sourcepub const fn checked_from_ascii(src: &[u8]) -> Result<Self, Utc2kError>
pub const fn checked_from_ascii(src: &[u8]) -> Result<Self, Utc2kError>
§From ASCII Date/Time Slice (Checked).
Same as Utc2k::from_ascii, but will return an error if the
resulting date is too old or new to be represented faithfully
(rather than clamping it to Utc2k::MIN/Utc2k::MAX).
§Examples
use utc2k::{Utc2k, Utc2kError};
assert_eq!(
Utc2k::checked_from_ascii(b"1990-01-15 10:20:00"),
Err(Utc2kError::Underflow), // Too old.
);
assert_eq!(
Utc2k::checked_from_ascii(b"3000-12-01 13:00:00"),
Err(Utc2kError::Overflow), // Too new.
);
assert_eq!(
Utc2k::checked_from_ascii(b"2025-06-17 00:00:00")
.map(Utc2k::parts),
Ok((2025, 6, 17, 0, 0, 0)), // Just right!
);§Errors
This method will return an error if the slice cannot be parsed, or the parsed value is too big or small to fit within our century.
Sourcepub const fn checked_from_unixtime(src: u32) -> Result<Self, Utc2kError>
pub const fn checked_from_unixtime(src: u32) -> Result<Self, Utc2kError>
§From Unixtime (Checked).
This can be used instead of the usual Utc2k::from_unixtime or
From<u32> if you’d like to trigger an error when the timestamp is out
of range (rather than just saturating it).
§Errors
An error will be returned if the timestamp is less than Utc2k::MIN_UNIXTIME
or greater than Utc2k::MAX_UNIXTIME.
§Examples
use utc2k::{Utc2k, Utc2kError};
assert_eq!(
Utc2k::checked_from_unixtime(u32::MIN),
Err(Utc2kError::Underflow), // Too old.
);
assert_eq!(
Utc2k::checked_from_unixtime(u32::MAX),
Err(Utc2kError::Overflow), // Too new.
);
assert_eq!(
Utc2k::checked_from_unixtime(1_750_187_543)
.map(Utc2k::parts),
Ok((2025, 6, 17, 19, 12, 23)), // Just right!
);Sourcepub const fn checked_sub(self, secs: u32) -> Option<Self>
pub const fn checked_sub(self, secs: u32) -> Option<Self>
§Checked Sub.
Return a new Utc2k instance set n seconds before this one,
returning none (rather than saturating) on overflow.
If you’d rather saturate subtraction, you can just use std::ops::Sub.
§Examples
use utc2k::Utc2k;
let date = Utc2k::MIN;
assert!(date.checked_sub(1).is_none());
let date = Utc2k::new(2010, 1, 1, 0, 0, 0);
let subbed = date.checked_sub(86_413).unwrap();
assert_eq!(subbed.to_string(), "2009-12-30 23:59:47");Source§impl Utc2k
§Comparison.
impl Utc2k
§Comparison.
Sourcepub const fn abs_diff(self, other: Self) -> u32
pub const fn abs_diff(self, other: Self) -> u32
§Absolute Difference.
This returns the (absolute) number of seconds between two datetimes.
§Examples.
use utc2k::Utc2k;
let date1 = Utc2k::new(2022, 10, 15, 11, 30, 0);
let date2 = Utc2k::new(2022, 10, 15, 11, 31, 0);
// ABS means the ordering does not matter.
assert_eq!(date1.abs_diff(date2), 60);
assert_eq!(date2.abs_diff(date1), 60);
// If the dates are equal, the difference is zero.
assert_eq!(date1.abs_diff(date1), 0);
// Because we're only dealing with a single century, there is an
// upper limit to the possible return values…
assert_eq!(Utc2k::MIN.abs_diff(Utc2k::MAX), 3_155_759_999);Sourcepub const fn cmp(a: Self, b: Self) -> Ordering
pub const fn cmp(a: Self, b: Self) -> Ordering
§Compare Two Date/Times.
Same as Ord/PartialOrd, but constant.
§Examples
use std::cmp::Ordering;
use utc2k::Utc2k;
let date1 = Utc2k::new(2020, 1, 2, 12, 0, 0);
let date2 = Utc2k::new(2021, 12, 31, 15, 30, 0);
assert_eq!(Utc2k::cmp(date1, date1), Ordering::Equal);
assert_eq!(Utc2k::cmp(date1, date1), date1.cmp(&date1));
assert_eq!(Utc2k::cmp(date1, date2), Ordering::Less);
assert_eq!(Utc2k::cmp(date1, date2), date1.cmp(&date2));
assert_eq!(Utc2k::cmp(date2, date1), Ordering::Greater);
assert_eq!(Utc2k::cmp(date2, date1), date2.cmp(&date1));Sourcepub const fn cmp_date(a: Self, b: Self) -> Ordering
pub const fn cmp_date(a: Self, b: Self) -> Ordering
§Compare (Only) Dates.
Compare self to another Utc2k instance, ignoring the time
components of each.
§Examples
use utc2k::Utc2k;
use std::cmp::Ordering;
// The times are different, but the dates match.
let date1 = Utc2k::new(2020, 3, 15, 0, 0, 0);
let date2 = Utc2k::new(2020, 3, 15, 16, 30, 20);
assert_eq!(Utc2k::cmp_date(date1, date2), Ordering::Equal);
// If the dates don't match, it's what you'd expect.
let date3 = Utc2k::new(2022, 10, 31, 0, 0, 0);
assert_eq!(Utc2k::cmp_date(date1, date3), Ordering::Less);Sourcepub const fn cmp_time(a: Self, b: Self) -> Ordering
pub const fn cmp_time(a: Self, b: Self) -> Ordering
§Compare (Only) Times.
Compare self to another Utc2k instance, ignoring the date
components of each.
§Examples
use utc2k::Utc2k;
use std::cmp::Ordering;
// The dates match, but the times are different.
let date1 = Utc2k::new(2020, 3, 15, 0, 0, 0);
let date2 = Utc2k::new(2020, 3, 15, 16, 30, 20);
assert_eq!(Utc2k::cmp_time(date1, date2), Ordering::Less);
// If the times match, it's what you'd expect.
let date3 = Utc2k::new(2022, 10, 31, 0, 0, 0);
assert_eq!(Utc2k::cmp_time(date1, date3), Ordering::Equal);Trait Implementations§
Source§impl Add<u32> for Utc2k
impl Add<u32> for Utc2k
Source§impl AddAssign<u32> for Utc2k
impl AddAssign<u32> for Utc2k
Source§fn add_assign(&mut self, other: u32)
fn add_assign(&mut self, other: u32)
§Add Seconds.
use utc2k::{DAY_IN_SECONDS, Utc2k};
let mut utc = Utc2k::from(1_750_620_170);
assert_eq!(
utc.parts(),
(2025, 6, 22, 19, 22, 50),
);
utc += DAY_IN_SECONDS * 2;
assert_eq!(
utc.parts(),
(2025, 6, 24, 19, 22, 50),
);Source§impl<'de> Deserialize<'de> for Utc2k
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Utc2k
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
§Deserialize.
Use the optional serde crate feature to enable serialization support.
Source§impl From<u32> for Utc2k
impl From<u32> for Utc2k
Source§fn from(src: u32) -> Self
fn from(src: u32) -> Self
§From Timestamp.
Note, this will saturate to Utc2k::MIN_UNIXTIME and
Utc2k::MAX_UNIXTIME if the timestamp is out of range.
§Examples
use utc2k::Utc2k;
assert_eq!(Utc2k::from(0).to_string(), "2000-01-01 00:00:00");
assert_eq!(Utc2k::from(u32::MAX).to_string(), "2099-12-31 23:59:59");Source§impl Ord for Utc2k
impl Ord for Utc2k
Source§impl PartialEq<Utc2k> for Local2k
Available on crate feature local only.
impl PartialEq<Utc2k> for Local2k
local only.Source§fn eq(&self, other: &Utc2k) -> bool
fn eq(&self, other: &Utc2k) -> bool
§Cross-Offset Equality.
Local and UTC dates are compared as unix timestamps, so should always match up.
§Examples
use utc2k::{Local2k, Utc2k};
let utc = Utc2k::new(2025, 1, 1, 0, 0, 0);
let local = Local2k::from(utc);
assert_eq!(utc, local);
// String representations, however, will only be equal if there's
// no offset.
assert_eq!(
utc.to_string() == local.to_string(),
local.offset().is_none(),
);Source§impl PartialOrd for Utc2k
impl PartialOrd for Utc2k
Source§impl Sub<u32> for Utc2k
impl Sub<u32> for Utc2k
Source§fn sub(self, other: u32) -> Self
fn sub(self, other: u32) -> Self
§Subtraction.
This method returns a new Utc2k object reduced by a given number of
seconds.
§Examples
use utc2k::Utc2k;
assert_eq!(
Utc2k::new(2020, 1, 5, 0, 0, 0) - 86_400_u32,
Utc2k::new(2020, 1, 4, 0, 0, 0),
);
assert_eq!(
Utc2k::new(2020, 1, 5, 0, 0, 0) - 86_399_u32,
Utc2k::new(2020, 1, 4, 0, 0, 1),
);
assert_eq!(
Utc2k::new(2020, 1, 5, 3, 10, 20) - 14_400_u32,
Utc2k::new(2020, 1, 4, 23, 10, 20),
);
assert_eq!(
Utc2k::new(2020, 1, 1, 3, 10, 20) - 14_400_u32,
Utc2k::new(2019, 12, 31, 23, 10, 20),
);Source§impl SubAssign<u32> for Utc2k
impl SubAssign<u32> for Utc2k
Source§fn sub_assign(&mut self, other: u32)
fn sub_assign(&mut self, other: u32)
-= operation. Read moreSource§impl TryFrom<&OsStr> for Utc2k
impl TryFrom<&OsStr> for Utc2k
Source§fn try_from(src: &OsStr) -> Result<Self, Self::Error>
fn try_from(src: &OsStr) -> Result<Self, Self::Error>
§From OsStr.
use std::ffi::OsStr;
use utc2k::Utc2k;
assert_eq!(
Utc2k::try_from(OsStr::new("2013-12-15 21:30:02")).unwrap().to_string(),
"2013-12-15 21:30:02"
);
assert_eq!(
Utc2k::try_from(OsStr::new("2013-12-15")).unwrap().to_string(),
"2013-12-15 00:00:00"
);