Skip to content

Commit

Permalink
Fix deserialization of negative timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 21, 2023
1 parent c9b554f commit 4f9f292
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
24 changes: 18 additions & 6 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ pub mod ts_nanoseconds {
E: de::Error,
{
serde_from(
Utc.timestamp_opt(value / 1_000_000_000, (value % 1_000_000_000) as u32),
Utc.timestamp_opt(
value.div_euclid(1_000_000_000),
(value.rem_euclid(1_000_000_000)) as u32,
),
&value,
)
}
Expand Down Expand Up @@ -480,7 +483,10 @@ pub mod ts_microseconds {
E: de::Error,
{
serde_from(
Utc.timestamp_opt(value / 1_000_000, ((value % 1_000_000) * 1_000) as u32),
Utc.timestamp_opt(
value.div_euclid(1_000_000),
(value.rem_euclid(1_000_000) * 1_000) as u32,
),
&value,
)
}
Expand Down Expand Up @@ -734,7 +740,7 @@ pub mod ts_milliseconds {
where
E: de::Error,
{
serde_from(Utc.timestamp_opt(value / 1000, ((value % 1000) * 1_000_000) as u32), &value)
serde_from(Utc.timestamp_millis_opt(value), &value)
}

/// Deserialize a timestamp in milliseconds since the epoch
Expand Down Expand Up @@ -923,8 +929,7 @@ pub mod ts_seconds {
use serde::{de, ser};

use super::{serde_from, SecondsTimestampVisitor};
use crate::offset::TimeZone;
use crate::{DateTime, Utc};
use crate::{DateTime, LocalResult, TimeZone, Utc};

/// Serialize a UTC datetime into an integer number of seconds since the epoch
///
Expand Down Expand Up @@ -1003,7 +1008,14 @@ pub mod ts_seconds {
where
E: de::Error,
{
serde_from(Utc.timestamp_opt(value as i64, 0), &value)
serde_from(
if value > i64::MAX as u64 {
LocalResult::None
} else {
Utc.timestamp_opt(value as i64, 0)
},
&value,
)
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/naive/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,20 @@ pub mod ts_nanoseconds {
where
E: de::Error,
{
NaiveDateTime::from_timestamp_opt(value / 1_000_000_000, (value % 1_000_000_000) as u32)
.ok_or_else(|| E::custom(ne_timestamp(value)))
NaiveDateTime::from_timestamp_opt(
value.div_euclid(1_000_000_000),
(value.rem_euclid(1_000_000_000)) as u32,
)
.ok_or_else(|| E::custom(ne_timestamp(value)))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
NaiveDateTime::from_timestamp_opt(
value as i64 / 1_000_000_000,
(value as i64 % 1_000_000_000) as u32,
(value / 1_000_000_000) as i64,
(value % 1_000_000_000) as u32,
)
.ok_or_else(|| E::custom(ne_timestamp(value)))
}
Expand Down Expand Up @@ -414,11 +417,8 @@ pub mod ts_microseconds {
where
E: de::Error,
{
NaiveDateTime::from_timestamp_opt(
value / 1_000_000,
((value % 1_000_000) * 1000) as u32,
)
.ok_or_else(|| E::custom(ne_timestamp(value)))
NaiveDateTime::from_timestamp_micros(value)
.ok_or_else(|| E::custom(ne_timestamp(value)))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
Expand Down Expand Up @@ -671,7 +671,7 @@ pub mod ts_milliseconds {
where
E: de::Error,
{
NaiveDateTime::from_timestamp_opt(value / 1000, ((value % 1000) * 1_000_000) as u32)
NaiveDateTime::from_timestamp_millis(value)
.ok_or_else(|| E::custom(ne_timestamp(value)))
}

Expand Down

0 comments on commit 4f9f292

Please sign in to comment.