Struct utcnow::UtcTime

source ·
pub struct UtcTime { /* private fields */ }
Expand description

A Unix time, i.e. seconds since 1970-01-01 in UTC

Using i64 values as seconds since 1970-01-01, this library will work for the next 292 billion years.

Implementations§

source§

impl UtcTime

source

pub const EPOCH: UtcTime = _

Start of the Unix time epoch, 1970-01-01.

source

pub fn now() -> Result<Self>

Get the current time

This method does the same as calling utcnow().

§Errors

See utcnow() for further information.

§Example
let now = UtcTime::now().unwrap();
let seconds = now.as_secs();
let nanos = now.subsec_nanos();
source

pub const unsafe fn new_unchecked(secs: i64, nanos: u32) -> Self

Build a new UtcTime without normalization

If the value for nanos exceeds 1_000_000_000, your program is malformed. Expect undefined behavior!

§Safety

Expect random and impossible to debug runtime errors if the input is not in range.

source

pub const fn new(secs: i64, nanos: u32) -> Option<Self>

Build a new UtcTime

nanos will be normalized to a values less than 1_000_000_000, the number of nanoseconds in a second. If the resulting number of seconds will exceed i64::MAX, None is returned.

§Example
// August 3, 2022, about 19 o'clock in the evening in CEST.
let timestamp = UtcTime::new(1_659_545_693, 895_531_827).unwrap();
source

pub fn from_system_time(value: SystemTime) -> Option<Self>

Available on crate feature std only.

Convert a SystemTime

§Example
let system_time = SystemTime::now();
let now = UtcTime::from_system_time(system_time).unwrap();
source

pub const fn from_duration(value: Duration) -> Option<Self>

Convert a Duration

The duration is interpreted as seconds since epoch (1970-01-01 in UTC).

§Example
let duration = Duration::from_secs(42);
let timestamp = UtcTime::from_duration(duration).unwrap();
assert_eq!(timestamp.as_nanos(), 42_000_000_000);
source

pub const fn as_secs(self) -> i64

Total number of whole seconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let total_secs = now.as_secs();
assert!(total_secs > 1_658_711_810);
assert!(total_secs < 1_974_324_043); // update before 2032-07-25
source

pub const fn as_millis(self) -> i128

Total number of whole milliseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let total_millis = now.as_millis();
assert!(total_millis > 1_658_711_810_802);
assert!(total_millis < 1_974_324_043_000); // update before 2032-07-25
source

pub const fn as_micros(self) -> i128

Total number of whole microseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let total_micros = now.as_micros();
assert!(total_micros > 1_658_711_810_802_520);
assert!(total_micros < 1_974_324_043_000_000); // update before 2032-07-25
source

pub const fn as_nanos(self) -> i128

Total number of whole nanoseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let total_nanos = now.as_nanos();
assert!(total_nanos > 1_658_711_810_802_520_027);
assert!(total_nanos < 1_974_324_043_000_000_000); // update before 2032-07-25
source

pub const fn subsec_millis(self) -> u32

Fractional number of milliseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let millis = now.subsec_millis();
assert!(millis < 1_000);
source

pub const fn subsec_micros(self) -> u32

Fractional number of microseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let micros = now.subsec_micros();
assert!(micros < 1_000_000);
source

pub const fn subsec_nanos(self) -> u32

Fractional number of nanoseconds since epoch (1970-01-01 in UTC)

§Example
let now = UtcTime::now().unwrap();
let nanos = now.subsec_nanos();
assert!(nanos < 1_000_000_000);
source

pub const fn into_duration(self) -> Result<Duration, ConversionError>

Convert the timestamp to a Duration since epoch (1970-01-01 in UTC)

§Errors

Fails if the timestamp lies before 1970-01-01 in UTC.

§Example
let now = UtcTime::now().unwrap();
let duration = now.into_duration().unwrap();
source

pub fn into_system_time(self) -> Result<SystemTime, ConversionError>

Available on crate feature std only.

Convert the timestamp to a SystemTime

§Errors

The conversion won’t succeed if and only if the stored date is earlier than 1970-01-01.

§Example
let now = UtcTime::now().unwrap();
let system_time = now.into_system_time().unwrap();

Trait Implementations§

source§

impl<'a> Arbitrary<'a> for UtcTime

Available on crate feature arbitrary only.
source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

impl Arbitrary for UtcTime

