mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 07:21:41 +00:00
Merge pull request #446 from schrieveslaach/feature/js-sys-date-improvements
Improve From<js_sys::Date> Variants for DateTime<Utc>
This commit is contained in:
commit
6be7be37fc
@ -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)
|
* 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
|
## 0.4.12
|
||||||
|
|
||||||
### New Methods and impls
|
### New Methods and impls
|
||||||
|
@ -52,7 +52,7 @@ criterion = { version = "0.3" }
|
|||||||
doc-comment = { version = "0.3" }
|
doc-comment = { version = "0.3" }
|
||||||
|
|
||||||
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
|
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
|
||||||
wasm-bindgen-test = "0.2"
|
wasm-bindgen-test = "0.3"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = ["serde"]
|
features = ["serde"]
|
||||||
|
@ -91,9 +91,8 @@ test_wasm() {
|
|||||||
|
|
||||||
test_wasm_simple() {
|
test_wasm_simple() {
|
||||||
now=$(date +%s)
|
now=$(date +%s)
|
||||||
for tz in "${TEST_TZS[@]}"; do
|
tz=$(date +%z)
|
||||||
runt env TZ="$tz" NOW="$now" wasm-pack test --node -- --features wasmbind
|
runt env TZ="$tz" NOW="$now" wasm-pack test --node -- --features wasmbind
|
||||||
done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
@ -753,22 +753,18 @@ impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
|
|||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||||
impl From<js_sys::Date> for DateTime<Utc> {
|
impl From<js_sys::Date> for DateTime<Utc> {
|
||||||
fn from(date: js_sys::Date) -> DateTime<Utc> {
|
fn from(date: js_sys::Date) -> DateTime<Utc> {
|
||||||
let time = date.get_time() as i64;
|
DateTime::<Utc>::from(&date)
|
||||||
DateTime::<Utc>::from_utc(
|
|
||||||
NaiveDateTime::from_timestamp(time / 1000, ((time % 1000) * 1_000_000) as u32),
|
|
||||||
Utc,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||||
impl From<&js_sys::Date> for DateTime<Utc> {
|
impl From<&js_sys::Date> for DateTime<Utc> {
|
||||||
fn from(date: &js_sys::Date) -> DateTime<Utc> {
|
fn from(date: &js_sys::Date) -> DateTime<Utc> {
|
||||||
let time = date.get_time() as i64;
|
let millisecs_since_unix_epoch: u64 = date.get_time() as u64;
|
||||||
DateTime::<Utc>::from_utc(
|
let secs = millisecs_since_unix_epoch / 1000;
|
||||||
NaiveDateTime::from_timestamp(time / 1000, ((time % 1000) * 1_000_000) as u32),
|
let nanos = 1_000_000 * (millisecs_since_unix_epoch % 1000);
|
||||||
Utc,
|
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"))]
|
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
|
||||||
pub fn now() -> DateTime<Utc> {
|
pub fn now() -> DateTime<Utc> {
|
||||||
let now = js_sys::Date::new_0();
|
let now = js_sys::Date::new_0();
|
||||||
let millisecs_since_unix_epoch: u64 = now.get_time() as u64;
|
DateTime::<Utc>::from(now)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,17 +12,36 @@ mod test {
|
|||||||
let local: DateTime<Local> = Local::now();
|
let local: DateTime<Local> = Local::now();
|
||||||
|
|
||||||
// Ensure time fetched is correct
|
// Ensure time fetched is correct
|
||||||
let actual = Utc.datetime_from_str(env!("NOW"), "%s").unwrap();
|
let actual = Utc.datetime_from_str(&env!("NOW"), "%s").unwrap();
|
||||||
assert!(utc - actual < chrono::Duration::minutes(5));
|
assert!(utc - actual < chrono::Duration::minutes(5));
|
||||||
|
|
||||||
|
let tz = env!("TZ");
|
||||||
|
eprintln!("testing with tz={}", tz);
|
||||||
|
|
||||||
// Ensure offset retrieved when getting local time is correct
|
// Ensure offset retrieved when getting local time is correct
|
||||||
let expected_offset = match env!("TZ") {
|
let expected_offset = match tz {
|
||||||
"ACST-9:30" => FixedOffset::east(19 * 30 * 60),
|
"ACST-9:30" => FixedOffset::east(19 * 30 * 60),
|
||||||
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
|
"Asia/Katmandu" => FixedOffset::east(23 * 15 * 60), // No DST thankfully
|
||||||
"EST4" => FixedOffset::east(-4 * 60 * 60),
|
"EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60),
|
||||||
"UTC0" => FixedOffset::east(0),
|
"EST" | "-0500" => FixedOffset::east(-5 * 60 * 60),
|
||||||
_ => panic!("unexpected TZ"),
|
"UTC0" | "+0000" => FixedOffset::east(0),
|
||||||
|
tz => panic!("unexpected TZ {}", tz),
|
||||||
};
|
};
|
||||||
assert_eq!(&expected_offset, local.offset());
|
assert_eq!(
|
||||||
|
&expected_offset,
|
||||||
|
local.offset(),
|
||||||
|
"expected: {:?} local: {:?}",
|
||||||
|
expected_offset,
|
||||||
|
local.offset(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn from_is_exact() {
|
||||||
|
let now = js_sys::Date::new_0();
|
||||||
|
|
||||||
|
let dt = DateTime::<Utc>::from(now.clone());
|
||||||
|
|
||||||
|
assert_eq!(now.get_time() as i64, dt.timestamp_millis());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user