| 1 |
use crate::OffsetDateTime; |
| 2 |
use crate::convert::*; |
| 3 |
|
| 4 |
impl From<js_sys::Date> for OffsetDateTime { |
| 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() as i128 * Nanosecond::per_t::<i128>(Millisecond); |
| 12 |
Self::from_unix_timestamp_nanos(timestamp_nanos) |
| 13 |
.expect("invalid timestamp: Timestamp cannot fit in range") |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
impl From<OffsetDateTime> for js_sys::Date { |
| 18 |
fn from(datetime: OffsetDateTime) -> Self { |
| 19 |
// new Date() takes milliseconds |
| 20 |
let timestamp = |
| 21 |
(datetime.unix_timestamp_nanos() / Nanosecond::per_t::<i128>(Millisecond)) as f64; |
| 22 |
Self::new(×tamp.into()) |
| 23 |
} |
| 24 |
} |