Proposal Details
The new package json/v2 has format options unix, unixmilli, unixmicro, and unixnano for time.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, and unixnano 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 the time.Time-methods Unix, UnixMilli, UnixMicro, and UnixNano that return integer values. Equally named json/v2 format options should have the same logic to avoid confusion.
Example of Current Behavior (Playground)
type times struct {
Time time.Time `json:"time,format:unix"`
Time2 time.Time `json:"time2,format:unixmilli"`
Time3 time.Time `json:"time3,format:unixmicro"`
Time4 time.Time `json:"time4,format:unixnano"`
}
enc, _ := json.Marshal(times{
Time: time.Unix(123, 456),
Time2: time.Unix(123, 456),
Time3: time.Unix(123, 456),
Time4: time.Unix(123, 456),
})
fmt.Print(string(enc))
// Output:
// {"time":123.000000456,"time2":123000.000456,"time3":123000000.456,"time4":123000000456}
Proposal Details
The new package
json/v2has format optionsunix,unixmilli,unixmicro, andunixnanofortime.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, andunixnanoto 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
timepackage has thetime.Time-methodsUnix,UnixMilli,UnixMicro, andUnixNanothat return integer values. Equally namedjson/v2format options should have the same logic to avoid confusion.time.Timejson/v2formatUnix() int64format:unixUnixMilli() int64format:unixmilliUnixMicro() int64format:unixmicroUnixNano() int64format:unixnanoExample of Current Behavior (Playground)