OffsetDateTime

Struct OffsetDateTime 

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

A PrimitiveDateTime with a UtcOffset.

All comparisons are performed using the UTC time.

Implementations§

Source§

impl OffsetDateTime

Source

pub fn now() -> OffsetDateTime

👎Deprecated since 0.2.11: This function returns a value with an offset of UTC, which is not apparent from its name alone. You should use OffsetDateTime::now_utc() instead.

Create a new OffsetDateTime with the current date and time in UTC.

assert!(OffsetDateTime::now().year() >= 2019);
assert_eq!(OffsetDateTime::now().offset(), offset!(UTC));
Source

pub fn now_utc() -> OffsetDateTime

Create a new OffsetDateTime with the current date and time in UTC.

assert!(OffsetDateTime::now_utc().year() >= 2019);
assert_eq!(OffsetDateTime::now_utc().offset(), offset!(UTC));
Source

pub fn now_local() -> OffsetDateTime

👎Deprecated since 0.2.23: UTC is returned if the local offset cannot be determined

Create a new OffsetDateTime with the current date and time in the local offset.

assert!(OffsetDateTime::now_local().year() >= 2019);
Source

pub fn try_now_local() -> Result<OffsetDateTime, IndeterminateOffset>

Attempt to create a new OffsetDateTime with the current date and time in the local offset. If the offset cannot be determined, an error is returned.

let now = OffsetDateTime::try_now_local();
assert!(now.is_ok());
Source

pub const fn to_offset(self, offset: UtcOffset) -> OffsetDateTime

Convert the OffsetDateTime from the current UtcOffset to the provided UtcOffset.

assert_eq!(
    date!(2000-01-01)
        .midnight()
        .assume_utc()
        .to_offset(offset!(-1))
        .year(),
    1999,
);

// Let's see what time Sydney's new year's celebration is in New York
// and Los Angeles.

// Construct midnight on new year's in Sydney. This is equivalent to
// 13:00 UTC.
let sydney = date!(2000-01-01).midnight().assume_offset(offset!(+11));
let new_york = sydney.to_offset(offset!(-5));
let los_angeles = sydney.to_offset(offset!(-8));
assert_eq!(sydney.hour(), 0);
assert_eq!(new_york.hour(), 8);
assert_eq!(los_angeles.hour(), 5);
Source

pub const fn unix_epoch() -> OffsetDateTime

Midnight, 1 January, 1970 (UTC).

assert_eq!(
    OffsetDateTime::unix_epoch(),
    date!(1970-01-01)
        .midnight()
        .assume_utc(),
);
Source

pub fn from_unix_timestamp(timestamp: i64) -> OffsetDateTime

Create an OffsetDateTime from the provided Unix timestamp.

assert_eq!(
    OffsetDateTime::from_unix_timestamp(0),
    OffsetDateTime::unix_epoch(),
);
assert_eq!(
    OffsetDateTime::from_unix_timestamp(1_546_300_800),
    date!(2019-01-01)
        .midnight()
        .assume_utc(),
);

If you have a timestamp-nanosecond pair, you can use something along the lines of the following:

let (timestamp, nanos) = (1, 500_000_000);
assert_eq!(
    OffsetDateTime::from_unix_timestamp(timestamp) + Duration::nanoseconds(nanos),
    OffsetDateTime::unix_epoch() + 1.5.seconds()
);
Source

pub fn from_unix_timestamp_nanos(timestamp: i128) -> OffsetDateTime

Construct an OffsetDateTime from the provided Unix timestamp (in nanoseconds).

assert_eq!(
    OffsetDateTime::from_unix_timestamp_nanos(0),
    OffsetDateTime::unix_epoch(),
);
assert_eq!(
    OffsetDateTime::from_unix_timestamp_nanos(1_546_300_800_000_000_000),
    date!(2019-01-01)
        .midnight()
        .assume_utc(),
);

