Skip to content

Commit

Permalink
Merge pull request #335 from karl-chan/patch-1
Browse files Browse the repository at this point in the history
Fix Rfc850DatetimeFormat to handle 2-digit year
  • Loading branch information
adamw authored Mar 15, 2024
2 parents 4acff29 + acd4df5 commit bc4b665
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/src/main/scala/sttp/model/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import sttp.model.internal.Validate._

import java.time.{Instant, ZoneId}
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.ChronoField
import java.util.Locale
import scala.util.{Failure, Success, Try}
import scala.util.hashing.MurmurHash3
Expand Down Expand Up @@ -141,8 +143,12 @@ object Header {

//

private lazy val Rfc850DatetimePattern = "dd-MMM-yyyy HH:mm:ss zzz"
private lazy val Rfc850DatetimeFormat = DateTimeFormatter.ofPattern(Rfc850DatetimePattern, Locale.US)
private lazy val Rfc850DatetimeFormat =
new DateTimeFormatterBuilder()
.appendPattern("dd-MMM-")
.appendValueReduced(ChronoField.YEAR, 2, 4, 1970)
.appendPattern(" HH:mm:ss zzz")
.toFormatter(Locale.US);

val Rfc850WeekDays = Set("mon", "tue", "wed", "thu", "fri", "sat", "sun") // not private b/c of bin-compat
private val Rfc1123WeekDays: Array[String] = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
Expand Down
24 changes: 24 additions & 0 deletions core/src/test/scalajvm/sttp/model/HeaderTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@ class HeaderTests extends AnyFlatSpec with Matchers {
"rfc1123-date1" should "be parsed correctly" in {
Header.parseHttpDate(rfc1123DatetimeFormatted) shouldBe Right(rfc1123DatetimeToBeChecked)
}

"rfc850-2-digit-year-after-70" should "be parsed as 19xx" in {
Header.parseHttpDate("Sun, 08-Feb-70 02:03:04 GMT") shouldBe Right(
Instant.from {
ZonedDateTime.of(LocalDateTime.of(1970, 2, 8, 2, 3, 4), ZoneId.of("Z"))
}
)
}

"rfc850-2-digit-year-before-69" should "be parsed as 20xx" in {
Header.parseHttpDate("Fri, 08-Feb-69 02:03:04 GMT") shouldBe Right(
Instant.from {
ZonedDateTime.of(LocalDateTime.of(2069, 2, 8, 2, 3, 4), ZoneId.of("Z"))
}
)
}

"rfc850-4-digit-year" should "be parsed correctly" in {
Header.parseHttpDate("Wed, 08-Feb-2023 02:03:04 GMT") shouldBe Right(
Instant.from {
ZonedDateTime.of(LocalDateTime.of(2023, 2, 8, 2, 3, 4), ZoneId.of("Z"))
}
)
}
}

0 comments on commit bc4b665

Please sign in to comment.