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
impl Moment
Sourcepub const fn from_unix(seconds: i64, nanoseconds: u32) -> Self
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.
Sourcepub const fn from_unix_seconds(seconds: i64) -> Self
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");Sourcepub const fn from_unix_milliseconds(milliseconds: i64) -> Self
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.
Sourcepub const fn from_unix_microseconds(microseconds: i128) -> Self
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.
Sourcepub const fn from_unix_nanoseconds(nanoseconds: i128) -> Self
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.
Sourcepub fn from_julian_day(jd: f64) -> Result<Self, Error>
Available on crate feature astronomy only.
pub fn from_julian_day(jd: f64) -> Result<Self, Error>
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.
Sourcepub fn from_julian_ephemeris_day(jde: f64) -> Result<Self, Error>
Available on crate feature astronomy only.
pub fn from_julian_ephemeris_day(jde: f64) -> Result<Self, Error>
astronomy only.Creates a Moment corresponding to the given Julian day (JD) in
the Dynamical timescale (also known as Ephemeris time).
Source§impl Moment
impl Moment
Sourcepub const fn to_unix(self) -> (i64, u32)
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.
Sourcepub const fn to_unix_seconds(self) -> i64
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.
Sourcepub const fn to_unix_milliseconds(self) -> i64
pub const fn to_unix_milliseconds(self) -> i64
Gets the number of milliseconds since the Unix epoch.
Sourcepub const fn to_unix_microseconds(self) -> i64
pub const fn to_unix_microseconds(self) -> i64
Gets the number of microseconds since the Unix epoch.
Sourcepub const fn to_unix_nanoseconds(self) -> i128
pub const fn to_unix_nanoseconds(self) -> i128
Gets the number of nanoseconds since the Unix epoch.
Sourcepub fn to_julian_day(self) -> f64
Available on crate feature astronomy only.
pub fn to_julian_day(self) -> f64
astronomy only.Gets the Julian day (JD) for this moment.
Sourcepub fn to_julian_year(self) -> f64
Available on crate feature astronomy only.
pub fn to_julian_year(self) -> f64
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.
Trait Implementations§
Source§impl From<Moment> for SystemTime
Available on crate feature std only.
impl From<Moment> for SystemTime
std only.Source§impl From<SystemTime> for Moment
Available on crate feature std only.
impl From<SystemTime> for Moment
std only.