Note that the range of timestamps possible here is far larger than the valid range of dates storable in this crate. It is the user’s responsibility to ensure the timestamp provided as a parameter is valid. No behavior is guaranteed if this parameter would not result in a valid value.

Source

pub const fn offset(self) -> UtcOffset

Get the UtcOffset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .offset(),
    offset!(UTC),
);
assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_offset(offset!(+1))
        .offset(),
    offset!(+1),
);
Source

pub fn unix_timestamp(self) -> i64

Get the Unix timestamp.

assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .unix_timestamp(),
    0,
);
assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .to_offset(offset!(-1))
        .unix_timestamp(),
    0,
);
Source

pub fn timestamp(self) -> i64

👎Deprecated since 0.2.23: Use OffsetDateTime::unix_timestamp instead

Get the Unix timestamp.

assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .timestamp(),
    0,
);
assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .to_offset(offset!(-1))
        .timestamp(),
    0,
);
Source

pub fn unix_timestamp_nanos(self) -> i128

Get the Unix timestamp in nanoseconds.

use time::{date, offset, time};
assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .unix_timestamp_nanos(),
    0,
);
assert_eq!(
    date!(1970-01-01)
        .with_time(time!(1:00))
        .assume_utc()
        .to_offset(offset!(-1))
        .unix_timestamp_nanos(),
    3_600_000_000_000,
);
Source

pub fn timestamp_nanos(self) -> i128

👎Deprecated since 0.2.23: Use OffsetDateTime::unix_timestamp_nanos instead

Get the Unix timestamp in nanoseconds.

use time::{date, offset, time};
assert_eq!(
    date!(1970-01-01)
        .midnight()
        .assume_utc()
        .unix_timestamp_nanos(),
    0,
);
assert_eq!(
    date!(1970-01-01)
        .with_time(time!(1:00))
        .assume_utc()
        .to_offset(offset!(-1))
        .unix_timestamp_nanos(),
    3_600_000_000_000,
);
Source

pub fn date(self) -> Date

Get the Date in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .date(),
    date!(2019-01-01),
);
assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .to_offset(offset!(-1))
        .date(),
    date!(2018-12-31),
);
Source

pub fn time(self) -> Time

Get the Time in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .time(),
    time!(0:00)
);
assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .to_offset(offset!(-1))
        .time(),
    time!(23:00)
);
Source

pub fn year(self) -> i32

Get the year of the date in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .year(),
    2019,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .assume_utc()
        .to_offset(offset!(+1))
        .year(),
    2020,
);
assert_eq!(
    date!(2020-01-01)
        .midnight()
        .assume_utc()
        .year(),
    2020,
);
Source

pub fn month(self) -> u8

Get the month of the date in the stored offset. If fetching both the month and day, it is more efficient to use OffsetDateTime::month_day.

The returned value will always be in the range 1..=12.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .month(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .assume_utc()
        .to_offset(offset!(+1))
        .month(),
    1,
);
Source

pub fn day(self) -> u8

Get the day of the date in the stored offset. If fetching both the month and day, it is more efficient to use OffsetDateTime::month_day.

The returned value will always be in the range 1..=31.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .day(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .assume_utc()
        .to_offset(offset!(+1))
        .day(),
    1,
);
Source

pub fn month_day(self) -> (u8, u8)

Get the month and day of the date in the stored offset.

The month component will always be in the range 1..=12; the day component in 1..=31.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .month_day(),
    (1, 1),
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .assume_utc()
        .to_offset(offset!(+1))
        .month_day(),
    (1, 1),
);
Source

pub fn ordinal(self) -> u16

Get the day of the year of the date in the stored offset.

The returned value will always be in the range 1..=366.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .ordinal(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .assume_utc()
        .to_offset(offset!(+1))
        .ordinal(),
    1,
);
Source

pub fn iso_year_week(self) -> (i32, u8)

