Skip to content

Commit a0aa632

Browse files
author
leonlux
committed
update example in custom-date-format.md to use NaiveDateTime::parse_from_str instead of deprecated Utc.datetime_from_str
1 parent 828d13e commit a0aa632

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

_src/custom-date-format.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ attribute is used to provide the logic for handling the custom representation.
66

77
!PLAYGROUND 7185eb211a4822ce97184ae25fedda91
88
```rust
9-
use chrono::{DateTime, Utc};
9+
use chrono::NaiveDateTime;
1010
use serde::{Serialize, Deserialize};
1111

1212
#[derive(Serialize, Deserialize, Debug)]
1313
pub struct StructWithCustomDate {
1414
// DateTime supports Serde out of the box, but uses RFC3339 format. Provide
1515
// some custom logic to make it use our desired format.
1616
#[serde(with = "my_date_format")]
17-
pub timestamp: DateTime<Utc>,
17+
pub timestamp: NaiveDateTime,
1818

1919
// Any other fields in the struct.
2020
pub bidder: String,
2121
}
2222

2323
mod my_date_format {
24-
use chrono::{DateTime, Utc, TimeZone};
24+
use chrono::NaiveDateTime;
2525
use serde::{self, Deserialize, Serializer, Deserializer};
2626

2727
const FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
@@ -34,11 +34,11 @@ mod my_date_format {
3434
//
3535
// although it may also be generic over the input types T.
3636
pub fn serialize<S>(
37-
date: &DateTime<Utc>,
37+
date: &NaiveDateTime,
3838
serializer: S,
3939
) -> Result<S::Ok, S::Error>
40-
where
41-
S: Serializer,
40+
where
41+
S: Serializer,
4242
{
4343
let s = format!("{}", date.format(FORMAT));
4444
serializer.serialize_str(&s)
@@ -53,13 +53,14 @@ mod my_date_format {
5353
// although it may also be generic over the output types T.
5454
pub fn deserialize<'de, D>(
5555
deserializer: D,
56-
) -> Result<DateTime<Utc>, D::Error>
57-
where
58-
D: Deserializer<'de>,
56+
) -> Result<NaiveDateTime, D::Error>
57+
where
58+
D: Deserializer<'de>,
5959
{
6060
let s = String::deserialize(deserializer)?;
61-
Utc.datetime_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
61+
NaiveDateTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
6262
}
63+
6364
}
6465

6566
fn main() {

0 commit comments

Comments
 (0)