Skip to content

Commit fdc11e6

Browse files
committed
Support RFC3339 timestamps
This extracts @lukesteensen's RFC 3339 timestamp parser from #377. This allows rust-tuf to interoperate better with other TUF implementations, which might include things like microseconds, or not be in the UTC timezone.
1 parent 8c6a86f commit fdc11e6

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

tuf/src/interchange/cjson/shims.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ fn valid_spec_version(other: &str) -> bool {
1919
}
2020

2121
fn parse_datetime(ts: &str) -> Result<DateTime<Utc>> {
22-
Utc.datetime_from_str(ts, "%FT%TZ")
22+
DateTime::parse_from_rfc3339(ts)
23+
.map(|ts| ts.with_timezone(&Utc))
2324
.map_err(|e| Error::Encoding(format!("Can't parse DateTime: {:?}", e)))
2425
}
2526

@@ -601,4 +602,28 @@ mod test {
601602
);
602603
}
603604
}
605+
606+
#[test]
607+
fn datetime_formats() {
608+
// The TUF spec says datetimes should be in ISO8601 format, specifically
609+
// "YYYY-MM-DDTHH:MM:SSZ". Since not all TUF clients adhere strictly to that, we choose to
610+
// be more lenient here. The following represent the intersection of valid ISO8601 and
611+
// RFC3339 datetime formats (source: https://ijmacd.github.io/rfc3339-iso8601/).
612+
let valid_formats = [
613+
"2022-08-30T19:53:55Z",
614+
"2022-08-30T19:53:55.7Z",
615+
"2022-08-30T19:53:55.77Z",
616+
"2022-08-30T19:53:55.775Z",
617+
"2022-08-30T19:53:55+00:00",
618+
"2022-08-30T19:53:55.7+00:00",
619+
"2022-08-30T14:53:55-05:00",
620+
"2022-08-30T14:53:55.7-05:00",
621+
"2022-08-30T14:53:55.77-05:00",
622+
"2022-08-30T14:53:55.775-05:00",
623+
];
624+
625+
for format in valid_formats {
626+
assert!(parse_datetime(format).is_ok(), "should parse {:?}", format);
627+
}
628+
}
604629
}

0 commit comments

Comments
 (0)