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 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};
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 crate::types::serde_timestamp;
pub(crate) fn serialize<S>(
this: &Option<DateTime<Utc>>,
serializer: S,
@ -257,8 +271,7 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
where
D: Deserializer<'de>,
{
Ok(Option::<i64>::deserialize(deserializer)?
.map(|timestamp| DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc)))
Option::<i64>::deserialize(deserializer)?.map(serde_timestamp).transpose()
}
#[test]
@ -271,7 +284,8 @@ pub(crate) mod serde_opt_date_from_unix_timestamp {
{
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();
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 {
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Utc};
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>
where
S: Serializer,
@ -301,9 +317,7 @@ pub(crate) mod serde_date_from_unix_timestamp {
where
D: Deserializer<'de>,
{
let timestamp = i64::deserialize(deserializer)?;
Ok(DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc))
serde_timestamp(i64::deserialize(deserializer)?)
}
}
@ -360,10 +374,6 @@ pub(crate) mod duration_secs {
where
S: Serializer,
{
// match this {
// Some(url) => url.serialize(serializer),
// None => "".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 crate::types::serde_timestamp;
/// A range of time, before some date (for example a time before a restrictions
/// will be lifted from a member of a chat).
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
@ -31,10 +33,7 @@ impl<'de> Deserialize<'de> for UntilDate {
{
match v {
0 => Ok(UntilDate::Forever),
timestamp => Ok(UntilDate::Date(DateTime::from_utc(
NaiveDateTime::from_timestamp(timestamp, 0),
Utc,
))),
timestamp => serde_timestamp(timestamp).map(UntilDate::Date),
}
}

View file

@ -302,7 +302,8 @@ mod test {
#[test]
fn message() {
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#"{
"update_id":892252934,