Skip to content

Commit

Permalink
fix: truncate milliseconds to i64 before serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDowson committed Jan 9, 2025
1 parent 0a50218 commit f97f52f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions time/src/serde/timestamp/milliseconds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ pub fn serialize<S: Serializer>(
datetime: &OffsetDateTime,
serializer: S,
) -> Result<S::Ok, S::Error> {
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<OffsetDateTime, D::Error> {
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))
}

Expand Down

0 comments on commit f97f52f

Please sign in to comment.