-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Add missing nano part when encoding timestamp to document #1576
fix: Add missing nano part when encoding timestamp to document #1576
Conversation
@@ -101,7 +101,7 @@ class DocumentEncoderSchemaVisitor( | |||
.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 EPOCH_SECONDS => ts => DNumber(BigDecimal(s"${ts.epochSecond}.${ts.nano}")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I am not really a fan of constructing bigDecimal from string in here but I couldn't think of anything better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BigDecimal(ts.epochSecond + 10e-9 * ts.nano)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should've made myself clearer. I thought about this approach, but I didn't want to use because of potential precision loss issue.
Consider following:
val now = Instant.now()
val epochSecond: Long = now.getEpochSecond
val nano: Int = 999_999_999 // almost 1 second
val result: Double = epochSecond + 1e-9 * nano
println(s"Result with potential precision loss: ${BigDecimal(result)}")
val accurateBigDecimal =
BigDecimal(epochSecond) + (BigDecimal(nano) * BigDecimal(10).pow(-9))
println(s"Accurate result : ${accurateBigDecimal}")
Which prints:
Result with potential precision loss: 1725278847
Accurate result : 1725278846.999999999
This is still probably a better idea than going through string parsing :)
e0302bc
to
d2d21f4
Compare
PR Checklist (not all items are relevant to all PRs)