diff --git a/time.go b/time.go index 97a49b2..5a620ff 100644 --- a/time.go +++ b/time.go @@ -62,6 +62,62 @@ func TimeUnmarshalIntUnixFor(names ...string) func(*jsontext.Decoder, *time.Time } } +// TimeMarshalIntUnixMilli is a custom marshaler for time.Time, marshaling them as integers representing unix time in +// milliseconds. +func TimeMarshalIntUnixMilli(enc *jsontext.Encoder, t time.Time) error { + if t.IsZero() { + return enc.WriteToken(jsontext.Int(0)) + } + + return enc.WriteToken(jsontext.Int(t.UnixMilli())) +} + +// TimeUnmarshalIntUnixMilli is a custom unmarshaler for time.Time, unmarshaling them from integers representing unix +// time in milliseconds. +func TimeUnmarshalIntUnixMilli(dec *jsontext.Decoder, d *time.Time) error { + var ms int64 + if err := json.UnmarshalDecode(dec, &ms); err != nil { + return err + } + + if ms == 0 { + *d = time.Time{} + } else { + *d = time.UnixMilli(ms) + } + + return nil +} + +// TimeMarshalIntUnixMilliFor returns a marshaler for time.Time that writes, as [TimeMarshalIntUnixMilli] does, the times +// held by an object member of one of the names given, or by an array such a member holds; see [DateMarshalIntUnixFor]. +func TimeMarshalIntUnixMilliFor(names ...string) func(*jsontext.Encoder, time.Time) error { + set := nameSet(names) + + return func(enc *jsontext.Encoder, t time.Time) error { + if !set[memberName(enc.StackPointer())] { + return errors.ErrUnsupported + } + + return TimeMarshalIntUnixMilli(enc, t) + } +} + +// TimeUnmarshalIntUnixMilliFor returns an unmarshaler for time.Time that reads, as [TimeUnmarshalIntUnixMilli] does, the +// times held by an object member of one of the names given, or by an array such a member holds; see +// [DateMarshalIntUnixFor]. +func TimeUnmarshalIntUnixMilliFor(names ...string) func(*jsontext.Decoder, *time.Time) error { + set := nameSet(names) + + return func(dec *jsontext.Decoder, t *time.Time) error { + if !set[memberName(dec.StackPointer())] { + return errors.ErrUnsupported + } + + return TimeUnmarshalIntUnixMilli(dec, t) + } +} + // timeAltLayout is a fallback layout for time strings that don't conform to RFC3339. const timeAltLayout = "Mon Jan 2 2006 15:04:05 MST-0700" diff --git a/time_test.go b/time_test.go index dd641d3..5d8daa2 100644 --- a/time_test.go +++ b/time_test.go @@ -220,3 +220,59 @@ func TestStringOrUnixTime(t *testing.T) { } }) } + +// A time is written and read as unix milliseconds, to the millisecond, and the zero time as 0; by member name, beside +// times in unix seconds and in the type's own form. +func TestUnixMilliTime(t *testing.T) { + t.Parallel() + + type trade struct { + At time.Time `json:"at"` + Read time.Time `json:"read"` + Created time.Time `json:"created"` + Ticks []time.Time `json:"ticks"` + Settled time.Time `json:"settled"` + Reported time.Time `json:"reported"` + } + + ms, secs := []string{"at", "ticks", "settled"}, []string{"read"} + opts := json.JoinOptions( + json.WithMarshalers(json.JoinMarshalers( + json.MarshalToFunc(jsonutil.TimeMarshalIntUnixMilliFor(ms...)), + json.MarshalToFunc(jsonutil.TimeMarshalIntUnixFor(secs...)), + )), + json.WithUnmarshalers(json.JoinUnmarshalers( + json.UnmarshalFromFunc(jsonutil.TimeUnmarshalIntUnixMilliFor(ms...)), + json.UnmarshalFromFunc(jsonutil.TimeUnmarshalIntUnixFor(secs...)), + )), + ) + + at := time.UnixMilli(1795464914123).UTC() + in := trade{At: at, Read: at.Truncate(time.Second), Created: at, Ticks: []time.Time{at}, Reported: at} + + b, err := json.Marshal(in, opts) + if err != nil { + t.Fatal(err) + } + + want := `{"at":1795464914123,"read":1795464914,"created":"2026-11-23T20:15:14.123Z","ticks":[1795464914123],` + + `"settled":0,"reported":"2026-11-23T20:15:14.123Z"}` + if string(b) != want { + t.Fatalf("got %s, want %s", b, want) + } + + var out trade + if err := json.Unmarshal(b, &out, opts); err != nil { + t.Fatal(err) + } + + if !out.At.Equal(at) || !out.Ticks[0].Equal(at) || !out.Read.Equal(in.Read) || !out.Settled.IsZero() { + t.Errorf("got %+v, want %+v", out, in) + } + + // without names, every time is unix milliseconds + var plain time.Time + if err := json.Unmarshal([]byte(`1795464914123`), &plain, json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.TimeUnmarshalIntUnixMilli))); err != nil || !plain.Equal(at) { + t.Errorf("got %v (%v), want %v", plain, err, at) + } +}