-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: encoding/json/v2: encode time with format:unix
as integer instead of float
#73486
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
Comments
Here are the pros and cons of this. Benefits
Detriments
Note that the behavior today (having fractional precision by default) can be turned into a non-fractional integer by always calling If we change this, we would also need something like |
Perhaps this proposal is too narrow in scope. There are often times where you want to adjust a One could consider the ability to specify something like |
This seems like a lot of complexity. A potentially more straight forward solution might be to support customizable formatting behavior via #71664. This way one could change the behaviour of e.g. the |
Proposal Details
The new package
json/v2
has format optionsunix
,unixmilli
,unixmicro
, andunixnano
fortime.Time
, that encode the time as float of seconds, milliseconds, microseconds, and nanoseconds since epoch. The encoded float has nanosecond precision for each unit.I propose that we change the format options
unix
,unixmilli
,unixmicro
, andunixnano
to emit integer timestamps, rather than floats.Motivation
Many APIs that use epoch timestamps use integers epoch timestamps. High precision floats are not commonly used due to rounding issues in clients.
The
time
package has thetime.Time
-methodsUnix
,UnixMilli
,UnixMicro
, andUnixNano
that return integer values. Equally namedjson/v2
format options should have the same logic to avoid confusion.time.Time
json/v2
formatUnix() int64
format:unix
UnixMilli() int64
format:unixmilli
UnixMicro() int64
format:unixmicro
UnixNano() int64
format:unixnano
Example of Current Behavior (Playground)
The text was updated successfully, but these errors were encountered: