Skip to content

Commit

Permalink
Merge pull request #116 from honeycombio/nathanleclaire.milli_timestamp
Browse files Browse the repository at this point in the history
Add support for other timestamp types
  • Loading branch information
nathanleclaire committed Oct 10, 2018
2 parents 78d628b + 0a229a9 commit 39c940a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions httime/httime.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,32 @@ func tryTimeFormats(t, intendedFormat string) time.Time {
ts = tOther
} else if tOther, err := Parse(time.UnixDate, t); err == nil {
ts = tOther
} else {
// the defaults didn't catch it, let's try a few other things
// is it all numeric? then try unix epoch times
epochInt, err := strconv.ParseInt(t, 0, 64)
if err == nil {
// it might be seconds or it might be milliseconds! Who can know!
// 10-digit numbers are seconds, 13-digit milliseconds, 16 microseconds
if len(t) == 10 {
ts = time.Unix(epochInt, 0)
} else if len(t) > 10 {
// turn it into seconds and fractional seconds
fractionalTime := t[:10] + "." + t[10:]
// then chop it into the int part and the fractional part
if epochFloat, err := strconv.ParseFloat(fractionalTime, 64); err == nil {
sec, dec := math.Modf(epochFloat)
ts = time.Unix(int64(sec), int64(dec*(1e9)))
}

}
} else {
epochFloat, err := strconv.ParseFloat(t, 64)
if err == nil {
sec, dec := math.Modf(epochFloat)
ts = time.Unix(int64(sec), int64(dec*(1e9)))
}
}
}
return ts
}
Expand Down
8 changes: 8 additions & 0 deletions httime/httime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ var tts = []testTimestamp{
expected: time.Unix(1397113200, 0),
diffThreshold: 0,
},
{
format: "",
input: "1538860697500",
tz: utc,
fieldName: "timestamp",
expected: time.Unix(1538860697, 500000000),
diffThreshold: 0,
},
}

func TestGetTimestampValid(t *testing.T) {
Expand Down

0 comments on commit 39c940a

Please sign in to comment.