Skip to content

Commit f270c84

Browse files
authored
Merge pull request #159 from Lachstec/master
Remove usage of deprecated Utc.datetime_from_str from the custom date format example
2 parents 828d13e + 0dfe85a commit f270c84

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

_src/custom-date-format.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This uses the [`chrono`](https://github.com/chronotope/chrono) crate to
44
serialize and deserialize JSON data containing a custom date format. The `with`
55
attribute is used to provide the logic for handling the custom representation.
66

7-
!PLAYGROUND 7185eb211a4822ce97184ae25fedda91
7+
!PLAYGROUND 2ef7c347c76b030fe7e8c59ce9efccd3
88
```rust
99
use chrono::{DateTime, Utc};
1010
use serde::{Serialize, Deserialize};
@@ -21,7 +21,7 @@ pub struct StructWithCustomDate {
2121
}
2222

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

2727
const FORMAT: &'static str = "%Y-%m-%d %H:%M:%S";
@@ -58,7 +58,8 @@ mod my_date_format {
5858
D: Deserializer<'de>,
5959
{
6060
let s = String::deserialize(deserializer)?;
61-
Utc.datetime_from_str(&s, FORMAT).map_err(serde::de::Error::custom)
61+
let dt = NaiveDateTime::parse_from_str(&s, FORMAT).map_err(serde::de::Error::custom)?;
62+
Ok(DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc))
6263
}
6364
}
6465

0 commit comments

Comments
 (0)