Skip to content

Commit

Permalink
feat: allow unix milliseconds in time field in json
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <[email protected]>
  • Loading branch information
paralin committed Apr 22, 2024
1 parent 74107ec commit 3dc5102
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions json/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,24 @@ func (s *UnmarshalState) ReadTime() *time.Time {
if s.ReadNil() {
return nil
}
t, err := time.Parse("2006-01-02T15:04:05.999999999Z", s.inner.ReadString())
if err != nil {
s.SetErrorf("invalid time: %w", err)
return nil
nextTok := s.WhatIsNext()
switch nextTok {
case jsoniter.StringValue:
t, err := time.Parse("2006-01-02T15:04:05.999999999Z", s.inner.ReadString())
if err != nil {
s.SetErrorf("invalid time: %w", err)
return nil
}
return &t
case jsoniter.NumberValue:
timeMs := s.inner.ReadInt64()
t := time.UnixMilli(timeMs)
return &t
default:
s.SetErrorf("invalid value type for duration: %s", valueTypeString(nextTok))
t := time.Time{}
return &t
}
return &t
}

// ReadDuration reads a duration.
Expand Down

0 comments on commit 3dc5102

Please sign in to comment.