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

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);

Now.

Create a new instance representing the current UTC time.

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.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 5, 16, 30, 1);
assert_eq!(date.parts(), (2010, 5, 5, 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);

Year.

This returns the year value.

Month.

This returns the month value.

Day.

This returns the day value.

Hour.

This returns the hour value.

Minute.

This returns the minute value.

Second.

This returns the second value.

Formatted.

This returns a FmtUtc2k and is equivalent to calling FmtUtc2k::from(self).

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

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");

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.