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

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.

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

Year.

This returns the year value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.year(), 2010);

Month.

This returns the month value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.month(), 5);

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

Day.

This returns the day value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.day(), 15);

Hour.

This returns the hour value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.hour(), 16);

Minute.

This returns the minute value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.minute(), 30);

Second.

This returns the second value.

Examples

use utc2k::Utc2k;

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.second(), 1);

Formatted.

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

Examples

use utc2k::{FmtUtc2k, Utc2k};

let date = Utc2k::new(2010, 5, 15, 16, 30, 1);
assert_eq!(date.formatted(), FmtUtc2k::from(date));

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 returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests for !=.

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. 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.

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.