Get the ISO 8601 year and week number in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .iso_year_week(),
    (2019, 1),
);
assert_eq!(
    date!(2019-10-04)
        .midnight()
        .assume_utc()
        .iso_year_week(),
    (2019, 40),
);
assert_eq!(
    date!(2020-01-01)
        .midnight()
        .assume_utc()
        .iso_year_week(),
    (2020, 1),
);
assert_eq!(
    date!(2020-12-31)
        .midnight()
        .assume_utc()
        .iso_year_week(),
    (2020, 53),
);
assert_eq!(
    date!(2021-01-01)
        .midnight()
        .assume_utc()
        .iso_year_week(),
    (2020, 53),
);
Source

pub fn week(self) -> u8

Get the ISO week number of the date in the stored offset.

The returned value will always be in the range 1..=53.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .week(),
    1,
);
assert_eq!(
    date!(2020-01-01)
        .midnight()
        .assume_utc()
        .week(),
    1,
);
assert_eq!(
    date!(2020-12-31)
        .midnight()
        .assume_utc()
        .week(),
    53,
);
assert_eq!(
    date!(2021-01-01)
        .midnight()
        .assume_utc()
        .week(),
    53,
);
Source

pub fn weekday(self) -> Weekday

Get the weekday of the date in the stored offset.

This current uses Zeller’s congruence internally.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .weekday(),
    Tuesday,
);
assert_eq!(
    date!(2019-02-01)
        .midnight()
        .assume_utc()
        .weekday(),
    Friday,
);
assert_eq!(
    date!(2019-03-01)
        .midnight()
        .assume_utc()
        .weekday(),
    Friday,
);
Source

pub fn hour(self) -> u8

Get the clock hour in the stored offset.

The returned value will always be in the range 0..24.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .hour(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .assume_utc()
        .to_offset(offset!(-2))
        .hour(),
    21,
);
Source

pub fn minute(self) -> u8

Get the minute within the hour in the stored offset.

The returned value will always be in the range 0..60.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .minute(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .assume_utc()
        .to_offset(offset!(+0:30))
        .minute(),
    29,
);
Source

pub fn second(self) -> u8

Get the second within the minute in the stored offset.

The returned value will always be in the range 0..60.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .second(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .assume_utc()
        .to_offset(offset!(+0:00:30))
        .second(),
    29,
);
Source

pub fn millisecond(self) -> u16

Get the milliseconds within the second in the stored offset.

The returned value will always be in the range 0..1_000.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .millisecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999))
        .assume_utc()
        .millisecond(),
    999,
);
Source

pub fn microsecond(self) -> u32

Get the microseconds within the second in the stored offset.

The returned value will always be in the range 0..1_000_000.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .microsecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999_999))
        .assume_utc()
        .microsecond(),
    999_999,
);
Source

pub fn nanosecond(self) -> u32

Get the nanoseconds within the second in the stored offset.

The returned value will always be in the range 0..1_000_000_000.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .assume_utc()
        .nanosecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999_999_999))
        .assume_utc()
        .nanosecond(),
    999_999_999,
);
Source§

impl OffsetDateTime

Methods that allow formatting the OffsetDateTime.

Source

pub fn format(self, format: impl Into<Format>) -> String

Format the OffsetDateTime using the provided string.

assert_eq!(
    date!(2019-01-02)
        .midnight()
        .assume_utc()
        .format("%F %r %z"),
    "2019-01-02 12:00:00 am +0000",
);
Source

pub fn lazy_format(self, format: impl Into<Format>) -> impl Display

Format the OffsetDateTime using the provided string.

assert_eq!(
    date!(2019-01-02)
        .midnight()
        .assume_utc()
        .lazy_format("%F %r %z")
        .to_string(),
    "2019-01-02 12:00:00 am +0000",
);
Source

pub fn parse( s: impl AsRef<str>, format: impl Into<Format>, ) -> Result<OffsetDateTime, Error>

