Skip to content

Commit

Permalink
fix(scanner): bring up to Unix(0) if parsed timestamp is negative
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme committed Oct 29, 2024
1 parent 8719a7f commit 0f57c86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 0 additions & 9 deletions json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ func searchJSON(kvs map[string]interface{}, fieldList []string, found func(key s
return false
}

func checkEachUntilFound(fieldList []string, found func(string) bool) bool {
for _, field := range fieldList {
if found(field) {
return true
}
}
return false
}

func (h *JSONHandler) clear() {
h.Level = ""
h.Time = time.Time{}
Expand Down
9 changes: 9 additions & 0 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ func Scan(ctx context.Context, src io.Reader, sink sink.Sink, opts *HandlerOptio
return err
}
}

func checkEachUntilFound(fieldList []string, found func(string) bool) bool {
for _, field := range fieldList {
if found(field) {
return true
}
}
return false
}
22 changes: 18 additions & 4 deletions time_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,37 @@ func parseTimeFloat64(value float64) time.Time {
case v > 1e12:
v *= 1e6
default:
return time.Unix(v, 0)
return fixTimebeforeUnixZero(time.Unix(v, 0))
}

return time.Unix(v/1e9, v%1e9)
return fixTimebeforeUnixZero(time.Unix(v/1e9, v%1e9))
}

var zeroTime = time.Time{}

func fixTimebeforeUnixZero(t time.Time) time.Time {
if t.Unix() >= 0 {
return t
}
// fast forward in the future at unix 0... unfortunately
// we can't handle times before that, the JSON API stack
// fails to marshal negative UNIX seconds (ConnectRPC)
t = t.AddDate(zeroTime.Year(), 0, 0)
return t
}

// tries to parse time using a couple of formats before giving up
func tryParseTime(value interface{}) (time.Time, bool) {
var t time.Time
var err error
switch v := value.(type) {
case string:
for _, layout := range TimeFormats {
t, err = time.Parse(layout, v)
t, err := time.Parse(layout, v)
if err == nil {
t = fixTimebeforeUnixZero(t)
return t, true
}

}
// try to parse unix time number from string
floatVal, err := strconv.ParseFloat(v, 64)
Expand Down

0 comments on commit 0f57c86

Please sign in to comment.