Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialization of negative timestamps #1194

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ pub mod ts_nanoseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355733).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(-1, 999_999_999).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand All @@ -221,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 @@ -452,6 +458,9 @@ pub mod ts_microseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(-1, 999_999_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand All @@ -474,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 @@ -704,6 +716,9 @@ pub mod ts_milliseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918000000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(-1, 999_000_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<DateTime<Utc>, D::Error>
Expand All @@ -725,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 @@ -914,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 @@ -994,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
38 changes: 28 additions & 10 deletions src/naive/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ pub mod ts_nanoseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_999_999).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<NaiveDateTime, D::Error>
Expand All @@ -157,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 @@ -259,6 +265,9 @@ pub mod ts_nanoseconds_option {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733) });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_999_999) });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
Expand Down Expand Up @@ -383,6 +392,9 @@ pub mod ts_microseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_999_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<NaiveDateTime, D::Error>
Expand All @@ -405,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 @@ -510,6 +519,9 @@ pub mod ts_microseconds_option {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000) });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_999_000) });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
Expand Down Expand Up @@ -634,6 +646,9 @@ pub mod ts_milliseconds {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000).unwrap() });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_000_000).unwrap() });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<NaiveDateTime, D::Error>
Expand All @@ -656,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 Expand Up @@ -758,6 +773,9 @@ pub mod ts_milliseconds_option {
///
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000) });
///
/// let my_s: S = serde_json::from_str(r#"{ "time": -1 }"#)?;
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(-1, 999_000_000) });
/// # Ok::<(), serde_json::Error>(())
/// ```
pub fn deserialize<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
Expand Down
Loading