Struct utc2k::Utc2k [−][src]
pub struct Utc2k { /* fields omitted */ }
Expand description
UTC2K.
This is a lightweight date/time object for UTC date ranges within the
current century (e.g. 2000-01-01 00:00:00
to 2099-12-31 23:59:59
).
Values outside this range are saturated to fit, unless using
Utc2k::checked_from_unixtime
.
To instantiate from a UTC unix timestamp, use From<u32>
. To try to parse
from a YYYY-MM-DD HH:MM:SS
string, use TryFrom<&str>
.
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; use std::convert::TryFrom; 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-31T23:59:59.0000").is_ok()); // Also fine. assert!(Utc2k::try_from("January 1, 2010").is_err()); // Nope!
Implementations
Maximum Value.
This is equivalent to 2099-12-31 23:59:59
.
Examples
use utc2k::Utc2k; let date = Utc2k::max(); assert_eq!(date.to_string(), "2099-12-31 23:59:59");
Minimum Value.
This is equivalent to 2000-01-01 00:00:00
.
Examples
use utc2k::Utc2k; let date = Utc2k::min(); assert_eq!(date.to_string(), "2000-01-01 00:00:00");
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");
From Unixtime (Checked).
This can be used instead of the usual 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; // Too old. assert!(Utc2k::checked_from_unixtime(0).is_err()); // Too new. assert!(Utc2k::checked_from_unixtime(u32::MAX).is_err()); // This fits. assert!(Utc2k::checked_from_unixtime(Utc2k::MIN_UNIXTIME).is_ok());
Parts.
Return the year, month, etc., parts.
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, 5, 16, 30, 1); assert_eq!(date.parts(), (2010, 5, 5, 16, 30, 1));
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));
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));
Unix Timestamp.
Return the unix timestamp for this object.
Examples
use utc2k::Utc2k; let date = Utc2k::default(); // 2000-01-01 00:00:00 assert_eq!(date.unixtime(), Utc2k::MIN_UNIXTIME);
Month Name.
Return the name of the month, nice and pretty.
Examples
use utc2k::Utc2k; use std::convert::TryFrom; let date = Utc2k::try_from("2020-06-24 20:19:30").unwrap(); assert_eq!(date.month_name(), "June");
Trait Implementations
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");
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for Utc2k
impl UnwindSafe for Utc2k
Blanket Implementations
Mutably borrows from an owned value. Read more