mirror of
https://github.com/chronotope/chrono.git
synced 2025-09-28 05:21:39 +00:00
Improve From<js_sys::Date> Variants for DateTime<Utc>
This commit is contained in:
parent
4348cd13c7
commit
545fd9f863
@ -14,6 +14,11 @@ Versions with only mechanical changes will be omitted from the following list.
|
||||
|
||||
* Add `DurationRound` trait that allows rounding and truncating by `Duration` (@robyoung)
|
||||
|
||||
### Internal Improvements
|
||||
|
||||
* Code improvements to impl `From` for `js_sys` in wasm to reuse code (@schrieveslaach)
|
||||
|
||||
|
||||
## 0.4.12
|
||||
|
||||
### New Methods and impls
|
||||
|
@ -753,22 +753,18 @@ impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
|
||||
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||
impl From<js_sys::Date> for DateTime<Utc> {
|
||||
fn from(date: js_sys::Date) -> DateTime<Utc> {
|
||||
let time = date.get_time() as i64;
|
||||
DateTime::<Utc>::from_utc(
|
||||
NaiveDateTime::from_timestamp(time / 1000, ((time % 1000) * 1_000_000) as u32),
|
||||
Utc,
|
||||
)
|
||||
DateTime::<Utc>::from(&date)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||
impl From<&js_sys::Date> for DateTime<Utc> {
|
||||
fn from(date: &js_sys::Date) -> DateTime<Utc> {
|
||||
let time = date.get_time() as i64;
|
||||
DateTime::<Utc>::from_utc(
|
||||
NaiveDateTime::from_timestamp(time / 1000, ((time % 1000) * 1_000_000) as u32),
|
||||
Utc,
|
||||
)
|
||||
let millisecs_since_unix_epoch: u64 = date.get_time() as u64;
|
||||
let secs = millisecs_since_unix_epoch / 1000;
|
||||
let nanos = 1_000_000 * (millisecs_since_unix_epoch - 1000 * secs);
|
||||
let naive = NaiveDateTime::from_timestamp(secs as i64, nanos as u32);
|
||||
DateTime::from_utc(naive, Utc)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,11 +54,7 @@ impl Utc {
|
||||
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||
pub fn now() -> DateTime<Utc> {
|
||||
let now = js_sys::Date::new_0();
|
||||
let millisecs_since_unix_epoch: u64 = now.get_time() as u64;
|
||||
let secs = millisecs_since_unix_epoch / 1000;
|
||||
let nanos = 1_000_000 * (millisecs_since_unix_epoch - 1000 * secs);
|
||||
let naive = NaiveDateTime::from_timestamp(secs as i64, nanos as u32);
|
||||
DateTime::from_utc(naive, Utc)
|
||||
DateTime::<Utc>::from(now)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user