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

Support for unix int timestamps in string values #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions logfmt_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (h *LogfmtHandler) UnmarshalLogfmt(data []byte) bool {
val := dec.Value()
if h.Time.IsZero() {
foundTime := checkEachUntilFound(h.Opts.TimeFields, func(field string) bool {
if !bytes.Equal(key, []byte(field)) {
return false
}
time, ok := tryParseTime(string(val))
if ok {
h.Time = time
Expand Down
20 changes: 13 additions & 7 deletions time_parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package humanlog

import (
"strconv"
"time"
)

Expand Down Expand Up @@ -46,24 +47,29 @@ func parseTimeFloat64(value float64) time.Time {
func tryParseTime(value interface{}) (time.Time, bool) {
var t time.Time
var err error
switch value.(type) {
switch typedVal := value.(type) {
case string:
for _, layout := range formats {
t, err = time.Parse(layout, value.(string))
t, err = time.Parse(layout, typedVal)
if err == nil {
return t, true
}
}
// try to parse unix time number from string
floatVal, err := strconv.ParseFloat(typedVal, 64)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case shouldn't this parse an int?

if err == nil {
return parseTimeFloat64(floatVal), true
}
case float32:
return parseTimeFloat64(float64(value.(float32))), true
return parseTimeFloat64(float64(typedVal)), true
case float64:
return parseTimeFloat64(value.(float64)), true
return parseTimeFloat64(typedVal), true
case int:
return parseTimeFloat64(float64(value.(int))), true
return parseTimeFloat64(float64(typedVal)), true
case int32:
return parseTimeFloat64(float64(value.(int32))), true
return parseTimeFloat64(float64(typedVal)), true
case int64:
return parseTimeFloat64(float64(value.(int64))), true
return parseTimeFloat64(float64(typedVal)), true
}
return t, false
}
40 changes: 40 additions & 0 deletions time_parse_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package humanlog

import (
"fmt"
"testing"
"time"
)

func TestTimeParseFloat64(t *testing.T) {
Expand Down Expand Up @@ -34,3 +36,41 @@ func TestTimeParseFloat64(t *testing.T) {
}
})
}

func TestTryParseFloatTime(t *testing.T) {
testTime := time.Now()

t.Run("microseconds", func(t *testing.T) {
actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.UnixMicro()))
if !ok {
t.Fatal("time not parsed")
}

if actualTime.UnixMicro() != testTime.UnixMicro() {
t.Fatalf("time not equal: %d != %d", actualTime.UnixMicro(), testTime.UnixMicro())
}
})

t.Run("milliseconds", func(t *testing.T) {
actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.UnixMilli()))
if !ok {
t.Fatal("time not parsed")
}

if actualTime.UnixMilli() != testTime.UnixMilli() {
t.Fatalf("time not equal: %d != %d", actualTime.UnixMilli(), testTime.UnixMilli())
}
})

t.Run("seconds", func(t *testing.T) {
actualTime, ok := tryParseTime(fmt.Sprintf("%d", testTime.Unix()))
if !ok {
t.Fatal("time not parsed")
}

if actualTime.Unix() != testTime.Unix() {
t.Fatalf("time not equal: %d != %d", actualTime.Unix(), testTime.Unix())
}
})

}