Struct Moment

Source
pub struct Moment { /* private fields */ }
Expand description

A precise moment in time.

A Moment has no concept of a particular time zone or calendar. It represents a single point in time that can be globally agreed upon.

Internally, moments are represented in the Coordinated Universal Time (UTC) time scale as the signed number of seconds and sub-second nanoseconds since the Unix epoch at January 1, 1970. A positive number of seconds indicates a moment after the Unix epoch where a negative number of seconds indicates a moment before the Unix epoch.

A Moment can be created from and displayed as a RFC 3339 string like 2018-02-15T03:41:23Z with the FromStr and Display traits.

§Interoperability

The Moment type can be created from and converted to many representations of exact time and other time scales, such as Unix time, a Julian day, or RFC 3339.

Please feel free to open an issue requesting additional representations for conversion to and from.

§Unix Time

Unix time is widely used as a storage and transmission format for date and time. It is the count of non-leap seconds from January 1, 1970, at 00:00:00 UTC.

§Create a Moment from a Unix time, in milliseconds
use ako::Moment;

let moment = Moment::from_unix_milliseconds(1727597174768);

assert_eq!(moment.to_string(), "2024-09-29T08:06:14.768000000Z");
§Get the current time as a Unix timestamp
use ako::Moment;

// secs is the number of seconds since the Unix epoch
// nsec is the sub-second nanoseconds
let (secs, nsec) = Moment::now().to_unix();

§SystemTime (std)

Moment and std::time::SystemTime can be freely converted between each other.

§Get a value of SystemTime from RFC 3339
use std::time::SystemTime;
use ako::Moment;

let moment: Moment = "2018-02-15T03:41:23Z".parse()?;
let system = SystemTime::from(moment);
§Create a Moment from the current system time
use std::time::SystemTime;
use ako::Moment;

let now: Moment = SystemTime::now().into();

This is what Moment::now() does internally.

Implementations§

Source§

impl Moment

Source

pub const MAX: Self

The maximum supported Moment, +1587287-03-17T15:30:07.999999999Z.

§Examples
use ako::Moment;

assert_eq!(Moment::MAX.to_unix_seconds(), i64::MAX);
Source

pub const MIN: Self

The minimum (the largest negative) supported Moment, -1583348-10-16T08:29:52Z.

§Examples
use ako::Moment;

assert_eq!(Moment::MIN.to_unix_seconds(), i64::MIN);
Source§

impl Moment

Source

pub fn now() -> Self

Available on crate feature std only.

Returns the moment corresponding to “now.”

§Examples
use ako::Moment;

// Current moment as reported by the system
let now = Moment::now();
Source§

impl Moment

Source

pub const fn from_unix(seconds: i64, nanoseconds: u32) -> Self

Creates a Moment from the given number of seconds and nanoseconds since the Unix epoch of January 1, 1970, at 00:00:00 UTC.

Allows for an out-of-range nanoseconds value to be passed in. The nanoseconds will overflow into seconds to ensure that the stored sub-second nanoseconds are in the range 0 to 999,999,999.

Source

pub const fn from_unix_seconds(seconds: i64) -> Self

Creates a Moment from the given number of seconds since the Unix epoch of January 1, 1970, at 00:00:00 UTC.

§Examples
use ako::Moment;

// first appearance of the gregorian date represented by the
// Unix timestamp to be in the timestamp
let date_in_timestamp = Moment::from_unix_seconds(1_1973_10_17);
assert_eq!(date_in_timestamp.to_string(), "1973-10-17T18:36:57Z");
Source

pub const fn from_unix_milliseconds(milliseconds: i64) -> Self

Creates a Moment from the given number of milliseconds since the Unix epoch of January 1, 1970, at 00:00:00 UTC.

Source

pub const fn from_unix_microseconds(microseconds: i128) -> Self

Creates a Moment from the given number of microseconds since the Unix epoch of January 1, 1970, at 00:00:00 UTC.

Source

pub const fn from_unix_nanoseconds(nanoseconds: i128) -> Self

Creates a Moment from the given number of nanoseconds since the Unix epoch of January 1, 1970, at 00:00:00 UTC.

Source

pub fn from_julian_day(jd: f64) -> Result<Self, Error>

Available on crate feature astronomy only.

Creates a Moment corresponding to the given Julian day (JD).

The Julian day is the continuous count of days and fractions thereof from the beginning of the Julian period, where day 0 is Monday, January 1, 4713 BCE of the Julian calendar.

Source

pub fn from_julian_ephemeris_day(jde: f64) -> Result<Self, Error>

Available on crate feature astronomy only.

Creates a Moment corresponding to the given Julian day (JD) in the Dynamical timescale (also known as Ephemeris time).

Source§

impl Moment

Source

pub const fn to_unix(self) -> (i64, u32)

Gets the number of seconds and nanoseconds since the Unix epoch. Negative seconds represent moments before the Unix epoch.

Source

pub const fn to_unix_seconds(self) -> i64

Gets the number of seconds since the Unix epoch. Negative seconds represent moments before the Unix epoch.

Source

pub const fn to_unix_milliseconds(self) -> i64

Gets the number of milliseconds since the Unix epoch.

Source

pub const fn to_unix_microseconds(self) -> i64

Gets the number of microseconds since the Unix epoch.

Source

pub const fn to_unix_nanoseconds(self) -> i128

Gets the number of nanoseconds since the Unix epoch.

Source

pub fn to_julian_day(self) -> f64

Available on crate feature astronomy only.

Gets the Julian day (JD) for this moment.

Source

pub fn to_julian_year(self) -> f64

Available on crate feature astronomy only.

Gets the Julian year (a) for this moment.

In astronomy, a Julian year (a) is a unit of measurement of time defined as exactly 365.25 days of 86,400 seconds each. The Julian year does not correspond to years in any calendar.

Source§

impl Moment

Source

pub fn format_rfc3339(self) -> String

Formats this moment, according to the RFC 3339 and ISO-8601 standards.

§Examples
let epoch = Moment::from_unix_seconds(0).format_rfc3339();

assert_eq!(epoch, "1970-01-01T00:00:00Z");
Source§

impl Moment

Source

pub fn parse_rfc3339<S: AsRef<str>>(s: S) -> Result<Self, Error>

Parses a moment, according to the RFC 3339 and ISO-8601 standards.

Trait Implementations§

Source§

impl Add<Duration> for Moment

Source§

type Output = Moment

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Duration) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Moment> for Duration

Source§

type Output = Moment

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Moment) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Moment

Source§

fn clone(&self) -> Moment

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Moment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Moment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Moment> for SystemTime

Available on crate feature std only.
Source§

fn from(moment: Moment) -> Self

Converts to this type from the input type.
Source§

impl From<SystemTime> for Moment

Available on crate feature std only.
Source§

fn from(time: SystemTime) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Moment

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Moment

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Ord for Moment

Source§

fn cmp(&self, other: &Moment) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Moment

Source§

fn eq(&self, other: &Moment) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Moment

Source§

fn partial_cmp(&self, other: &Moment) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<Duration> for Moment

Source§

type Output = Moment

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Duration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Moment

Source§

type Output = TimeInterval

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Copy for Moment

Source§

impl Eq for Moment

Source§

impl StructuralPartialEq for Moment

Auto Trait Implementations§

§

impl Freeze for Moment

§

impl RefUnwindSafe for Moment

§

impl Send for Moment

§

impl Sync for Moment

§

impl Unpin for Moment

§

impl UnwindSafe for Moment

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.