Skip to content

Commit

Permalink
Consider %s to have be a timestamp in UTC
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jun 8, 2023
1 parent 15f8116 commit d7e474d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ fn test_datetime_parse_from_str() {
Utc.datetime_from_str("Fri, 09 Aug 2013 23:54:35 GMT", "%a, %d %b %Y %H:%M:%S GMT"),
Ok(Utc.with_ymd_and_hms(2013, 8, 9, 23, 54, 35).unwrap())
);
assert_eq!(
DateTime::parse_from_str("0", "%s").unwrap(),
NaiveDateTime::from_timestamp_opt(0, 0).unwrap().and_utc().fixed_offset()
);
}

#[test]
Expand Down
7 changes: 6 additions & 1 deletion src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,12 @@ impl Parsed {
/// plus a time zone offset.
/// Either way those fields have to be consistent to each other.
pub fn to_datetime(&self) -> ParseResult<DateTime<FixedOffset>> {
let offset = self.offset.ok_or(NOT_ENOUGH)?;
// If there is no explicit offset, consider a timestamp value as indication of a UTC value.
let offset = match (self.offset, self.timestamp) {
(Some(off), _) => off,
(None, Some(_)) => 0, // UNIX timestamp may assume 0 offset
(None, None) => return Err(NOT_ENOUGH),
};
let datetime = self.to_naive_datetime_with_offset(offset)?;
let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?;

Expand Down

0 comments on commit d7e474d

Please sign in to comment.