Available on crate feature quickcheck only.
source§

fn arbitrary(gen: &mut Gen) -> Self

Return an arbitrary value. Read more
source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

Return an iterator of values that are smaller than itself. Read more
source§

impl Arbitrary for UtcTime

Available on crate feature proptest only.
§

type Parameters = <(i64, u32) as Arbitrary>::Parameters

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
§

type Strategy = Map<<(i64, u32) as Arbitrary>::Strategy, fn(_: (i64, u32)) -> UtcTime>

The type of Strategy used to generate values of type Self.
source§

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
source§

impl Archive for UtcTime

Available on crate feature rkyv only.
§

type Archived = ArchivedUtcTime

The archived representation of this type. Read more
§

type Resolver = UtcTimeResolver

The resolver for this type. It must contain all the additional information from serializing needed to make the archived type from the normal type.
source§

unsafe fn resolve( &self, pos: usize, resolver: Self::Resolver, out: *mut Self::Archived )

Creates the archived version of this value at the given position and writes it to the given output. Read more
source§

impl Clone for UtcTime

source§

fn clone(&self) -> UtcTime

Returns a copy 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 UtcTime

source§

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

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

impl Default for UtcTime

source§

fn default() -> UtcTime

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

impl<'de> Deserialize<'de> for UtcTime

Available on crate feature serde only.
source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<D: Fallible + ?Sized> Deserialize<UtcTime, D> for Archived<UtcTime>

Available on crate feature rkyv only.
source§

fn deserialize(&self, deserializer: &mut D) -> Result<UtcTime, D::Error>

Deserializes using the given deserializer
source§

impl Display for UtcTime

source§

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

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

impl FromStr for UtcTime

§

type Err = ConversionError

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 UtcTime

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 UtcTime

source§

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

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

impl PartialEq for UtcTime

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for UtcTime

source§

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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<S: Fallible + ?Sized> Serialize<S> for UtcTime

Available on crate feature rkyv only.
source§

fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error>

Writes the dependencies for the object and returns a resolver that can create the archived type.
source§

impl Serialize for UtcTime

Available on crate feature serde only.
source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TryFrom<&str> for UtcTime

§

type Error = ConversionError

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

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<Duration> for UtcTime

§

type Error = ConversionError

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

fn try_from(value: Duration) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl TryFrom<SystemTime> for UtcTime

Available on crate feature std only.
§

type Error = ConversionError

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

fn try_from(value: SystemTime) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl TryFrom<UtcTime> for Duration

§

type Error = ConversionError

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

fn try_from(value: UtcTime) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl TryFrom<UtcTime> for SystemTime

Available on crate feature std only.
§

type Error = ConversionError

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

fn try_from(value: UtcTime) -> Result<Self, ConversionError>

Performs the conversion.
source§

impl Copy for UtcTime

source§

impl Eq for UtcTime

source§

impl LifetimeFree for UtcTime

Available on crate feature castaway only.
source§

impl StructuralPartialEq for UtcTime

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> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

impl<T> ArchiveUnsized for T
where T: Archive,

§

type Archived = <T as Archive>::Archived

The archived counterpart of this type. Unlike Archive, it may be unsized. Read more
§

type MetadataResolver = ()

The resolver for the metadata of this type. Read more
source§

unsafe fn resolve_metadata( &self, _: usize, _: <T as ArchiveUnsized>::MetadataResolver, _: *mut <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata )

Creates the archived version of the metadata for this value at the given position and writes it to the given output. Read more
source§

unsafe fn resolve_unsized( &self, from: usize, to: usize, resolver: Self::MetadataResolver, out: *mut RelPtr<Self::Archived, <isize as Archive>::Archived> )

Resolves a relative pointer to this value with the given from and to and writes it to the given output. 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> CallHasher for T
where T: Hash + ?Sized,

source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for F
where W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
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> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

impl<T, S> SerializeUnsized<S> for T
where T: Serialize<S>, S: Serializer + ?Sized,

source§

fn serialize_unsized( &self, serializer: &mut S ) -> Result<usize, <S as Fallible>::Error>

Writes the object and returns the position of the archived type.
source§

fn serialize_metadata(&self, _: &mut S) -> Result<(), <S as Fallible>::Error>

Serializes the metadata for the given type.
source§

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

§

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§

default 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>,

§

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>,

§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,