Struct Timestamp
#[repr(C)]
pub struct Timestamp { /* private fields */ }
A Unix timestamp with nanosecond precision.
This type represents a point in time as a number of seconds and nanoseconds elapsed since the Unix epoch (1970-01-01 00:00:00 UTC). Negative values represent times before the Unix epoch.
Implementations
impl Timestamp
const UNIX_EPOCH: Self = _;A
Timestamprepresenting the Unix epoch (1970-01-01 00:00:00 UTC).const MIN: Self = _;The minimum valid
Timestamp.The moment in time represented by this value may vary depending on the feature flags enabled.
const MAX: Self = _;The maximum valid
Timestamp.The moment in time represented by this value may vary depending on the feature flags enabled.
fn now() -> SelfCreate a new
Timestamprepresenting the current moment in time.# use Timestamp; assert!;const fn new(seconds: i64, nanoseconds: u32) -> Result<Self, ComponentRange>Create a
Timestampfrom the provided Unix timestamp in seconds and nanoseconds, returning an error if the resulting value is out of range.# use Timestamp; assert!; assert!;const fn from_seconds(seconds: i64) -> Result<Self, ComponentRange>Create a
Timestampfrom the provided Unix timestamp in seconds, returning an error if the resulting value is out of range.# use Timestamp; assert!; assert!;const fn from_milliseconds(milliseconds: i64) -> Result<Self, ComponentRange>Create a
Timestampfrom the provided Unix timestamp in milliseconds, returning an error if the resulting value is out of range.# use Timestamp; assert!; assert!;const fn from_microseconds(microseconds: i128) -> Result<Self, ComponentRange>Create a
Timestampfrom the provided Unix timestamp in microseconds, returning an error if the resulting value is out of range.# use Timestamp; assert!; assert!;const fn from_nanoseconds(nanoseconds: i128) -> Result<Self, ComponentRange>Create a
Timestampfrom the provided Unix timestamp in nanoseconds, returning an error if the resulting value is out of range.# use Timestamp; assert!; assert!;const fn to_offset(self, offset: UtcOffset) -> OffsetDateTimeConvert the
Timestampto anOffsetDateTimeat the provided offset.# use ; assert_eq!;Panics
This panics if the resulting date-time with the provided offset is outside the supported range. Consider using
checked_to_offsetfor a non-panicking alternative.const fn checked_to_offset(self, offset: UtcOffset) -> Option<OffsetDateTime>Convert the
Timestampto anOffsetDateTimewith the provided offset, returningNoneif the resulting value is out of range.# use ; assert!;const fn to_utc(self) -> UtcDateTimeConvert the
Timestampto aUtcDateTime.# use ; assert_eq!;const fn as_seconds(self) -> i64Get the number of seconds since the Unix epoch.
Negative values represent moments before the Unix epoch.
# use timestamp; assert_eq!;const fn as_milliseconds(self) -> i64Get the number of milliseconds since the Unix epoch.
Negative values represent moments before the Unix epoch.
# use timestamp; assert_eq!;const fn as_microseconds(self) -> i128Get the number of microseconds since the Unix epoch.
Negative values represent moments before the Unix epoch.
# use timestamp; assert_eq!;const fn as_nanoseconds(self) -> i128Get the number of nanoseconds since the Unix epoch.
Negative values represent moments before the Unix epoch.
# use timestamp; assert_eq!;const fn date(self) -> DateGet the
Dateof the timestamp in UTC.# use ; assert_eq!;const fn time(self) -> TimeGet the
Timeof the timestamp in UTC.# use ; assert_eq!;const fn year(self) -> i32Get the year of the timestamp in UTC.
# use timestamp; assert_eq!;const fn month(self) -> MonthGet the month of the timestamp in UTC.
# use Month; # use timestamp; assert_eq!;const fn day(self) -> u8Get the day of the month of the timestamp in UTC.
The returned value will always be in the range
1..=31.# use timestamp; assert_eq!;const fn ordinal(self) -> u16Get the day of the year of the timestamp in UTC.
The returned value will always be in the range
1..=366.# use timestamp; assert_eq!;const fn iso_week(self) -> u8Get the ISO week number of the timestamp in UTC.
The returned value will always be in the range
1..=53.# use timestamp; assert_eq!;const fn sunday_based_week(self) -> u8Get the Sunday-based week number of the timestamp in UTC.
The returned value will always be in the range
0..=53.# use timestamp; assert_eq!;const fn monday_based_week(self) -> u8Get the Monday-based week number of the timestamp in UTC.
The returned value will always be in the range
0..=53.# use timestamp; assert_eq!;const fn to_calendar_date(self) -> (i32, Month, u8)Get the calendar date (year, month, day) of the timestamp in UTC.
# use Month; # use timestamp; assert_eq!;const fn to_ordinal_date(self) -> (i32, u16)Get the ordinal date (year, ordinal day) of the timestamp in UTC.
# use timestamp; assert_eq!;const fn to_iso_week_date(self) -> (i32, u8, Weekday)Get the ISO week date (year, week number, weekday) of the timestamp in UTC.
# use Weekday; # use timestamp; assert_eq!;const fn weekday(self) -> WeekdayGet the weekday of the timestamp in UTC.
# use Weekday; # use timestamp; assert_eq!;const fn to_julian_day(self) -> i32Get the Julian day of the timestamp.
# use timestamp; assert_eq!;const fn as_hms(self) -> (u8, u8, u8)Get the hours, minutes, and seconds of the timestamp in UTC.
# use timestamp; assert_eq!;const fn as_hms_milli(self) -> (u8, u8, u8, u16)Get the hours, minutes, seconds, and milliseconds of the timestamp in UTC.
# use timestamp; assert_eq!;const fn as_hms_micro(self) -> (u8, u8, u8, u32)Get the hours, minutes, seconds, and microseconds of the timestamp in UTC.
# use timestamp; assert_eq!;const fn as_hms_nano(self) -> (u8, u8, u8, u32)Get the hours, minutes, seconds, and nanoseconds of the timestamp in UTC.
# use timestamp; assert_eq!;const fn hour(self) -> u8Get the hour of the timestamp in UTC.
# use timestamp; assert_eq!;const fn minute(self) -> u8Get the minute of the timestamp in UTC.
# use timestamp; assert_eq!;const fn second(self) -> u8Get the second of the timestamp in UTC.
# use timestamp; assert_eq!;const fn millisecond(self) -> u16Get the millisecond of the timestamp in UTC.
# use timestamp; assert_eq!;const fn microsecond(self) -> u32Get the microsecond of the timestamp in UTC.
# use timestamp; assert_eq!;const fn nanosecond(self) -> u32Get the nanosecond of the timestamp in UTC.
# use timestamp; assert_eq!;const fn checked_add(self, duration: SignedDuration) -> Option<Self>Checked addition of a
SignedDuration, returningNoneif the result is out of range.# use timestamp; # use NumericalDuration as _; assert_eq!; assert_eq!;const fn checked_sub(self, duration: SignedDuration) -> Option<Self>Checked subtraction of a
SignedDuration, returningNoneif the result is out of range.# use timestamp; # use NumericalDuration as _; assert_eq!; assert_eq!;const fn saturating_add(self, duration: SignedDuration) -> SelfSaturating addition of a
SignedDuration.Returns
Timestamp::MAXorTimestamp::MINif the result is out of range.# use Timestamp; # use timestamp; # use NumericalDuration as _; assert_eq!; assert_eq!; assert_eq!;const fn saturating_sub(self, duration: SignedDuration) -> SelfSaturating subtraction of a
SignedDuration.Returns
Timestamp::MAXorTimestamp::MINif the result is out of range.# use Timestamp; # use timestamp; # use NumericalDuration as _; assert_eq!; assert_eq!; assert_eq!;
impl Timestamp
const fn replace_time(self, time: Time) -> SelfReplace the time, preserving the date.
# use ; assert_eq!;const fn replace_date(self, date: Date) -> SelfReplace the date, preserving the time.
# use ; assert_eq!;const fn replace_year(self, year: i32) -> Result<Self, ComponentRange>Replace the year, preserving the month and day. If the date is February 29 and the resulting year is not a leap year, an error is returned.
# use timestamp; assert_eq!; assert!; // -1_000_000 isn't a valid year assert!; // 1_000_000 isn't a valid yearconst fn replace_month(self, month: Month) -> Result<Self, ComponentRange>Replace the month of the year, preserving the year and day. If the day is invalid for the resulting month, an error is returned.
# use timestamp; # use Month; assert_eq!; assert!; // the day of the month is 30, which is invalid for Februaryconst fn replace_day(self, day: u8) -> Result<Self, ComponentRange>Replace the day of the month.
# use timestamp; assert_eq!; assert!; // 00 isn't a valid day assert!; // 32 isn't a valid dayconst fn replace_ordinal(self, ordinal: u16) -> Result<Self, ComponentRange>Replace the day of the year.
# use timestamp; assert_eq!; assert!; // 0 isn't a valid day of the year assert!; // the timestamp is in 2019, which isn't a leap yearconst fn replace_hour(self, hour: u8) -> Result<Self, ComponentRange>Replace the clock hour.
# use timestamp; assert_eq!; assert!; // 24 isn't a valid hourconst fn replace_minute(self, minute: u8) -> Result<Self, ComponentRange>Replace the minutes within the hour.
# use timestamp; assert_eq!; assert!; // 60 isn't a valid minuteconst fn replace_second(self, second: u8) -> Result<Self, ComponentRange>Replace the seconds within the minute.
# use timestamp; assert_eq!; assert!; // 60 isn't a valid secondconst fn replace_millisecond(self, millisecond: u16) -> Result<Self, ComponentRange>Replace the milliseconds within the second.
# use timestamp; assert_eq!; assert!; // 1_000 isn't a valid millisecondconst fn replace_microsecond(self, microsecond: u32) -> Result<Self, ComponentRange>Replace the microseconds within the second.
# use timestamp; assert_eq!; assert!; // 1_000_000 isn't a valid microsecondconst fn replace_nanosecond(self, nanosecond: u32) -> Result<Self, ComponentRange>Replace the nanoseconds within the second.
# use timestamp; assert_eq!; assert!; // 1_000_000_000 isn't a valid nanosecond
Trait Implementations
impl Add<Duration> for Timestamp
type Output = Timestamp;fn add(self, rhs: StdDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl Add<SignedDuration> for Timestamp
type Output = Timestamp;fn add(self, rhs: SignedDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl AddAssign<Duration> for Timestamp
fn add_assign(&mut self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl AddAssign<SignedDuration> for Timestamp
fn add_assign(&mut self, rhs: SignedDuration)Panics
This may panic if an overflow occurs.
impl Clone for Timestamp
fn clone(&self) -> Timestamp
impl Copy for Timestamp
impl Debug for Timestamp
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Display for Timestamp
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Timestamp
impl From<OffsetDateTime> for Timestamp
fn from(datetime: OffsetDateTime) -> SelfPanics
This may panic if an overflow occurs.
impl From<SystemTime> for Timestamp
fn from(datetime: SystemTime) -> SelfPanics
This may panic if an overflow occurs.
impl From<UtcDateTime> for Timestamp
fn from(datetime: UtcDateTime) -> Self
impl Hash for Timestamp
fn hash<H: Hasher>(&self, state: &mut H)
impl Ord for Timestamp
fn cmp(&self, other: &Self) -> Ordering
impl PartialEq for Timestamp
fn eq(&self, other: &Self) -> bool
impl PartialEq<OffsetDateTime> for Timestamp
fn eq(&self, other: &OffsetDateTime) -> bool
impl PartialEq<SystemTime> for Timestamp
fn eq(&self, other: &SystemTime) -> bool
impl PartialEq<UtcDateTime> for Timestamp
fn eq(&self, other: &UtcDateTime) -> bool
impl PartialOrd for Timestamp
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
impl PartialOrd<OffsetDateTime> for Timestamp
fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering>
impl PartialOrd<SystemTime> for Timestamp
fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering>
impl PartialOrd<UtcDateTime> for Timestamp
fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering>
impl Sub for Timestamp
type Output = SignedDuration;fn sub(self, rhs: Self) -> Self::Output
impl Sub<Duration> for Timestamp
type Output = Timestamp;fn sub(self, rhs: StdDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl Sub<OffsetDateTime> for Timestamp
type Output = SignedDuration;fn sub(self, rhs: OffsetDateTime) -> Self::Output
impl Sub<SignedDuration> for Timestamp
type Output = Timestamp;fn sub(self, rhs: SignedDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl Sub<SystemTime> for Timestamp
type Output = SignedDuration;fn sub(self, rhs: SystemTime) -> Self::Output
impl Sub<UtcDateTime> for Timestamp
type Output = SignedDuration;fn sub(self, rhs: UtcDateTime) -> Self::Output
impl SubAssign<Duration> for Timestamp
fn sub_assign(&mut self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl SubAssign<SignedDuration> for Timestamp
fn sub_assign(&mut self, rhs: SignedDuration)Panics
This may panic if an overflow occurs.
Auto Trait Implementations
impl Freeze for Timestamp
impl RefUnwindSafe for Timestamp
impl Send for Timestamp
impl Sync for Timestamp
impl Unpin for Timestamp
impl UnsafeUnpin for Timestamp
impl UnwindSafe for Timestamp
Blanket Implementations
impl<T> Any for Timestamp
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Timestamp
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Timestamp
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Timestamp
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Timestamp
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Timestamp
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> ToString for Timestamp
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for Timestamp
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Timestamp
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Timestamp
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>