Debian Codegraph @

debian/missing-sources/time/src/interop/js_sys_date_utcdatetime.rs

raw
1 use crate::UtcDateTime;
2 use crate::convert::*;
3
4 impl From<js_sys::Date> for UtcDateTime {
5 /// # Panics
6 ///
7 /// This may panic if the timestamp can not be represented.
8 #[track_caller]
9 fn from(js_date: js_sys::Date) -> Self {
10 // get_time() returns milliseconds
11 let timestamp_nanos = (js_date.get_time() * Nanosecond::per_t::<f64>(Millisecond)) as i128;
12 Self::from_unix_timestamp_nanos(timestamp_nanos)
13 .expect("invalid timestamp: Timestamp cannot fit in range")
14 }
15 }
16
17 impl From<UtcDateTime> for js_sys::Date {
18 fn from(datetime: UtcDateTime) -> Self {
19 // new Date() takes milliseconds
20 let timestamp =
21 (datetime.unix_timestamp_nanos() / Nanosecond::per_t::<i128>(Millisecond)) as f64;
22 Self::new(&timestamp.into())
23 }
24 }