Skip to content
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
8 changes: 6 additions & 2 deletions pkg/timestamp/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package timestamp

import (
"fmt"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -144,11 +143,16 @@ func parseTimestamp(value string) (seconds int64, nanoseconds int64, _ error) {
if !ok {
return sec, 0, nil
}
if len(n) > 9 {
n = n[:9]
}
nsec, err := strconv.ParseInt(n, 10, 64)
if err != nil {
return sec, nsec, err
}
// should already be in nanoseconds but just in case convert n to nanoseconds
nsec = int64(float64(nsec) * math.Pow(float64(10), float64(9-len(n))))
for range 9 - len(n) {
nsec *= 10
}
return sec, nsec, nil
}
2 changes: 2 additions & 0 deletions pkg/timestamp/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestGetTimestamp(t *testing.T) {
// unix timestamps returned as is
{"1136073600", "1136073600", false},
{"1136073600.000000001", "1136073600.000000001", false},
{"1136073600.123456789123456789123", "1136073600.123456789123456789123", false},
// Durations
{"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix()), false},
{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},
Expand Down Expand Up @@ -99,6 +100,7 @@ func TestParseTimestamps(t *testing.T) {
{"1136073600.0000000010", 0, 1136073600, 1, false},
{"1136073600.0000000001", 0, 1136073600, 0, false},
{"1136073600.0000000009", 0, 1136073600, 0, false},
{"1136073600.123456789123456789123", 0, 1136073600, 123456789, false},
{"1136073600.00000001", 0, 1136073600, 10, false},
{"foo.bar", 0, 0, 0, true},
{"1136073600.bar", 0, 1136073600, 0, true},
Expand Down
Loading