Attempt to parse an OffsetDateTime using the provided string.

assert_eq!(
    OffsetDateTime::parse("2019-01-02 00:00:00 +0000", "%F %T %z"),
    Ok(date!(2019-01-02).midnight().assume_utc()),
);
assert_eq!(
    OffsetDateTime::parse("2019-002 23:59:59 +0000", "%Y-%j %T %z"),
    Ok(date!(2019-002).with_time(time!(23:59:59)).assume_utc()),
);
assert_eq!(
    OffsetDateTime::parse("2019-W01-3 12:00:00 pm +0000", "%G-W%V-%u %r %z"),
    Ok(date!(2019-W01-3).with_time(time!(12:00)).assume_utc()),
);

Trait Implementations§

Source§

impl Add<Duration> for OffsetDateTime

Source§

type Output = OffsetDateTime

The resulting type after applying the + operator.
Source§

fn add(self, duration: Duration) -> <OffsetDateTime as Add<Duration>>::Output

Performs the + operation. Read more
Source§

impl Add<Duration> for OffsetDateTime

Source§

type Output = OffsetDateTime

The resulting type after applying the + operator.
Source§

fn add(self, duration: Duration) -> <OffsetDateTime as Add<Duration>>::Output

Performs the + operation. Read more
Source§

impl AddAssign<Duration> for OffsetDateTime

Source§

fn add_assign(&mut self, duration: Duration)

Performs the += operation. Read more
Source§

impl AddAssign<Duration> for OffsetDateTime

Source§

fn add_assign(&mut self, duration: Duration)

Performs the += operation. Read more
Source§

impl Clone for OffsetDateTime

Source§

fn clone(&self) -> OffsetDateTime

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 OffsetDateTime

Source§

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

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

impl Display for OffsetDateTime

Source§

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

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

impl From<SystemTime> for OffsetDateTime

Source§

fn from(system_time: SystemTime) -> OffsetDateTime

Converts to this type from the input type.
Source§

impl Hash for OffsetDateTime

Source§

fn hash<H>(&self, hasher: &mut H)
where H: Hasher,

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 OffsetDateTime

Source§

fn cmp(&self, rhs: &OffsetDateTime) -> 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<SystemTime> for OffsetDateTime

Source§

fn eq(&self, rhs: &SystemTime) -> 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 PartialEq for OffsetDateTime

Source§

fn eq(&self, rhs: &OffsetDateTime) -> 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<SystemTime> for OffsetDateTime

Source§

fn partial_cmp(&self, other: &SystemTime) -> 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 PartialOrd for OffsetDateTime

Source§

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

Source§

type Output = OffsetDateTime

The resulting type after applying the - operator.
Source§

fn sub(self, duration: Duration) -> <OffsetDateTime as Sub<Duration>>::Output

Performs the - operation. Read more
Source§

impl Sub<Duration> for OffsetDateTime

Source§

type Output = OffsetDateTime

The resulting type after applying the - operator.
Source§

fn sub(self, duration: Duration) -> <OffsetDateTime as Sub<Duration>>::Output

Performs the - operation. Read more
Source§

impl Sub<SystemTime> for OffsetDateTime

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: SystemTime) -> <OffsetDateTime as Sub<SystemTime>>::Output

Performs the - operation. Read more
Source§

impl Sub for OffsetDateTime

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: OffsetDateTime) -> <OffsetDateTime as Sub>::Output

Performs the - operation. Read more
Source§

impl SubAssign<Duration> for OffsetDateTime

Source§

fn sub_assign(&mut self, duration: Duration)

Performs the -= operation. Read more
Source§

impl SubAssign<Duration> for OffsetDateTime

Source§

fn sub_assign(&mut self, duration: Duration)

Performs the -= operation. Read more
Source§

impl Copy for OffsetDateTime

Source§

impl Eq for OffsetDateTime

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