Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Fix codebuild_time for chrono ^0.4.29 by parsing via NaiveDateTime
  • Loading branch information
Chris Leach committed Sep 11, 2023
commit d6e91ce5d6f395dc3885d94d2386b46a7fc999d3
17 changes: 9 additions & 8 deletions lambda-events/src/custom_serde/codebuild_time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{DateTime, TimeZone, Utc};
use chrono::{DateTime, NaiveDateTime, Utc};
use serde::ser::Serializer;
use serde::{
de::{Deserializer, Error as DeError, Visitor},
Expand All @@ -18,7 +18,8 @@ impl<'de> Visitor<'de> for TimeVisitor {
}

fn visit_str<E: DeError>(self, val: &str) -> Result<Self::Value, E> {
Utc.datetime_from_str(val, CODEBUILD_TIME_FORMAT)
NaiveDateTime::parse_from_str(val, CODEBUILD_TIME_FORMAT)
.map(|naive| naive.and_utc())
.map_err(|e| DeError::custom(format!("Parse error {} for {}", e, val)))
}
}
Expand Down Expand Up @@ -81,9 +82,9 @@ mod tests {
"date": "Sep 1, 2017 4:12:29 PM"
});

let expected = Utc
.datetime_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap();
let expected = NaiveDateTime::parse_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap()
.and_utc();
let decoded: Test = serde_json::from_value(data).unwrap();
assert_eq!(expected, decoded.date);
}
Expand All @@ -99,9 +100,9 @@ mod tests {
"date": "Sep 1, 2017 4:12:29 PM"
});

let expected = Utc
.datetime_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap();
let expected = NaiveDateTime::parse_from_str("Sep 1, 2017 4:12:29 PM", CODEBUILD_TIME_FORMAT)
.unwrap()
.and_utc();
let decoded: Test = serde_json::from_value(data).unwrap();
assert_eq!(Some(expected), decoded.date);
}
Expand Down