Struct time::OffsetDateTime[][src]

pub struct OffsetDateTime { /* fields omitted */ }
Expand description

A PrimitiveDateTime with a UtcOffset.

All comparisons are performed using the UTC time.

Implementations

impl OffsetDateTime[src]

pub fn now() -> Self[src]

This is supported on crate feature std only.

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

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

pub const fn to_offset(self, offset: UtcOffset) -> Self[src]

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

assert_eq!(
    date!(2000-01-01)
        .midnight()
        .using_offset(offset!(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!(1999-12-31).with_time(time!(13:00)).using_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);

pub const fn unix_epoch() -> Self[src]

Midnight, 1 January, 1970 (UTC).

assert_eq!(
    OffsetDateTime::unix_epoch(),
    date!(1970-01-01)
        .midnight()
        .using_offset(offset!(UTC)),
);

pub fn from_unix_timestamp(timestamp: i64) -> Self[src]

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()
        .using_offset(offset!(UTC)),
);

pub const fn offset(self) -> UtcOffset[src]

Get the UtcOffset.

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

pub fn timestamp(self) -> i64[src]

Get the Unix timestamp.

assert_eq!(
    PrimitiveDateTime::unix_epoch()
        .using_offset(offset!(UTC))
        .timestamp(),
    0,
);
assert_eq!(
    PrimitiveDateTime::unix_epoch()
        .using_offset(offset!(-1))
        .timestamp(),
    0,
);

pub fn date(self) -> Date[src]

Get the Date in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .using_offset(offset!(UTC))
        .date(),
    date!(2019-01-01),
);
assert_eq!(
    date!(2019-01-01)
        .midnight()
        .using_offset(offset!(-1))
        .date(),
    date!(2018-12-31),
);

pub fn time(self) -> Time[src]

Get the Time in the stored offset.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .using_offset(offset!(UTC))
        .time(),
    time!(0:00)
);
assert_eq!(
    date!(2019-01-01)
        .midnight()
        .using_offset(offset!(-1))
        .time(),
    time!(23:00)
);

pub fn year(self) -> i32[src]

Get the year of the date in the stored offset.

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

pub fn month(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .month(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .using_offset(offset!(+1))
        .month(),
    1,
);

pub fn day(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .day(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .using_offset(offset!(+1))
        .day(),
    1,
);

pub fn month_day(self) -> (u8, u8)[src]

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()
        .using_offset(offset!(UTC))
        .month_day(),
    (1, 1),
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .using_offset(offset!(+1))
        .month_day(),
    (1, 1),
);

pub fn ordinal(self) -> u16[src]

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()
        .using_offset(offset!(UTC))
        .ordinal(),
    1,
);
assert_eq!(
    date!(2019-12-31)
        .with_time(time!(23:00))
        .using_offset(offset!(+1))
        .ordinal(),
    1,
);

pub fn iso_year_week(self) -> (i32, u8)[src]

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

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

pub fn week(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .week(),
    1,
);
assert_eq!(
    date!(2020-01-01)
        .midnight()
        .using_offset(offset!(UTC))
        .week(),
    1,
);
assert_eq!(
    date!(2020-12-31)
        .midnight()
        .using_offset(offset!(UTC))
        .week(),
    53,
);
assert_eq!(
    date!(2021-01-01)
        .midnight()
        .using_offset(offset!(UTC))
        .week(),
    53,
);

pub fn weekday(self) -> Weekday[src]

Get the weekday of the date in the stored offset.

This current uses Zeller’s congruence internally.

assert_eq!(
    date!(2019-01-01)
        .midnight()
        .using_offset(offset!(UTC))
        .weekday(),
    Tuesday,
);
assert_eq!(
    date!(2019-02-01)
        .midnight()
        .using_offset(offset!(UTC))
        .weekday(),
    Friday,
);
assert_eq!(
    date!(2019-03-01)
        .midnight()
        .using_offset(offset!(UTC))
        .weekday(),
    Friday,
);

pub fn hour(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .hour(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .using_offset(offset!(-2))
        .hour(),
    21,
);

pub fn minute(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .minute(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .using_offset(offset!(+0:30))
        .minute(),
    29,
);

pub fn second(self) -> u8[src]

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()
        .using_offset(offset!(UTC))
        .second(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59))
        .using_offset(offset!(+0:00:30))
        .second(),
    29,
);

