use core::cmp::Ordering;
#[cfg(feature = "std")]
use core::convert::From;
use core::fmt;
use core::hash::Hash;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use core::time::Duration as StdDuration;
#[cfg(feature = "formatting")]
use std::io;
#[cfg(feature = "std")]
use std::time::SystemTime;
use deranged::RangedI64;
use num_conv::prelude::*;
use powerfmt::ext::FormatterExt as _;
use powerfmt::smart_display::{self, FormatterOptions, Metadata, SmartDisplay};
use time_core::convert::*;
use crate::date::{MAX_YEAR, MIN_YEAR};
#[cfg(feature = "formatting")]
use crate::formatting::Formattable;
use crate::internal_macros::{
cascade, const_try, const_try_opt, div_floor, ensure_ranged, expect_opt,
};
#[cfg(feature = "parsing")]
use crate::parsing::Parsable;
#[cfg(feature = "parsing")]
use crate::util;
use crate::{error, Date, Duration, Month, PrimitiveDateTime, Time, UtcOffset, Weekday};
#[allow(clippy::undocumented_unsafe_blocks)]
const UNIX_EPOCH_JULIAN_DAY: i32 =
unsafe { Date::__from_ordinal_date_unchecked(1970, 1) }.to_julian_day();
#[derive(Clone, Copy, Eq)]
pub struct OffsetDateTime {
local_date_time: PrimitiveDateTime,
offset: UtcOffset,
}
impl PartialEq for OffsetDateTime {
fn eq(&self, other: &Self) -> bool {
self.to_offset_raw(UtcOffset::UTC) == other.to_offset_raw(UtcOffset::UTC)
}
}
impl PartialOrd for OffsetDateTime {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for OffsetDateTime {
fn cmp(&self, other: &Self) -> Ordering {
self.to_offset_raw(UtcOffset::UTC)
.cmp(&other.to_offset_raw(UtcOffset::UTC))
}
}
impl Hash for OffsetDateTime {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.to_offset_raw(UtcOffset::UTC).hash(state);
}
}
impl OffsetDateTime {
pub const UNIX_EPOCH: Self = Self::new_in_offset(
unsafe { Date::__from_ordinal_date_unchecked(1970, 1) },
Time::MIDNIGHT,
UtcOffset::UTC,
);
#[cfg(feature = "std")]
pub fn now_utc() -> Self {
#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
{
js_sys::Date::new_0().into()
}
#[cfg(not(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
)))]
SystemTime::now().into()
}
#[cfg(feature = "local-offset")]
pub fn now_local() -> Result<Self, error::IndeterminateOffset> {
let t = Self::now_utc();
Ok(t.to_offset(UtcOffset::local_offset_at(t)?))
}
pub const fn new_in_offset(date: Date, time: Time, offset: UtcOffset) -> Self {
Self {
local_date_time: date.with_time(time),
offset,
}
}
pub const fn new_utc(date: Date, time: Time) -> Self {
PrimitiveDateTime::new(date, time).assume_utc()
}
pub const fn to_offset(self, offset: UtcOffset) -> Self {
expect_opt!(
self.checked_to_offset(offset),
"local datetime out of valid range"
)
}
pub const fn checked_to_offset(self, offset: UtcOffset) -> Option<Self> {
if self.offset.whole_hours() == offset.whole_hours()
&& self.offset.minutes_past_hour() == offset.minutes_past_hour()
&& self.offset.seconds_past_minute() == offset.seconds_past_minute()
{
return Some(self.replace_offset(offset));
}
let (year, ordinal, time) = self.to_offset_raw(offset);
if year > MAX_YEAR || year < MIN_YEAR {
return None;
}
Some(Self::new_in_offset(
unsafe { Date::__from_ordinal_date_unchecked(year, ordinal) },
time,
offset,
))
}
pub(crate) const fn to_offset_raw(self, offset: UtcOffset) -> (i32, u16, Time) {
let from = self.offset;
let to = offset;
if from.whole_hours() == to.whole_hours()
&& from.minutes_past_hour() == to.minutes_past_hour()
&& from.seconds_past_minute() == to.seconds_past_minute()
{
return (self.year(), self.ordinal(), self.time());
}
let mut second = self.second() as i16 - from.seconds_past_minute() as i16
+ to.seconds_past_minute() as i16;
let mut minute =
self.minute() as i16 - from.minutes_past_hour() as i16 + to.minutes_past_hour() as i16;
let mut hour = self.hour() as i8 - from.whole_hours() + to.whole_hours();
let (mut year, ordinal) = self.to_ordinal_date();
let mut ordinal = ordinal as i16;
cascade!(second in 0..Second::per(Minute) as i16 => minute);
cascade!(second in 0..Second::per(Minute) as i16 => minute);
cascade!(minute in 0..Minute::per(Hour) as i16 => hour);
cascade!(minute in 0..Minute::per(Hour) as i16 => hour);
cascade!(hour in 0..Hour::per(Day) as i8 => ordinal);
cascade!(hour in 0..Hour::per(Day) as i8 => ordinal);
cascade!(ordinal => year);
debug_assert!(ordinal > 0);
debug_assert!(ordinal <= crate::util::days_in_year(year) as i16);
(
year,
ordinal as _,
unsafe {
Time::__from_hms_nanos_unchecked(
hour as _,
minute as _,
second as _,
self.nanosecond(),
)
},
)
}
pub const fn from_unix_timestamp(timestamp: i64) -> Result<Self, error::ComponentRange> {
type Timestamp = RangedI64<
{
OffsetDateTime::new_in_offset(Date::MIN, Time::MIDNIGHT, UtcOffset::UTC)
.unix_timestamp()
},
{
OffsetDateTime::new_in_offset(Date::MAX, Time::MAX, UtcOffset::UTC).unix_timestamp()
},
>;
ensure_ranged!(Timestamp: timestamp);
let date = Date::from_julian_day_unchecked(
UNIX_EPOCH_JULIAN_DAY + div_floor!(timestamp, Second::per(Day) as i64) as i32,
);
let seconds_within_day = timestamp.rem_euclid(Second::per(Day) as _);
let time = unsafe {
Time::__from_hms_nanos_unchecked(
(seconds_within_day / Second::per(Hour) as i64) as _,
((seconds_within_day % Second::per(Hour) as i64) / Minute::per(Hour) as i64) as _,
(seconds_within_day % Second::per(Minute) as i64) as _,
0,
)
};
Ok(Self::new_in_offset(date, time, UtcOffset::UTC))
}
pub const fn from_unix_timestamp_nanos(timestamp: i128) -> Result<Self, error::ComponentRange> {
let datetime = const_try!(Self::from_unix_timestamp(div_floor!(
timestamp,
Nanosecond::per(Second) as i128
) as i64));
Ok(Self::new_in_offset(
datetime.date(),
unsafe {
Time::__from_hms_nanos_unchecked(
datetime.hour(),
datetime.minute(),
datetime.second(),
timestamp.rem_euclid(Nanosecond::per(Second) as _) as u32,
)
},
UtcOffset::UTC,
))
}
pub const fn offset(self) -> UtcOffset {
self.offset
}
pub const fn unix_timestamp(self) -> i64 {
let days =
(self.to_julian_day() as i64 - UNIX_EPOCH_JULIAN_DAY as i64) * Second::per(Day) as i64;
let hours = self.hour() as i64 * Second::per(Hour) as i64;
let minutes = self.minute() as i64 * Second::per(Minute) as i64;
let seconds = self.second() as i64;
let offset_seconds = self.offset.whole_seconds() as i64;
days + hours + minutes + seconds - offset_seconds
}
pub const fn unix_timestamp_nanos(self) -> i128 {
self.unix_timestamp() as i128 * Nanosecond::per(Second) as i128 + self.nanosecond() as i128
}
const fn date_time(self) -> PrimitiveDateTime {
self.local_date_time
}
pub const fn date(self) -> Date {
self.date_time().date()
}
pub const fn time(self) -> Time {
self.date_time().time()
}
pub const fn year(self) -> i32 {
self.date().year()
}
pub const fn month(self) -> Month {
self.date().month()
}
pub const fn day(self) -> u8 {
self.date().day()
}
pub const fn ordinal(self) -> u16 {
self.date().ordinal()
}
pub const fn iso_week(self) -> u8 {
self.date().iso_week()
}
pub const fn sunday_based_week(self) -> u8 {
self.date().sunday_based_week()
}
pub const fn monday_based_week(self) -> u8 {
self.date().monday_based_week()
}
pub const fn to_calendar_date(self) -> (i32, Month, u8) {
self.date().to_calendar_date()
}
pub const fn to_ordinal_date(self) -> (i32, u16) {
self.date().to_ordinal_date()
}
pub const fn to_iso_week_date(self) -> (i32, u8, Weekday) {
self.date().to_iso_week_date()
}
pub const fn weekday(self) -> Weekday {
self.date().weekday()
}
pub const fn to_julian_day(self) -> i32 {
self.date().to_julian_day()
}
pub const fn to_hms(self) -> (u8, u8, u8) {
self.time().as_hms()
}
pub const fn to_hms_milli(self) -> (u8, u8, u8, u16) {
self.time().as_hms_milli()
}
pub const fn to_hms_micro(self) -> (u8, u8, u8, u32) {
self.time().as_hms_micro()
}
pub const fn to_hms_nano(self) -> (u8, u8, u8, u32) {
self.time().as_hms_nano()
}
pub const fn hour(self) -> u8 {
self.time().hour()
}
pub const fn minute(self) -> u8 {
self.time().minute()
}
pub const fn second(self) -> u8 {
self.time().second()
}
pub const fn millisecond(self) -> u16 {
self.time().millisecond()
}
pub const fn microsecond(self) -> u32 {
self.time().microsecond()
}
pub const fn nanosecond(self) -> u32 {
self.time().nanosecond()
}
pub const fn checked_add(self, duration: Duration) -> Option<Self> {
Some(const_try_opt!(self.date_time().checked_add(duration)).assume_offset(self.offset()))
}
pub const fn checked_sub(self, duration: Duration) -> Option<Self> {
Some(const_try_opt!(self.date_time().checked_sub(duration)).assume_offset(self.offset()))
}
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(-999999-01-01 0:00 +10).saturating_add((-2).days()),"
)]
#[cfg_attr(feature = "large-dates", doc = " datetime!(-999999-01-01 0:00 +10)")]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(-9999-01-01 0:00 +10).saturating_add((-2).days()),"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(-9999-01-01 0:00 +10)"
)]
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(+999999-12-31 23:59:59.999_999_999 +10).saturating_add(2.days()),"
)]
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(+999999-12-31 23:59:59.999_999_999 +10)"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(+9999-12-31 23:59:59.999_999_999 +10).saturating_add(2.days()),"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(+9999-12-31 23:59:59.999_999_999 +10)"
)]
pub const fn saturating_add(self, duration: Duration) -> Self {
if let Some(datetime) = self.checked_add(duration) {
datetime
} else if duration.is_negative() {
PrimitiveDateTime::MIN.assume_offset(self.offset())
} else {
PrimitiveDateTime::MAX.assume_offset(self.offset())
}
}
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(-999999-01-01 0:00 +10).saturating_sub(2.days()),"
)]
#[cfg_attr(feature = "large-dates", doc = " datetime!(-999999-01-01 0:00 +10)")]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(-9999-01-01 0:00 +10).saturating_sub(2.days()),"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(-9999-01-01 0:00 +10)"
)]
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(+999999-12-31 23:59:59.999_999_999 +10).saturating_sub((-2).days()),"
)]
#[cfg_attr(
feature = "large-dates",
doc = " datetime!(+999999-12-31 23:59:59.999_999_999 +10)"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(+9999-12-31 23:59:59.999_999_999 +10).saturating_sub((-2).days()),"
)]
#[cfg_attr(
not(feature = "large-dates"),
doc = " datetime!(+9999-12-31 23:59:59.999_999_999 +10)"
)]
pub const fn saturating_sub(self, duration: Duration) -> Self {
if let Some(datetime) = self.checked_sub(duration) {
datetime
} else if duration.is_negative() {
PrimitiveDateTime::MAX.assume_offset(self.offset())
} else {
PrimitiveDateTime::MIN.assume_offset(self.offset())
}
}
}
impl OffsetDateTime {
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_time(self, time: Time) -> Self {
Self::new_in_offset(self.date(), time, self.offset())
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_date(self, date: Date) -> Self {
Self::new_in_offset(date, self.time(), self.offset())
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_date_time(self, date_time: PrimitiveDateTime) -> Self {
date_time.assume_offset(self.offset())
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_offset(self, offset: UtcOffset) -> Self {
self.date_time().assume_offset(offset)
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_year(self, year: i32) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_year(year)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_month(self, month: Month) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_month(month)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_day(day)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_ordinal(self, ordinal: u16) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_ordinal(ordinal)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_hour(self, hour: u8) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_hour(hour)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_minute(self, minute: u8) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_minute(minute)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_second(self, second: u8) -> Result<Self, error::ComponentRange> {
Ok(const_try!(self.date_time().replace_second(second)).assume_offset(self.offset()))
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_millisecond(
self,
millisecond: u16,
) -> Result<Self, error::ComponentRange> {
Ok(
const_try!(self.date_time().replace_millisecond(millisecond))
.assume_offset(self.offset()),
)
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_microsecond(
self,
microsecond: u32,
) -> Result<Self, error::ComponentRange> {
Ok(
const_try!(self.date_time().replace_microsecond(microsecond))
.assume_offset(self.offset()),
)
}
#[must_use = "This method does not mutate the original `OffsetDateTime`."]
pub const fn replace_nanosecond(self, nanosecond: u32) -> Result<Self, error::ComponentRange> {
Ok(
const_try!(self.date_time().replace_nanosecond(nanosecond))
.assume_offset(self.offset()),
)
}
}
#[cfg(feature = "formatting")]
impl OffsetDateTime {
pub fn format_into(
self,
output: &mut impl io::Write,
format: &(impl Formattable + ?Sized),
) -> Result<usize, error::Format> {
format.format_into(
output,
Some(self.date()),
Some(self.time()),
Some(self.offset()),
)
}
pub fn format(self, format: &(impl Formattable + ?Sized)) -> Result<String, error::Format> {
format.format(Some(self.date()), Some(self.time()), Some(self.offset()))
}
}
#[cfg(feature = "parsing")]
impl OffsetDateTime {
pub fn parse(
input: &str,
description: &(impl Parsable + ?Sized),
) -> Result<Self, error::Parse> {
description.parse_offset_date_time(input.as_bytes())
}
#[cfg(feature = "parsing")]
pub(crate) const fn is_valid_leap_second_stand_in(self) -> bool {
if self.nanosecond() != 999_999_999 {
return false;
}
let (year, ordinal, time) = self.to_offset_raw(UtcOffset::UTC);
let Ok(date) = Date::from_ordinal_date(year, ordinal) else {
return false;
};
time.hour() == 23
&& time.minute() == 59
&& time.second() == 59
&& date.day() == util::days_in_year_month(year, date.month())
}
}
impl SmartDisplay for OffsetDateTime {
type Metadata = ();
fn metadata(&self, _: FormatterOptions) -> Metadata<Self> {
let width =
smart_display::padded_width_of!(self.date(), " ", self.time(), " ", self.offset());
Metadata::new(width, self, ())
}
fn fmt_with_metadata(
&self,
f: &mut fmt::Formatter<'_>,
metadata: Metadata<Self>,
) -> fmt::Result {
f.pad_with_width(
metadata.unpadded_width(),
format_args!("{} {} {}", self.date(), self.time(), self.offset()),
)
}
}
impl fmt::Display for OffsetDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
SmartDisplay::fmt(self, f)
}
}
impl fmt::Debug for OffsetDateTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl Add<Duration> for OffsetDateTime {
type Output = Self;
fn add(self, duration: Duration) -> Self::Output {
self.checked_add(duration)
.expect("resulting value is out of range")
}
}
impl Add<StdDuration> for OffsetDateTime {
type Output = Self;
fn add(self, duration: StdDuration) -> Self::Output {
let (is_next_day, time) = self.time().adjusting_add_std(duration);
Self::new_in_offset(
if is_next_day {
(self.date() + duration)
.next_day()
.expect("resulting value is out of range")
} else {
self.date() + duration
},
time,
self.offset,
)
}
}
impl AddAssign<Duration> for OffsetDateTime {
fn add_assign(&mut self, rhs: Duration) {
*self = *self + rhs;
}
}
impl AddAssign<StdDuration> for OffsetDateTime {
fn add_assign(&mut self, rhs: StdDuration) {
*self = *self + rhs;
}
}
impl Sub<Duration> for OffsetDateTime {
type Output = Self;
fn sub(self, rhs: Duration) -> Self::Output {
self.checked_sub(rhs)
.expect("resulting value is out of range")
}
}
impl Sub<StdDuration> for OffsetDateTime {
type Output = Self;
fn sub(self, duration: StdDuration) -> Self::Output {
let (is_previous_day, time) = self.time().adjusting_sub_std(duration);
Self::new_in_offset(
if is_previous_day {
(self.date() - duration)
.previous_day()
.expect("resulting value is out of range")
} else {
self.date() - duration
},
time,
self.offset,
)
}
}
impl SubAssign<Duration> for OffsetDateTime {
fn sub_assign(&mut self, rhs: Duration) {
*self = *self - rhs;
}
}
impl SubAssign<StdDuration> for OffsetDateTime {
fn sub_assign(&mut self, rhs: StdDuration) {
*self = *self - rhs;
}
}
impl Sub for OffsetDateTime {
type Output = Duration;
fn sub(self, rhs: Self) -> Self::Output {
let base = self.date_time() - rhs.date_time();
let adjustment = Duration::seconds(
(self.offset.whole_seconds() - rhs.offset.whole_seconds()).extend::<i64>(),
);
base - adjustment
}
}
#[cfg(feature = "std")]
impl Sub<SystemTime> for OffsetDateTime {
type Output = Duration;
fn sub(self, rhs: SystemTime) -> Self::Output {
self - Self::from(rhs)
}
}
#[cfg(feature = "std")]
impl Sub<OffsetDateTime> for SystemTime {
type Output = Duration;
fn sub(self, rhs: OffsetDateTime) -> Self::Output {
OffsetDateTime::from(self) - rhs
}
}
#[cfg(feature = "std")]
impl Add<Duration> for SystemTime {
type Output = Self;
fn add(self, duration: Duration) -> Self::Output {
if duration.is_zero() {
self
} else if duration.is_positive() {
self + duration.unsigned_abs()
} else {
debug_assert!(duration.is_negative());
self - duration.unsigned_abs()
}
}
}
crate::internal_macros::impl_add_assign!(SystemTime: #[cfg(feature = "std")] Duration);
#[cfg(feature = "std")]
impl Sub<Duration> for SystemTime {
type Output = Self;
fn sub(self, duration: Duration) -> Self::Output {
(OffsetDateTime::from(self) - duration).into()
}
}
crate::internal_macros::impl_sub_assign!(SystemTime: #[cfg(feature = "std")] Duration);
#[cfg(feature = "std")]
impl PartialEq<SystemTime> for OffsetDateTime {
fn eq(&self, rhs: &SystemTime) -> bool {
self == &Self::from(*rhs)
}
}
#[cfg(feature = "std")]
impl PartialEq<OffsetDateTime> for SystemTime {
fn eq(&self, rhs: &OffsetDateTime) -> bool {
&OffsetDateTime::from(*self) == rhs
}
}
#[cfg(feature = "std")]
impl PartialOrd<SystemTime> for OffsetDateTime {
fn partial_cmp(&self, other: &SystemTime) -> Option<Ordering> {
self.partial_cmp(&Self::from(*other))
}
}
#[cfg(feature = "std")]
impl PartialOrd<OffsetDateTime> for SystemTime {
fn partial_cmp(&self, other: &OffsetDateTime) -> Option<Ordering> {
OffsetDateTime::from(*self).partial_cmp(other)
}
}
#[cfg(feature = "std")]
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(),
}
}
}
#[cfg(feature = "std")]
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()
}
}
}
#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
impl From<js_sys::Date> for OffsetDateTime {
fn from(js_date: js_sys::Date) -> Self {
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")
}
}
#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm-bindgen"
))]
impl From<OffsetDateTime> for js_sys::Date {
fn from(datetime: OffsetDateTime) -> Self {
let timestamp = (datetime.unix_timestamp_nanos()
/ Nanosecond::per(Millisecond).cast_signed().extend::<i128>())
as f64;
Self::new(×tamp.into())
}
}