Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scanner): bring up to Unix(0) if parsed timestamp is negative #113

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading