[][src]Struct time::OffsetDateTime

pub struct OffsetDateTime { /* fields omitted */ }

A PrimitiveDateTime with a UtcOffset.

For equality, comparisons, and hashing, calculations are performed using the Unix timestamp.

Methods

impl OffsetDateTime[src]

pub fn now() -> Self[src]

This is supported on feature="std" only.

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

assert!(OffsetDateTime::now().year() >= 2019);
assert_eq!(OffsetDateTime::now().offset(), UtcOffset::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::try_from_ymd(2000, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .to_offset(UtcOffset::hours(-1))
        .year(),
    1999,
);

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

Midnight, 1 January, 1970 (UTC).

assert_eq!(
    OffsetDateTime::unix_epoch(),
    Date::try_from_ymd(1970, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC),
);

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

Get the UtcOffset.

assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .offset(),
    UtcOffset::UTC,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::hours(1))
        .offset(),
    UtcOffset::hours(1),
);

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

Get the Unix timestamp.

assert_eq!(
    PrimitiveDateTime::unix_epoch()
        .using_offset(UtcOffset::UTC)
        .timestamp(),
    0,
);
assert_eq!(
    PrimitiveDateTime::unix_epoch()
        .using_offset(UtcOffset::hours(-1))
        .timestamp(),
    3_600,
);

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

Get the Date in the stored offset.

assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .date(),
    Date::try_from_ymd(2019, 1, 1).unwrap(),
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::hours(-1))
        .date(),
    Date::try_from_ymd(2018, 12, 31).unwrap(),
);

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

Get the Time in the stored offset.

assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .time(),
    Time::try_from_hms(0, 0, 0).unwrap(),
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::hours(-1))
        .time(),
    Time::try_from_hms(23, 0, 0).unwrap(),
);

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

Get the year of the date in the stored offset.

assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .year(),
    2019,
);
assert_eq!(
    Date::try_from_ymd(2019, 12, 31)
        .unwrap()
        .try_with_hms(23, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .to_offset(UtcOffset::hours(1))
        .year(),
    2020,
);
assert_eq!(
    Date::try_from_ymd(2020, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .month(),
    1,
);
assert_eq!(
    Date::try_from_ymd(2019, 12, 31)
        .unwrap()
        .try_with_hms(23, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::hours(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .day(),
    1,
);
assert_eq!(
    Date::try_from_ymd(2019, 12, 31)
        .unwrap()
        .try_with_hms(23, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::hours(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .month_day(),
    (1, 1),
);
assert_eq!(
    Date::try_from_ymd(2019, 12, 31)
        .unwrap()
        .try_with_hms(23, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::hours(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .ordinal(),
    1,
);
assert_eq!(
    Date::try_from_ymd(2019, 12, 31)
        .unwrap()
        .try_with_hms(23, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::hours(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .iso_year_week(),
    (2019, 1),
);
assert_eq!(
    Date::try_from_ymd(2019, 10, 4)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .iso_year_week(),
    (2019, 40),
);
assert_eq!(
    Date::try_from_ymd(2020, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .iso_year_week(),
    (2020, 1),
);
assert_eq!(
    Date::try_from_ymd(2020, 12, 31)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .iso_year_week(),
    (2020, 53),
);
assert_eq!(
    Date::try_from_ymd(2021, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .week(),
    1,
);
assert_eq!(
    Date::try_from_ymd(2020, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .week(),
    1,
);
assert_eq!(
    Date::try_from_ymd(2020, 12, 31)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .week(),
    53,
);
assert_eq!(
    Date::try_from_ymd(2021, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .weekday(),
    Tuesday,
);
assert_eq!(
    Date::try_from_ymd(2019, 2, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::UTC)
        .weekday(),
    Friday,
);
assert_eq!(
    Date::try_from_ymd(2019, 3, 1)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .hour(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(23, 59, 59)
        .unwrap()
        .using_offset(UtcOffset::hours(-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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .minute(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(23, 59, 59)
        .unwrap()
        .using_offset(UtcOffset::minutes(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .second(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms(23, 59, 59)
        .unwrap()
        .using_offset(UtcOffset::seconds(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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_milli(0, 0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .millisecond(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_milli(23, 59, 59, 999)
        .unwrap()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_micro(0, 0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .microsecond(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_micro(23, 59, 59, 999_999)
        .unwrap()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_nano(0, 0, 0, 0)
        .unwrap()
        .using_offset(UtcOffset::UTC)
        .nanosecond(),
    0,
);
assert_eq!(
    Date::try_from_ymd(2019, 1, 1)
        .unwrap()
        .try_with_hms_nano(23, 59, 59, 999_999_999)
        .unwrap()
        .using_offset(UtcOffset::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::try_from_ymd(2019, 1, 2)
        .unwrap()
        .midnight()
        .using_offset(UtcOffset::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!(
    PrimitiveDateTime::parse("2019-01-02 00:00:00", "%F %T"),
    Ok(Date::try_from_ymd(2019, 1, 2).unwrap().midnight()),
);
assert_eq!(
    PrimitiveDateTime::parse("2019-002 23:59:59", "%Y-%j %T"),
    Ok(Date::try_from_yo(2019, 2)
        .unwrap()
        .try_with_hms(23, 59, 59)
        .unwrap())
);
assert_eq!(
    PrimitiveDateTime::parse("2019-W01-3 12:00:00 pm", "%G-W%V-%u %r"),
    Ok(Date::try_from_iso_ywd(2019, 1, Wednesday)
        .unwrap()
        .try_with_hms(12, 0, 0)
        .unwrap()),
);

Trait Implementations

impl Add<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the + operator.

impl Add<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the + operator.

impl AddAssign<Duration> for OffsetDateTime[src]

impl AddAssign<Duration> for OffsetDateTime[src]

impl Clone for OffsetDateTime[src]

impl Copy for OffsetDateTime[src]

impl Debug for OffsetDateTime[src]

impl Eq for OffsetDateTime[src]

impl Hash for OffsetDateTime[src]

impl Ord for OffsetDateTime[src]

impl PartialEq<OffsetDateTime> for OffsetDateTime[src]

impl PartialOrd<OffsetDateTime> for OffsetDateTime[src]

impl StructuralEq for OffsetDateTime[src]

impl Sub<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the - operator.

impl Sub<Duration> for OffsetDateTime[src]

type Output = Self

The resulting type after applying the - operator.

impl Sub<OffsetDateTime> for OffsetDateTime[src]

type Output = Duration

The resulting type after applying the - operator.

impl SubAssign<Duration> for OffsetDateTime[src]

impl SubAssign<Duration> for OffsetDateTime[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

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

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

type Owned = T

The resulting type after obtaining ownership.

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.

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.