From 3dc5102188f5e1490672cad8892a008c5c3b79e6 Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Mon, 22 Apr 2024 00:15:21 -0700 Subject: [PATCH] feat: allow unix milliseconds in time field in json Signed-off-by: Christian Stewart --- json/unmarshal.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/json/unmarshal.go b/json/unmarshal.go index ce5eb15f..3f1449ca 100644 --- a/json/unmarshal.go +++ b/json/unmarshal.go @@ -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.