Remove use of deprecated chrono::NaiveDateTime::from_timestamp

This commit is contained in:
Maybe Waffle 2022-11-14 17:55:11 +04:00
parent 0df19e5fc9
commit 5f0df0577f
3 changed files with 28 additions and 18 deletions

View file

@ -239,10 +239,24 @@ pub use chat_id::*;
pub use recipient::*; pub use recipient::*;
pub use user_id::*; pub use user_id::*;
pub(crate) mod serde_opt_date_from_unix_timestamp { /// Converts an `i64` timestump to a `choro::DateTime`, producing serde error
/// for invalid timestumps
pub(crate) fn serde_timestamp<E: serde::de::Error>(
timestamp: i64,
) -> Result<chrono::DateTime<chrono::Utc>, E> {
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
NaiveDateTime::from_timestamp_opt(timestamp, 0)
.ok_or_else(|| E::custom("invalid timestump"))
.map(|naive| DateTime::from_utc(naive, Utc))
}
pub(crate) mod serde_opt_date_from_unix_timestamp {
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::types::serde_timestamp;
pub(crate) fn serialize<S>( pub(crate) fn serialize<S>(
this: &Option<DateTime<Utc>>, this: &Option<DateTime<Utc>>,
serializer: S, serializer: S,
@ -257,8 +271,7 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
Ok(Option::<i64>::deserialize(deserializer)? Option::<i64>::deserialize(deserializer)?.map(serde_timestamp).transpose()
.map(|timestamp| DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)))
} }
#[test] #[test]
@ -271,7 +284,8 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
{ {
let json = r#"{"date":1}"#; let json = r#"{"date":1}"#;
let expected = DateTime::from_utc(NaiveDateTime::from_timestamp(1, 0), Utc); let expected =
DateTime::from_utc(chrono::NaiveDateTime::from_timestamp_opt(1, 0).unwrap(), Utc);
let Struct { date } = serde_json::from_str(json).unwrap(); let Struct { date } = serde_json::from_str(json).unwrap();
assert_eq!(date, Some(expected)); assert_eq!(date, Some(expected));
@ -287,9 +301,11 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
} }
pub(crate) mod serde_date_from_unix_timestamp { pub(crate) mod serde_date_from_unix_timestamp {
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::types::serde_timestamp;
pub(crate) fn serialize<S>(this: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error> pub(crate) fn serialize<S>(this: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
@ -301,9 +317,7 @@ pub(crate) mod serde_date_from_unix_timestamp {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let timestamp = i64::deserialize(deserializer)?; serde_timestamp(i64::deserialize(deserializer)?)
Ok(DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc))
} }
} }
@ -360,10 +374,6 @@ pub(crate) mod duration_secs {
where where
S: Serializer, S: Serializer,
{ {
// match this {
// Some(url) => url.serialize(serializer),
// None => "".serialize(serializer),
// }
this.as_secs().serialize(serializer) this.as_secs().serialize(serializer)
} }

View file

@ -1,6 +1,8 @@
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{de::Visitor, Deserialize, Serialize}; use serde::{de::Visitor, Deserialize, Serialize};
use crate::types::serde_timestamp;
/// A range of time, before some date (for example a time before a restrictions /// A range of time, before some date (for example a time before a restrictions
/// will be lifted from a member of a chat). /// will be lifted from a member of a chat).
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
@ -31,10 +33,7 @@ impl<'de> Deserialize<'de> for UntilDate {
{ {
match v { match v {
0 => Ok(UntilDate::Forever), 0 => Ok(UntilDate::Forever),
timestamp => Ok(UntilDate::Date(DateTime::from_utc( timestamp => serde_timestamp(timestamp).map(UntilDate::Date),
NaiveDateTime::from_timestamp(timestamp, 0),
Utc,
))),
} }
} }

View file

@ -302,7 +302,8 @@ mod test {
#[test] #[test]
fn message() { fn message() {
let timestamp = 1_569_518_342; let timestamp = 1_569_518_342;
let date = DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc); let date =
DateTime::from_utc(NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(), Utc);
let json = r#"{ let json = r#"{
"update_id":892252934, "update_id":892252934,