Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test wasm-bindgen feature on CI and fix implementation #1131

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 6 additions & 14 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ jobs:
os: [ubuntu-latest]
target:
[
wasm32-unknown-unknown,
wasm32-wasi,
wasm32-unknown-emscripten,
aarch64-apple-ios,
Expand All @@ -130,19 +129,11 @@ jobs:
with:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-node@v3
with:
node-version: "12"
- run: |
set -euxo pipefail
export RUST_BACKTRACE=1
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf \
| bash --noprofile --norc
wasm-pack --version
shell: bash
- run: cargo build --target ${{ matrix.target }} --color=always

features_check_wasm:
test_wasm:
strategy:
matrix:
os: [ubuntu-latest]
Expand All @@ -154,10 +145,11 @@ jobs:
targets: wasm32-unknown-unknown
- uses: taiki-e/install-action@cargo-hack
- uses: Swatinem/rust-cache@v2
- run: |
cargo hack check --feature-powerset --optional-deps serde,rkyv \
--skip default --skip __internal_bench --skip __doctest \
--skip iana-time-zone --skip pure-rust-locales
- uses: actions/setup-node@v3
- uses: jetli/wasm-pack-action@v0.4.0
# The `TZ` and `NOW` variables are used to compare the results inside the WASM environment
# with the host system.
- run: TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set TZ and NOW here? Maybe add a comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment


cross-targets:
strategy:
Expand Down
51 changes: 23 additions & 28 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,34 @@ mod inner {
not(any(target_os = "emscripten", target_os = "wasi"))
))]
mod inner {
use crate::{FixedOffset, LocalResult, NaiveDateTime};
use crate::{Datelike, FixedOffset, LocalResult, NaiveDateTime, Timelike};

pub(super) fn offset_from_utc_datetime(_utc: &NaiveDateTime) -> LocalResult<FixedOffset> {
let offset = js_sys::Date::new_0().get_timezone_offset();
pub(super) fn offset_from_utc_datetime(utc: &NaiveDateTime) -> LocalResult<FixedOffset> {
let offset = js_sys::Date::from(utc.and_utc()).get_timezone_offset();
LocalResult::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap())
}

pub(super) fn offset_from_local_datetime(local: &NaiveDateTime) -> LocalResult<FixedOffset> {
offset_from_utc_datetime(local)
let mut year = local.year();
if year < 100 {
// The API in `js_sys` does not let us create a `Date` with negative years.
// And values for years from `0` to `99` map to the years `1900` to `1999`.
// Shift the value by a multiple of 400 years until it is `>= 100`.
let shift_cycles = (year - 100).div_euclid(400);
year -= shift_cycles * 400;
}
let js_date = js_sys::Date::new_with_year_month_day_hr_min_sec(
year as u32,
local.month0() as i32,
local.day() as i32,
local.hour() as i32,
local.minute() as i32,
local.second() as i32,
// ignore milliseconds, our representation of leap seconds may be problematic
);
let offset = js_date.get_timezone_offset();
// We always get a result, even if this time does not exist or is ambiguous.
LocalResult::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap())
}
}

Expand Down Expand Up @@ -95,33 +114,9 @@ impl Local {
}

/// Returns a `DateTime` which corresponds to the current date and time.
#[cfg(not(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
#[must_use]
pub fn now() -> DateTime<Local> {
Utc::now().with_timezone(&Local)
}

/// Returns a `DateTime` which corresponds to the current date and time.
#[cfg(all(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When/how did this become unnecessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The platform-specific code still lives in Utc::now. This could have been part of #1072.
At the time of #331 Local::now depended on time::now.

target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]
#[must_use]
pub fn now() -> DateTime<Local> {
use super::Utc;
let now: DateTime<Utc> = super::Utc::now();

// Workaround missing timezone logic in `time` crate
let offset =
FixedOffset::west_opt((js_sys::Date::new_0().get_timezone_offset() as i32) * 60)
.unwrap();
DateTime::from_utc(now.naive_utc(), offset)
}
}

impl TimeZone for Local {
Expand Down
15 changes: 11 additions & 4 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
//! Run this test with:
//! `env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind`
//!
//! The `TZ` and `NOW` variables are used to compare the results inside the WASM environment with
//! the host system.
//! The check will fail if the local timezone does not match one of the timezones defined below.

#![cfg(all(
target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi"))
))]

use self::chrono::prelude::*;
use self::wasm_bindgen_test::*;
use chrono::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn now() {
Expand Down Expand Up @@ -52,7 +59,7 @@ fn from_is_exact() {

let dt = DateTime::<Utc>::from(now.clone());

assert_eq!(now.get_time() as i64, dt.timestamp_millis_opt().unwrap());
assert_eq!(now.get_time() as i64, dt.timestamp_millis());
}

#[wasm_bindgen_test]
Expand All @@ -72,7 +79,7 @@ fn convert_all_parts_with_milliseconds() {
let js_date = js_sys::Date::from(time);

assert_eq!(js_date.get_utc_full_year(), 2020);
assert_eq!(js_date.get_utc_month(), 12);
assert_eq!(js_date.get_utc_month(), 11); // months are numbered 0..=11
assert_eq!(js_date.get_utc_date(), 1);
assert_eq!(js_date.get_utc_hours(), 3);
assert_eq!(js_date.get_utc_minutes(), 1);
Expand Down