3
// code adapted from https://github.com/moby/moby/blob/master/api/types/time/timestamp.go
11
func TestGetTimestamp(t *testing.T) {
12
now := time.Now().In(time.UTC)
17
// Partial RFC3339 strings get parsed with second precision
18
{"2006-01-02T15:04:05.999999999+07:00", "1136189045.999999999", false},
19
{"2006-01-02T15:04:05.999999999Z", "1136214245.999999999", false},
20
{"2006-01-02T15:04:05.999999999", "1136214245.999999999", false},
21
{"2006-01-02T15:04:05Z", "1136214245.000000000", false},
22
{"2006-01-02T15:04:05", "1136214245.000000000", false},
23
{"2006-01-02T15:04:0Z", "", true},
24
{"2006-01-02T15:04:0", "", true},
25
{"2006-01-02T15:04Z", "1136214240.000000000", false},
26
{"2006-01-02T15:04+00:00", "1136214240.000000000", false},
27
{"2006-01-02T15:04-00:00", "1136214240.000000000", false},
28
{"2006-01-02T15:04", "1136214240.000000000", false},
29
{"2006-01-02T15:0Z", "", true},
30
{"2006-01-02T15:0", "", true},
31
{"2006-01-02T15Z", "1136214000.000000000", false},
32
{"2006-01-02T15+00:00", "1136214000.000000000", false},
33
{"2006-01-02T15-00:00", "1136214000.000000000", false},
34
{"2006-01-02T15", "1136214000.000000000", false},
35
{"2006-01-02T1Z", "1136163600.000000000", false},
36
{"2006-01-02T1", "1136163600.000000000", false},
37
{"2006-01-02TZ", "", true},
38
{"2006-01-02T", "", true},
39
{"2006-01-02+00:00", "1136160000.000000000", false},
40
{"2006-01-02-00:00", "1136160000.000000000", false},
41
{"2006-01-02-00:01", "1136160060.000000000", false},
42
{"2006-01-02Z", "1136160000.000000000", false},
43
{"2006-01-02", "1136160000.000000000", false},
44
{"2015-05-13T20:39:09Z", "1431549549.000000000", false},
46
// unix timestamps returned as is
47
{"1136073600", "1136073600", false},
48
{"1136073600.000000001", "1136073600.000000001", false},
50
{"1m", strconv.FormatInt(now.Add(-1*time.Minute).Unix(), 10), false},
51
{"1.5h", strconv.FormatInt(now.Add(-90*time.Minute).Unix(), 10), false},
52
{"1h30m", strconv.FormatInt(now.Add(-90*time.Minute).Unix(), 10), false},
54
{"invalid", "", true},
58
for _, c := range cases {
59
o, err := GetTimestamp(c.in, now)
61
(err == nil && c.expectedErr) ||
62
(err != nil && !c.expectedErr) {
63
t.Errorf("wrong value for '%s'. expected:'%s' got:'%s' with error: `%s`", c.in, c.expected, o, err)
69
func TestParseTimestamps(t *testing.T) {
72
def, expectedS, expectedN int64
76
{"1136073600", 0, 1136073600, 0, false},
77
{"1136073600.000000001", 0, 1136073600, 1, false},
78
{"1136073600.0000000010", 0, 1136073600, 1, false},
79
{"1136073600.00000001", 0, 1136073600, 10, false},
80
{"foo.bar", 0, 0, 0, true},
81
{"1136073600.bar", 0, 1136073600, 0, true},
82
{"", -1, -1, 0, false},
85
for _, c := range cases {
86
s, n, err := ParseTimestamps(c.in, c.def)
87
if s != c.expectedS ||
89
(err == nil && c.expectedErr) ||
90
(err != nil && !c.expectedErr) {
91
t.Errorf("wrong values for input `%s` with default `%d` expected:'%d'seconds and `%d`nanosecond got:'%d'seconds and `%d`nanoseconds with error: `%s`", c.in, c.def, c.expectedS, c.expectedN, s, n, err)