Skip to main content

icu_time/
time_crate.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use icu_calendar::Gregorian;
6
7use crate::zone::UtcOffset;
8use crate::ZonedDateTime;
9use crate::{DateTime, Hour, Minute, Nanosecond, Second, Time};
10
11impl From<time::Time> for Time {
12    fn from(time: time::Time) -> Self {
13        Time {
14            hour: Hour(time.hour()),
15            minute: Minute(time.minute()),
16            second: Second(time.second()),
17            subsecond: Nanosecond(time.nanosecond()),
18        }
19    }
20}
21
22impl From<time::PrimitiveDateTime> for DateTime<Gregorian> {
23    fn from(chrono: time::PrimitiveDateTime) -> Self {
24        Self {
25            date: chrono.date().into(),
26            time: chrono.time().into(),
27        }
28    }
29}
30
31impl From<time::UtcOffset> for UtcOffset {
32    fn from(other: time::UtcOffset) -> Self {
33        UtcOffset::from_seconds_unchecked(other.whole_seconds())
34    }
35}
36
37impl From<&time::OffsetDateTime> for ZonedDateTime<Gregorian, UtcOffset> {
38    fn from(other: &time::OffsetDateTime) -> Self {
39        let date = other.date().into();
40        let time = other.time().into();
41        let zone = other.offset().into();
42
43        ZonedDateTime { date, time, zone }
44    }
45}