Skip to content

Commit

Permalink
fix: Add missing nano part when encoding timestamp to document (#1576)
Browse files Browse the repository at this point in the history
Co-authored-by: ghostbuster91 <[email protected]>
  • Loading branch information
ghostbuster91 and ghostbuster91 authored Sep 2, 2024
1 parent d4edb32 commit 3608c82
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
25 changes: 25 additions & 0 deletions modules/bootstrapped/test/src/smithy4s/DocumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,31 @@ class DocumentSpec() extends FunSuite {
)
}

test("Document encoder - timestamp epoch seconds with nanos") {
val timestampWithNanos =
Timestamp(1716459630L, 500 * 1000 * 1000 /* half a second */ )
val result = Document.Encoder
.withExplicitDefaultsEncoding(false)
.fromSchema(TimestampOperationInput.schema)
.encode(
TimestampOperationInput(
timestampWithNanos,
timestampWithNanos,
timestampWithNanos
)
)
expect.same(
Document.obj(
"httpDate" -> Document.fromString("Thu, 23 May 2024 10:20:30.500 GMT"),
"dateTime" -> Document.fromString("2024-05-23T10:20:30.500Z"),
"epochSeconds" -> Document.fromBigDecimal(
BigDecimal("1716459630.500000")
)
),
result
)
}

test("Document decoder - timestamp defaults") {
val doc = Document.obj()
val result = Document.Decoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ class DocumentEncoderSchemaVisitor(
hints
.get(TimestampFormat)
.getOrElse(TimestampFormat.EPOCH_SECONDS) match {
case DATE_TIME => ts => DString(ts.format(DATE_TIME))
case HTTP_DATE => ts => DString(ts.format(HTTP_DATE))
case EPOCH_SECONDS => ts => DNumber(BigDecimal(ts.epochSecond))
case DATE_TIME => ts => DString(ts.format(DATE_TIME))
case HTTP_DATE => ts => DString(ts.format(HTTP_DATE))
case EPOCH_SECONDS =>
ts =>
val epochSecondsWithNanos =
BigDecimal(ts.epochSecond) + (BigDecimal(ts.nano) * BigDecimal(10)
.pow(-9))
DNumber(epochSecondsWithNanos)
}
case PDocument => from(identity)
case PFloat => from(float => DNumber(BigDecimal(float.toDouble)))
Expand Down

0 comments on commit 3608c82

Please sign in to comment.