2015-05-13 12:27:00 -04:00
|
|
|
package timeutils
|
|
|
|
|
|
|
|
import (
|
2015-05-12 15:59:34 -04:00
|
|
|
"fmt"
|
2015-05-13 12:27:00 -04:00
|
|
|
"testing"
|
2015-05-12 15:59:34 -04:00
|
|
|
"time"
|
2015-05-13 12:27:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetTimestamp(t *testing.T) {
|
2015-05-12 15:59:34 -04:00
|
|
|
now := time.Now()
|
2015-05-13 12:27:00 -04:00
|
|
|
cases := []struct{ in, expected string }{
|
|
|
|
{"0", "-62167305600"}, // 0 gets parsed year 0
|
|
|
|
|
|
|
|
// Partial RFC3339 strings get parsed with second precision
|
|
|
|
{"2006-01-02T15:04:05.999999999+07:00", "1136189045"},
|
|
|
|
{"2006-01-02T15:04:05.999999999Z", "1136214245"},
|
|
|
|
{"2006-01-02T15:04:05.999999999", "1136214245"},
|
|
|
|
{"2006-01-02T15:04:05", "1136214245"},
|
|
|
|
{"2006-01-02T15:04", "1136214240"},
|
|
|
|
{"2006-01-02T15", "1136214000"},
|
|
|
|
{"2006-01-02T", "1136160000"},
|
|
|
|
{"2006-01-02", "1136160000"},
|
|
|
|
{"2006", "1136073600"},
|
|
|
|
{"2015-05-13T20:39:09Z", "1431549549"},
|
|
|
|
|
|
|
|
// unix timestamps returned as is
|
|
|
|
{"1136073600", "1136073600"},
|
|
|
|
|
2015-05-12 15:59:34 -04:00
|
|
|
// Durations
|
|
|
|
{"1m", fmt.Sprintf("%d", now.Add(-1*time.Minute).Unix())},
|
|
|
|
{"1.5h", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
|
|
|
|
{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix())},
|
|
|
|
|
2015-05-13 12:27:00 -04:00
|
|
|
// String fallback
|
|
|
|
{"invalid", "invalid"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2015-05-12 15:59:34 -04:00
|
|
|
o := GetTimestamp(c.in, now)
|
2015-05-13 12:27:00 -04:00
|
|
|
if o != c.expected {
|
|
|
|
t.Fatalf("wrong value for '%s'. expected:'%s' got:'%s'", c.in, c.expected, o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|