Skip to content
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 formatting of tendermint::Time values #775

Merged
merged 1 commit into from
Jan 8, 2021

Conversation

romac
Copy link
Member

@romac romac commented Jan 7, 2021

Close: #774

The Time datatype needs to be formatted the same way as
Go's time.RFC3339Nano format, ie. a RFC3339 date-time
with left-padded nanoseconds digits without trailing zeros
and no trailing dot.

This is taken care of by the to_rfc339_nanos function in
the tendermint_proto::serializers::timestamp module.

Prior to this commit, this function would incorrectly
trim the leading zeros of the nanoseconds digits, which
would break the Time -> String -> Time roundtrip.

For example, the date "2021-01-07T17:52:04.034475Z" was serialized
as "2021-01-07T17:52:04.34475Z", which then decodes to
"2021-01-07T17:52:04.344750Z", shifting the leading zero to the end.

This commit fixes to_rfc339_nanos function by using chrono's
formatting facilities to preserve the precision of the nanoseconds,
and then manually trim the trailing zeros and dot.

  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG.md

The `Time` datatype needs to be formatted the same way as
Go's `time.RFC3339Nano` format, ie. a RFC3339 date-time
with left-padded nanoseconds digits without trailing zeros
and no trailing dot.

This is taken care of by the `to_rfc339_nanos` function in
the `tendermint_proto::serializers::timestamp` module.

Prior to this commit, this function would incorrectly
trim the leading zeros of the nanoseconds digits, which
would break the `Time` -> String -> `Time` roundtrip.

For example, the date "2021-01-07T17:52:04.034475Z" was serialized
as "2021-01-07T17:52:04.34475Z", which then decodes to
"2021-01-07T17:52:04.344750Z", shifting the leading zero to the end.

This commit fixes `to_rfc339_nanos` function by using `chrono`'s
formatting facilities to preserve the precision of the nanoseconds,
and then manually trim the trailing zeros and dot.
Copy link
Contributor

@ancazamfir ancazamfir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Romain for getting to the root cause of this!

Copy link
Member

@thanethomson thanethomson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for catching this Romain!

@thanethomson thanethomson merged commit c44ae5b into master Jan 8, 2021
@thanethomson thanethomson deleted the romac/fix-time-ser-roundtrip branch January 8, 2021 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants