Skip to content

Commit

Permalink
Omit fractional seconds from date-time format
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmitt committed Sep 18, 2023
1 parent cf8c834 commit 02c581c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,9 @@ message = "Fix regression with redacting sensitive HTTP response bodies."
references = ["smithy-rs#2926", "smithy-rs#2972"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client" }
author = "ysaito1001"

[[smithy-rs]]
message = "Omit fractional seconds from `date-time` format."
references = ["smithy-rs#2831"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "all" }
author = "rschmitt"
4 changes: 2 additions & 2 deletions rust-runtime/aws-smithy-json/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ mod tests {
object.finish();

assert_eq!(
r#"{"epoch_seconds":5.2,"date_time":"2021-05-24T15:34:50.123Z","http_date":"Wed, 21 Oct 2015 07:28:00 GMT"}"#,
r#"{"epoch_seconds":5.2,"date_time":"2021-05-24T15:34:50Z","http_date":"Wed, 21 Oct 2015 07:28:00 GMT"}"#,
&output,
)
}
Expand Down Expand Up @@ -340,7 +340,7 @@ mod tests {
array.finish();

assert_eq!(
r#"[5.2,"2021-05-24T15:34:50.123Z","Wed, 21 Oct 2015 07:28:00 GMT"]"#,
r#"[5.2,"2021-05-24T15:34:50Z","Wed, 21 Oct 2015 07:28:00 GMT"]"#,
&output,
)
}
Expand Down
2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ mod tests {
"Action=SomeAction\
&Version=1.0\
&epoch_seconds=5.2\
&date_time=2021-05-24T15%3A34%3A50.123Z\
&date_time=2021-05-24T15%3A34%3A50Z\
&http_date=Wed%2C%2021%20Oct%202015%2007%3A28%3A00%20GMT\
",
out
Expand Down
22 changes: 2 additions & 20 deletions rust-runtime/aws-smithy-types/src/date_time/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ pub(crate) mod rfc3339 {
)
.into()
}
let (year, month, day, hour, minute, second, micros) = {
let (year, month, day, hour, minute, second) = {
let s = OffsetDateTime::from_unix_timestamp_nanos(date_time.as_nanos())
.map_err(out_of_range)?;
(
Expand All @@ -462,7 +462,6 @@ pub(crate) mod rfc3339 {
s.hour(),
s.minute(),
s.second(),
s.microsecond(),
)
};

Expand All @@ -479,26 +478,9 @@ pub(crate) mod rfc3339 {
year, month, day, hour, minute, second
)
.unwrap();
format_subsecond_fraction(&mut out, micros);
out.push('Z');
Ok(out)
}

/// Formats sub-second fraction for RFC-3339 (including the '.').
/// Expects to be called with a number of `micros` between 0 and 999_999 inclusive.
fn format_subsecond_fraction(into: &mut String, micros: u32) {
debug_assert!(micros < 1_000_000);
if micros > 0 {
into.push('.');
let (mut remaining, mut place) = (micros, 100_000);
while remaining > 0 {
let digit = (remaining / place) % 10;
into.push(char::from(b'0' + (digit as u8)));
remaining -= digit * place;
place /= 10;
}
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -623,7 +605,7 @@ mod tests {
rfc3339::format(&DateTime::from_secs(-62_135_596_800)).unwrap()
);
assert_eq!(
"9999-12-31T23:59:59.999999Z",
"9999-12-31T23:59:59Z",
rfc3339::format(&DateTime::from_secs_and_nanos(253402300799, 999_999_999)).unwrap()
);

Expand Down
2 changes: 1 addition & 1 deletion rust-runtime/aws-smithy-types/src/date_time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ mod test {
let date_time = DateTime::from_fractional_secs(1576540098, 0.52);
assert_eq!(
date_time.fmt(Format::DateTime).unwrap(),
"2019-12-16T23:48:18.52Z"
"2019-12-16T23:48:18Z"
);
assert_eq!(
date_time.fmt(Format::EpochSeconds).unwrap(),
Expand Down
Loading

0 comments on commit 02c581c

Please sign in to comment.