Instant

Struct Instant 

Source
#[non_exhaustive]
pub struct Instant(/* private fields */);
Expand description

The native Rust implementation of Temporal.Instant.

Represents a precise moment in time measured as nanoseconds since the Unix epoch (1970-01-01T00:00:00[UTC]). An Instant provides a universal timestamp that represents the same moment regardless of timezone or calendar system.

Use Instant when you need to record exact moments in time, measure elapsed time, or work with high-precision timestamps. Unlike PlainDateTime, an Instant represents an absolute point on the timeline.

§Examples

§Creating instants

use temporal_rs::Instant;

// From epoch nanoseconds (high-precision timestamps)
let precise_moment = Instant::try_new(1609459200000000000).unwrap();
assert_eq!(precise_moment.epoch_milliseconds(), 1609459200000);

// From epoch milliseconds (common in web applications)
let web_timestamp = Instant::from_epoch_milliseconds(1609459200000).unwrap();
assert_eq!(web_timestamp.epoch_nanoseconds().as_i128(), 1609459200000000000);

§Parsing ISO 8601 instant strings

use temporal_rs::Instant;
use core::str::FromStr;

// Parse ISO 8601 instant strings (must include timezone info)
let instant = Instant::from_str("2024-03-15T14:30:45.123Z").unwrap();
assert_eq!(instant.epoch_milliseconds(), 1710513045123);

// Parse instants with different timezone notations
let instant2 = Instant::from_str("2024-03-15T14:30:45.123+00:00").unwrap();
let instant3 = Instant::from_str("2024-03-15T14:30:45.123-00:00").unwrap();
assert_eq!(instant, instant2);
assert_eq!(instant2, instant3);

§Instant arithmetic

use temporal_rs::{Instant, Duration};
use core::str::FromStr;

let instant = Instant::try_new(1609459200000000000).unwrap(); // 2021-01-01T00:00:00Z

// Add time duration (only time durations, not date durations)
let later = instant.add(&Duration::from_str("PT1H30M").unwrap()).unwrap();
let expected_ns = 1609459200000000000 + (1 * 3600 + 30 * 60) * 1_000_000_000;
assert_eq!(later.epoch_nanoseconds().as_i128(), expected_ns);

// Calculate difference between instants
let earlier = Instant::try_new(1609459200000000000 - 3600_000_000_000).unwrap();
let duration = earlier.until(&instant, Default::default()).unwrap();
assert_eq!(duration.seconds(), 3600);

§Instant precision and limits

use temporal_rs::Instant;

// Instants have well-defined limits based on approximately 100 million days before/after Unix epoch
let max_ns = 8_640_000_000_000_000_000_000i128;  // ~100M days * 24 * 60 * 60 * 1e9
let min_ns = -max_ns;

let max_instant = Instant::try_new(max_ns).unwrap();
let min_instant = Instant::try_new(min_ns).unwrap();

// Values outside the range will fail
assert!(Instant::try_new(max_ns + 1).is_err());
assert!(Instant::try_new(min_ns - 1).is_err());

§Converting to ZonedDateTime (requires provider)

use temporal_rs::{Instant, TimeZone, Calendar};

let instant = Instant::try_new(1609459200000000000).unwrap();
let timezone = TimeZone::try_from_str("America/New_York").unwrap();

// Convert to a zoned date-time for display in local time
let zdt = instant.to_zoned_date_time_iso(timezone);
assert_eq!(zdt.timezone().identifier(), "America/New_York");
assert_eq!(zdt.calendar().identifier(), "iso8601");

§Rounding instants

use temporal_rs::{Instant, options::{RoundingOptions, Unit}};

let instant = Instant::try_new(1609459245123456789).unwrap(); // some precise moment

let mut opts = RoundingOptions::default();
opts.smallest_unit = Some(Unit::Second);
let rounded = instant.round(opts).unwrap();

// Rounded to the nearest second
assert_eq!(rounded.epoch_nanoseconds().as_i128() % 1_000_000_000, 0);

