From f97f52fbcf86adf850f2ad0dc766e04c94472be7 Mon Sep 17 00:00:00 2001 From: HJVT Date: Thu, 9 Jan 2025 23:22:27 +0300 Subject: [PATCH] fix: truncate milliseconds to i64 before serializing --- time/src/serde/timestamp/milliseconds.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/time/src/serde/timestamp/milliseconds.rs b/time/src/serde/timestamp/milliseconds.rs index e571b6c9e..f00333545 100644 --- a/time/src/serde/timestamp/milliseconds.rs +++ b/time/src/serde/timestamp/milliseconds.rs @@ -17,14 +17,15 @@ pub fn serialize( datetime: &OffsetDateTime, serializer: S, ) -> Result { - let timestamp = datetime.unix_timestamp_nanos() / 1_000_000; + // Truncating should be fine until year 2262 + let timestamp = (datetime.unix_timestamp_nanos() / 1_000_000) as i64; timestamp.serialize(serializer) } /// Deserialize an `OffsetDateTime` from its Unix timestamp with milliseconds pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result { - let value: i128 = <_>::deserialize(deserializer)?; - OffsetDateTime::from_unix_timestamp_nanos(value * 1_000_000) + let value: i64 = <_>::deserialize(deserializer)?; + OffsetDateTime::from_unix_timestamp_nanos((value as i128) * 1_000_000) .map_err(|err| de::Error::invalid_value(de::Unexpected::Signed(err.value), &err)) }