pub fn millisecond(self) -> u16[src]

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()
        .using_offset(offset!(UTC))
        .millisecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999))
        .using_offset(offset!(UTC))
        .millisecond(),
    999,
);

pub fn microsecond(self) -> u32[src]

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()
        .using_offset(offset!(UTC))
        .microsecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999_999))
        .using_offset(offset!(UTC))
        .microsecond(),
    999_999,
);

pub fn nanosecond(self) -> u32[src]

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()
        .using_offset(offset!(UTC))
        .nanosecond(),
    0,
);
assert_eq!(
    date!(2019-01-01)
        .with_time(time!(23:59:59.999_999_999))
        .using_offset(offset!(UTC))
        .nanosecond(),
    999_999_999,
);

impl OffsetDateTime[src]

Methods that allow formatting the OffsetDateTime.

pub fn format(self, format: &str) -> String[src]

Format the OffsetDateTime using the provided string.

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

pub fn parse(s: &str, format: &str) -> Result<Self, ParseError>[src]

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().using_offset(offset!(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)).using_offset(offset!(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)).using_offset(offset!(UTC))),
);

Trait Implementations

impl Add<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the + operator.

fn add(self, duration: Duration) -> Self::Output[src]

Performs the + operation. Read more

impl Add<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the + operator.

fn add(self, duration: StdDuration) -> Self::Output[src]

Performs the + operation. Read more

impl AddAssign<Duration> for OffsetDateTime[src]

fn add_assign(&mut self, duration: Duration)[src]

Performs the += operation. Read more

impl AddAssign<Duration> for OffsetDateTime[src]

fn add_assign(&mut self, duration: StdDuration)[src]

Performs the += operation. Read more

impl Clone for OffsetDateTime[src]

fn clone(&self) -> OffsetDateTime[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for OffsetDateTime[src]

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

Formats the value using the given formatter. Read more

impl Hash for OffsetDateTime[src]

fn hash<H: Hasher>(&self, hasher: &mut H)[src]

Feeds this value into the given Hasher. Read more

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given Hasher. Read more

impl Ord for OffsetDateTime[src]

fn cmp(&self, rhs: &Self) -> Ordering[src]

This method returns an Ordering between self and other. Read more

#[must_use]
fn max(self, other: Self) -> Self
1.21.0[src]

Compares and returns the maximum of two values. Read more

#[must_use]
fn min(self, other: Self) -> Self
1.21.0[src]

Compares and returns the minimum of two values. Read more

#[must_use]
fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]

Restrict a value to a certain interval. Read more

impl PartialEq<OffsetDateTime> for OffsetDateTime[src]

fn eq(&self, rhs: &Self) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

impl PartialOrd<OffsetDateTime> for OffsetDateTime[src]

fn partial_cmp(&self, rhs: &Self) -> Option<Ordering>[src]

This method returns an ordering between self and other values if one exists. Read more

#[must_use]
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than (for self and other) and is used by the < operator. Read more

#[must_use]
fn le(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

#[must_use]
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

#[must_use]
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]

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

impl Sub<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the - operator.

fn sub(self, duration: Duration) -> Self::Output[src]

Performs the - operation. Read more

impl Sub<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the - operator.

fn sub(self, duration: StdDuration) -> Self::Output[src]

Performs the - operation. Read more

impl Sub<OffsetDateTime> for OffsetDateTime[src]

type Output = Duration

The resulting type after applying the - operator.

fn sub(self, rhs: Self) -> Self::Output[src]

Performs the - operation. Read more

impl SubAssign<Duration> for OffsetDateTime[src]

fn sub_assign(&mut self, duration: Duration)[src]

Performs the -= operation. Read more

impl SubAssign<Duration> for OffsetDateTime[src]

fn sub_assign(&mut self, duration: StdDuration)[src]

Performs the -= operation. Read more

impl Copy for OffsetDateTime[src]

impl Eq for OffsetDateTime[src]

impl StructuralEq for OffsetDateTime[src]

Auto Trait Implementations

impl RefUnwindSafe for OffsetDateTime

impl Send for OffsetDateTime

impl Sync for OffsetDateTime

impl Unpin for OffsetDateTime

impl UnwindSafe for OffsetDateTime

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.