§Reference

For more information, see the MDN documentation.

Implementations§

Source§

impl Instant

Source

pub fn to_ixdtf_string( &self, timezone: Option<TimeZone>, options: ToStringRoundingOptions, ) -> TemporalResult<String>

Returns the RFC9557 (IXDTF) string for this Instant with the provided options

Enable with the compiled_data feature flag.

Source

pub fn to_ixdtf_writeable( &self, timezone: Option<TimeZone>, options: ToStringRoundingOptions, ) -> TemporalResult<impl Writeable + '_>

Returns the RFC9557 (IXDTF) string for this Instant with the provided options as a Writeable

Enable with the compiled_data feature flag.

Source

pub fn to_zoned_date_time_iso( &self, time_zone: TimeZone, ) -> TemporalResult<ZonedDateTime>

Source§

impl Instant

Source

pub fn as_i128(&self) -> i128

Source§

impl Instant

Source

pub fn try_new(nanoseconds: i128) -> TemporalResult<Self>

Create a new validated Instant.

Source

pub fn from_epoch_milliseconds(epoch_milliseconds: i64) -> TemporalResult<Self>

Creates a new Instant from the provided Epoch millisecond value.

Source

pub fn from_utf8(s: &[u8]) -> TemporalResult<Self>

Source

pub fn add(&self, duration: &Duration) -> TemporalResult<Self>

Adds a Duration to the current Instant, returning an error if the Duration contains a DateDuration.

Source

pub fn subtract(&self, duration: &Duration) -> TemporalResult<Self>

Subtract a Duration to the current Instant, returning an error if the Duration contains a DateDuration.

Source

pub fn since( &self, other: &Self, settings: DifferenceSettings, ) -> TemporalResult<Duration>

Returns a Duration representing the duration since provided Instant

Source

pub fn until( &self, other: &Self, settings: DifferenceSettings, ) -> TemporalResult<Duration>

Returns a Duration representing the duration until provided Instant

Source

pub fn round(&self, options: RoundingOptions) -> TemporalResult<Self>

Returns an Instant by rounding the current Instant according to the provided settings.

Source

pub fn epoch_milliseconds(&self) -> i64

Returns the epochMilliseconds value for this Instant.

Source

pub fn epoch_nanoseconds(&self) -> &EpochNanoseconds

Returns the EpochNanoseconds value for this Instant.

Source

pub fn to_zoned_date_time_iso_with_provider( &self, time_zone: TimeZone, provider: &(impl TimeZoneProvider + ?Sized), ) -> TemporalResult<ZonedDateTime>

Returns a ZonedDateTime for the current Instant.

Source§

impl Instant

Source

pub fn to_ixdtf_string_with_provider( &self, timezone: Option<TimeZone>, options: ToStringRoundingOptions, provider: &(impl TimeZoneProvider + ?Sized), ) -> TemporalResult<String>

Returns an RFC9557 IXDTF string representing the current Instant.

Source

pub fn to_ixdtf_writeable_with_provider( &self, timezone: Option<TimeZone>, options: ToStringRoundingOptions, provider: &(impl TimeZoneProvider + ?Sized), ) -> TemporalResult<impl Writeable + '_>

Returns a Writeable for formatting the current Instant in RFC9557’s IXDTF.

Trait Implementations§

Source§

impl Clone for Instant

Source§

fn clone(&self) -> Instant

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 Instant

Source§

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

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

impl From<EpochNanoseconds> for Instant

Source§

fn from(value: EpochNanoseconds) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Instant

Source§

type Err = TemporalError

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 Ord for Instant

Source§

fn cmp(&self, other: &Instant) -> 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 Instant

Source§

fn eq(&self, other: &Instant) -> 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 Instant

Source§

fn partial_cmp(&self, other: &Instant) -> 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 Copy for Instant

Source§

impl Eq for Instant

Source§

impl StructuralPartialEq for Instant

Auto Trait Implementations§

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, 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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,