diff options
Diffstat (limited to 'vendor/time/src/interop')
| -rw-r--r-- | vendor/time/src/interop/js_sys_date_offsetdatetime.rs | 27 | ||||
| -rw-r--r-- | vendor/time/src/interop/js_sys_date_utcdatetime.rs | 26 | ||||
| -rw-r--r-- | vendor/time/src/interop/mod.rs | 28 | ||||
| -rw-r--r-- | vendor/time/src/interop/offsetdatetime_systemtime.rs | 75 | ||||
| -rw-r--r-- | vendor/time/src/interop/offsetdatetime_utcdatetime.rs | 68 | ||||
| -rw-r--r-- | vendor/time/src/interop/utcdatetime_systemtime.rs | 75 |
6 files changed, 299 insertions, 0 deletions
diff --git a/vendor/time/src/interop/js_sys_date_offsetdatetime.rs b/vendor/time/src/interop/js_sys_date_offsetdatetime.rs new file mode 100644 index 00000000..ecbd601e --- /dev/null +++ b/vendor/time/src/interop/js_sys_date_offsetdatetime.rs @@ -0,0 +1,27 @@ +use num_conv::prelude::*; + +use crate::convert::*; +use crate::OffsetDateTime; + +impl From<js_sys::Date> for OffsetDateTime { + /// # Panics + /// + /// This may panic if the timestamp can not be represented. + fn from(js_date: js_sys::Date) -> Self { + // get_time() returns milliseconds + let timestamp_nanos = js_date.get_time() as i128 + * Nanosecond::per(Millisecond).cast_signed().extend::<i128>(); + Self::from_unix_timestamp_nanos(timestamp_nanos) + .expect("invalid timestamp: Timestamp cannot fit in range") + } +} + +impl From<OffsetDateTime> for js_sys::Date { + fn from(datetime: OffsetDateTime) -> Self { + // new Date() takes milliseconds + let timestamp = (datetime.unix_timestamp_nanos() + / Nanosecond::per(Millisecond).cast_signed().extend::<i128>()) + as f64; + Self::new(×tamp.into()) + } +} diff --git a/vendor/time/src/interop/js_sys_date_utcdatetime.rs b/vendor/time/src/interop/js_sys_date_utcdatetime.rs new file mode 100644 index 00000000..e387c684 --- /dev/null +++ b/vendor/time/src/interop/js_sys_date_utcdatetime.rs @@ -0,0 +1,26 @@ +use num_conv::prelude::*; + +use crate::convert::*; +use crate::UtcDateTime; + +impl From<js_sys::Date> for UtcDateTime { + /// # Panics + /// + /// This may panic if the timestamp can not be represented. + fn from(js_date: js_sys::Date) -> Self { + // get_time() returns milliseconds + let timestamp_nanos = (js_date.get_time() * Nanosecond::per(Millisecond) as f64) as i128; + Self::from_unix_timestamp_nanos(timestamp_nanos) + .expect("invalid timestamp: Timestamp cannot fit in range") + } +} + +impl From<UtcDateTime> for js_sys::Date { + fn from(datetime: UtcDateTime) -> Self { + // new Date() takes milliseconds + let timestamp = (datetime.unix_timestamp_nanos() + / Nanosecond::per(Millisecond).cast_signed().extend::<i128>()) + as f64; + Self::new(×tamp.into()) + } +} diff --git a/vendor/time/src/interop/mod.rs b/vendor/time/src/interop/mod.rs new file mode 100644 index 00000000..d562663c --- /dev/null +++ b/vendor/time/src/interop/mod.rs @@ -0,0 +1,28 @@ +//! Comparison, arithmetic, and conversion between various types in `time` and the standard library. +//! +//! Currently, full interoperability is present between [`OffsetDateTime`](crate::OffsetDateTime), +//! [`UtcDateTime`](crate::UtcDateTime), and [`SystemTime`](std::time::SystemTime). Partial +//! interoperability is present with [`js_sys::Date`]. Note that +//! [`PrimitiveDateTime`](crate::PrimitiveDateTime) is not interoperable with any of these types due +//! to the lack of an associated UTC offset. + +// Module names should have the two types sorted in alphabetical order. This avoids any question +// of which type should be the "primary" type in the module name. + +#[cfg(all( + target_family = "wasm", + not(any(target_os = "emscripten", target_os = "wasi")), + feature = "wasm-bindgen" +))] +mod js_sys_date_offsetdatetime; +#[cfg(all( + target_family = "wasm", + not(any(target_os = "emscripten", target_os = "wasi")), + feature = "wasm-bindgen" +))] +mod js_sys_date_utcdatetime; +#[cfg(feature = "std")] +mod offsetdatetime_systemtime; +mod offsetdatetime_utcdatetime; +#[cfg(feature = "std")] +mod utcdatetime_systemtime; diff --git a/vendor/time/src/interop/offsetdatetime_systemtime.rs b/vendor/time/src/interop/offsetdatetime_systemtime.rs new file mode 100644 index 00000000..21a5d7f1 --- /dev/null +++ b/vendor/time/src/interop/offsetdatetime_systemtime.rs @@ -0,0 +1,75 @@ +use core::cmp::Ordering; +use core::ops::Sub; +use std::time::SystemTime; + +use crate::{Duration, OffsetDateTime}; + +impl Sub<SystemTime> for OffsetDateTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: SystemTime) -> Self::Output { + self - Self::from(rhs) + } +} + +impl Sub<OffsetDateTime> for SystemTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: OffsetDateTime) -> Self::Output { + OffsetDateTime::from(self) - rhs + } +} + +impl PartialEq<SystemTime> for OffsetDateTime { + fn eq(&self, rhs: &SystemTime) -> bool { + self == &Self::from(*rhs) + } +} + +impl PartialEq<OffsetDateTime> for SystemTime { + fn eq(&self, rhs: &OffsetDateTime) -> bool { + &OffsetDateTime::from(*self) == rhs + } +} + +impl PartialOrd<SystemTime> for OffsetDateTime { + fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> { + self.partial_cmp(&Self::from(*other)) + } +} + +impl PartialOrd<OffsetDateTime> for SystemTime { + fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> { + OffsetDateTime::from(*self).partial_cmp(other) + } +} + +impl From<SystemTime> for OffsetDateTime { + fn from(system_time: SystemTime) -> Self { + match system_time.duration_since(SystemTime::UNIX_EPOCH) { + Ok(duration) => Self::UNIX_EPOCH + duration, + Err(err) => Self::UNIX_EPOCH - err.duration(), + } + } +} + +impl From<OffsetDateTime> for SystemTime { + fn from(datetime: OffsetDateTime) -> Self { + let duration = datetime - OffsetDateTime::UNIX_EPOCH; + + if duration.is_zero() { + Self::UNIX_EPOCH + } else if duration.is_positive() { + Self::UNIX_EPOCH + duration.unsigned_abs() + } else { + debug_assert!(duration.is_negative()); + Self::UNIX_EPOCH - duration.unsigned_abs() + } + } +} diff --git a/vendor/time/src/interop/offsetdatetime_utcdatetime.rs b/vendor/time/src/interop/offsetdatetime_utcdatetime.rs new file mode 100644 index 00000000..34a4887d --- /dev/null +++ b/vendor/time/src/interop/offsetdatetime_utcdatetime.rs @@ -0,0 +1,68 @@ +use core::cmp::Ordering; +use core::ops::Sub; + +use crate::{Duration, OffsetDateTime, UtcDateTime}; + +impl Sub<OffsetDateTime> for UtcDateTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: OffsetDateTime) -> Self::Output { + OffsetDateTime::from(self) - rhs + } +} + +impl Sub<UtcDateTime> for OffsetDateTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: UtcDateTime) -> Self::Output { + self - Self::from(rhs) + } +} + +impl PartialEq<OffsetDateTime> for UtcDateTime { + fn eq(&self, other: &OffsetDateTime) -> bool { + OffsetDateTime::from(*self) == *other + } +} + +impl PartialEq<UtcDateTime> for OffsetDateTime { + fn eq(&self, other: &UtcDateTime) -> bool { + *self == Self::from(*other) + } +} + +impl PartialOrd<OffsetDateTime> for UtcDateTime { + fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> { + OffsetDateTime::from(*self).partial_cmp(other) + } +} + +impl PartialOrd<UtcDateTime> for OffsetDateTime { + fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> { + self.partial_cmp(&Self::from(*other)) + } +} + +impl From<OffsetDateTime> for UtcDateTime { + /// # Panics + /// + /// This may panic if an overflow occurs. + fn from(datetime: OffsetDateTime) -> Self { + datetime.to_utc() + } +} + +impl From<UtcDateTime> for OffsetDateTime { + /// # Panics + /// + /// This may panic if an overflow occurs. + fn from(datetime: UtcDateTime) -> Self { + datetime.as_primitive().assume_utc() + } +} diff --git a/vendor/time/src/interop/utcdatetime_systemtime.rs b/vendor/time/src/interop/utcdatetime_systemtime.rs new file mode 100644 index 00000000..7cfb44e1 --- /dev/null +++ b/vendor/time/src/interop/utcdatetime_systemtime.rs @@ -0,0 +1,75 @@ +use core::cmp::Ordering; +use core::ops::Sub; +use std::time::SystemTime; + +use crate::{Duration, UtcDateTime}; + +impl Sub<SystemTime> for UtcDateTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: SystemTime) -> Self::Output { + self - Self::from(rhs) + } +} + +impl Sub<UtcDateTime> for SystemTime { + type Output = Duration; + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn sub(self, rhs: UtcDateTime) -> Self::Output { + UtcDateTime::from(self) - rhs + } +} + +impl PartialEq<SystemTime> for UtcDateTime { + fn eq(&self, rhs: &SystemTime) -> bool { + self == &Self::from(*rhs) + } +} + +impl PartialEq<UtcDateTime> for SystemTime { + fn eq(&self, rhs: &UtcDateTime) -> bool { + &UtcDateTime::from(*self) == rhs + } +} + +impl PartialOrd<SystemTime> for UtcDateTime { + fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> { + self.partial_cmp(&Self::from(*other)) + } +} + +impl PartialOrd<UtcDateTime> for SystemTime { + fn partial_cmp(&self, other: &UtcDateTime) -> Option<Ordering> { + UtcDateTime::from(*self).partial_cmp(other) + } +} + +impl From<SystemTime> for UtcDateTime { + fn from(system_time: SystemTime) -> Self { + match system_time.duration_since(SystemTime::UNIX_EPOCH) { + Ok(duration) => Self::UNIX_EPOCH + duration, + Err(err) => Self::UNIX_EPOCH - err.duration(), + } + } +} + +impl From<UtcDateTime> for SystemTime { + fn from(datetime: UtcDateTime) -> Self { + let duration = datetime - UtcDateTime::UNIX_EPOCH; + + if duration.is_zero() { + Self::UNIX_EPOCH + } else if duration.is_positive() { + Self::UNIX_EPOCH + duration.unsigned_abs() + } else { + debug_assert!(duration.is_negative()); + Self::UNIX_EPOCH - duration.unsigned_abs() + } + } +} |
