Skip to content

Cannot subtract dates before UNIX_EPOCH when targeting wasm32 #105762

@heaths

Description

@heaths

I'm using a crate that calculates the Windows epoch (1601-01-01T00:00:00Z) from the UNIX_EPOCH (1970-01-01T00:00:00Z) but it panics when targeting wasm32-unknown-unknown because of a checked_sub against u64. This appears to be because, for wasm32 (references "unsupported" sources up a level in the source tree), SystemTime is a newtype of Duration, which uses unsigned integers. On (most?) other platforms, SystemTime is a Timespec which uses signed integers; thus, any arithmetic on said fields can produce negative results, which is desirable.

Given Duration represents a span of time instead of an absolute time like SystemTime, shouldn't any SystemTime use signed integers representing absolute time?

I tried this code:

/// Returns the epoch used for Windows `FILETIME` values.
fn filetime_epoch() -> SystemTime {
    // The epoch used by CFB files is Jan 1, 1601 UTC, which we can calculate
    // from the Unix epoch constant, which is Jan 1, 1970 UTC.
    UNIX_EPOCH - Duration::from_secs(11644473600)
}

I expected a SystemTime representing 1601-01-01T00:00:00.

Instead, a panic occurred in <std::time::SystemTime as core::ops::arith::Sub<core::time::Duration>>::sub:

fn sub(self, rhs: Duration) -> Duration {
    self.checked_sub(rhs).expect("overflow when subtracting durations")
}

Meta

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6
WASM backtrace

wasm-pack or something in the toolchain seems to ignore RUST_BACKTRACE but the browser is generating it's own, which I hope is helpful:

$core::option::expect_failed::ha758e0d1c5165b94 | @ | 7516d5d……odule.wasm:0x1318da
$<std::time::SystemTime as core::ops::arith::Sub<core::time::Duration>>::sub::h47beefa3c5880621 | @ | 7516d5d……odule.wasm:0x10e7c0
$msi::internal::time::filetime_epoch::h319c0984523cb136 | @ | 7516d5d….module.wasm:0xf247e
$msi::internal::time::system_time_from_filetime::hb7b59f8afe76e03c

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions