Skip to content

Commit

Permalink
fix(rt): parse fractional seconds for http-dates (rfc5322)
Browse files Browse the repository at this point in the history
awsJson1.1 protocol tests include a test for fractional seconds. Note
this does not conform to the rfc.

See: smithy-lang/smithy#672
  • Loading branch information
aajtodd committed Jan 8, 2021
1 parent 0721a52 commit e57eb3f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private fun tzOffsetSecRfc5322(): Parser<Int> = { str, pos ->
}

// parse RFC-5322 timestamp
// HH:MM[:SS] GMT
// HH:MM[:SS][.(s*)] GMT
private fun rfc5322Time(): Parser<ParsedTime> = { str, pos ->
val (pos0, hour) = timeHour()(str, pos)
val (pos1, min) = preceded(char(':'), timeMin())(str, pos0)
Expand All @@ -225,8 +225,10 @@ private fun rfc5322Time(): Parser<ParsedTime> = { str, pos ->
} else {
ParseResult(pos1, 0)
}
val (pos3, offsetSec) = preceded(::sp, tzOffsetSecRfc5322())(str, pos2)
ParseResult(pos3, ParsedTime(hour, min, sec, 0, offsetSec))

val (pos3, ns) = optionalOr(preceded(oneOf("."), timeNanos()), 0)(str, pos2)
val (pos4, offsetSec) = preceded(::sp, tzOffsetSecRfc5322())(str, pos3)
ParseResult(pos4, ParsedTime(hour, min, sec, ns, offsetSec))
}

/**
Expand Down Expand Up @@ -262,5 +264,5 @@ internal fun parseRfc5322(input: String): ParsedDatetime {
val (_, ts) = preceded(::sp, rfc5322Time())(input, pos3)

// Sun, 06 Nov 1994 08:49:37 GMT
return ParsedDatetime(year, month, day, ts.hour, ts.min, ts.sec, 0, ts.offsetSec)
return ParsedDatetime(year, month, day, ts.hour, ts.min, ts.sec, ts.ns, ts.offsetSec)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class ParseRfc5322Test {
// obsolete zone names
ParseTest("Sun, 06 Nov 1994 08:49:37 UTC", 1994, 11, 6, 8, 49, 37, 0, 0),
ParseTest("Sun, 06 Nov 1994 08:49:37 UT", 1994, 11, 6, 8, 49, 37, 0, 0),
ParseTest("Sun, 06 Nov 1994 08:49:37 Z", 1994, 11, 6, 8, 49, 37, 0, 0)
ParseTest("Sun, 06 Nov 1994 08:49:37 Z", 1994, 11, 6, 8, 49, 37, 0, 0),
// fractional seconds (required by awsJson1.1 apparently). See: https://github.com/awslabs/smithy/blob/master/smithy-aws-protocol-tests/model/awsJson1_1/kitchen-sink.smithy#L682
ParseTest("Sun, 06 Nov 1994 08:49:37.001 GMT", 1994, 11, 6, 8, 49, 37, 1_000_000, 0)
)

@Test
Expand Down

0 comments on commit e57eb3f

Please sign in to comment.