You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When generating an OffsetDateTime from a js_sys::Date, there are cases where the sub-millisecond components are not set to zero. This behavior is unexpected and can lead to inaccuracies in the resulting OffsetDateTime.
The precision of js_sys::Date is limited to milliseconds. Therefore, when converting it to an OffsetDateTime, we expect the sub-millisecond components to be set to zero for consistency and accuracy.
let timestamp_nanos = (js_date.get_time()*Nanosecond::per(Millisecond)asf64)asi128;
Self::from_unix_timestamp_nanos(timestamp_nanos)
.expect("invalid timestamp: Timestamp cannot fit in range")
}
}
The issue stems from the conversion process of milliseconds to nanoseconds. It appears that the code converts milliseconds to nanoseconds using f64, and subsequently converts it to i128, which can introduce inaccuracies and prevent sub-millisecond components from being zeroed out.
Proposed Solution
To address this issue, consider converting milliseconds to i128 first and then perform the conversion to nanoseconds. This approach should maintain accuracy and ensure that sub-millisecond components are set to zero.
Additional Information
I encountered this issue while using OffsetDateTime::now_utc() in a WASM environment.
The text was updated successfully, but these errors were encountered:
Without even looking into this, I am confident that the multiplication magnifies the inherent error of representing decimal values in floating point numbers. I'm happy to accept your proposed solution as a PR, as I'm sure it'll fix the issue.
When generating an
OffsetDateTime
from ajs_sys::Date
, there are cases where the sub-millisecond components are not set to zero. This behavior is unexpected and can lead to inaccuracies in the resultingOffsetDateTime
.Steps to Reproduce
Reason for Considering It a Bug
The precision of
js_sys::Date
is limited to milliseconds. Therefore, when converting it to anOffsetDateTime
, we expect the sub-millisecond components to be set to zero for consistency and accuracy.Cause
time/time/src/date_time.rs
Lines 1226 to 1233 in 72f03e0
The issue stems from the conversion process of milliseconds to nanoseconds. It appears that the code converts milliseconds to nanoseconds using
f64
, and subsequently converts it toi128
, which can introduce inaccuracies and prevent sub-millisecond components from being zeroed out.Proposed Solution
To address this issue, consider converting milliseconds to i128 first and then perform the conversion to nanoseconds. This approach should maintain accuracy and ensure that sub-millisecond components are set to zero.
Additional Information
I encountered this issue while using
OffsetDateTime::now_utc()
in a WASM environment.The text was updated successfully, but these errors were encountered: