summaryrefslogtreecommitdiff
path: root/pkg/timetype/timestamp_test.go
blob: 0fffb85a9bdf036a00f219d22babd81b901c29c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package timetype

// code adapted from https://github.com/moby/moby/blob/master/api/types/time/timestamp.go

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

func TestGetTimestamp(t *testing.T) {
	now := time.Now().In(time.UTC)
	cases := []struct {
		in, expected string
		expectedErr  bool
	}{
		// Partial RFC3339 strings get parsed with second precision
		{"2006-01-02T15:04:05.999999999+07:00", "1136189045.999999999", false},
		{"2006-01-02T15:04:05.999999999Z", "1136214245.999999999", false},
		{"2006-01-02T15:04:05.999999999", "1136214245.999999999", false},
		{"2006-01-02T15:04:05Z", "1136214245.000000000", false},
		{"2006-01-02T15:04:05", "1136214245.000000000", false},
		{"2006-01-02T15:04:0Z", "", true},
		{"2006-01-02T15:04:0", "", true},
		{"2006-01-02T15:04Z", "1136214240.000000000", false},
		{"2006-01-02T15:04+00:00", "1136214240.000000000", false},
		{"2006-01-02T15:04-00:00", "1136214240.000000000", false},
		{"2006-01-02T15:04", "1136214240.000000000", false},
		{"2006-01-02T15:0Z", "", true},
		{"2006-01-02T15:0", "", true},
		{"2006-01-02T15Z", "1136214000.000000000", false},
		{"2006-01-02T15+00:00", "1136214000.000000000", false},
		{"2006-01-02T15-00:00", "1136214000.000000000", false},
		{"2006-01-02T15", "1136214000.000000000", false},
		{"2006-01-02T1Z", "1136163600.000000000", false},
		{"2006-01-02T1", "1136163600.000000000", false},
		{"2006-01-02TZ", "", true},
		{"2006-01-02T", "", true},
		{"2006-01-02+00:00", "1136160000.000000000", false},
		{"2006-01-02-00:00", "1136160000.000000000", false},
		{"2006-01-02-00:01", "1136160060.000000000", false},
		{"2006-01-02Z", "1136160000.000000000", false},
		{"2006-01-02", "1136160000.000000000", false},
		{"2015-05-13T20:39:09Z", "1431549549.000000000", false},

		// unix timestamps returned as is
		{"1136073600", "1136073600", false},
		{"1136073600.000000001", "1136073600.000000001", 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},
		{"1h30m", fmt.Sprintf("%d", now.Add(-90*time.Minute).Unix()), false},

		{"invalid", "", true},
		{"", "", true},
	}

	for _, c := range cases {
		o, err := GetTimestamp(c.in, now)
		if o != c.expected ||
			(err == nil && c.expectedErr) ||
			(err != nil && !c.expectedErr) {
			t.Errorf("wrong value for '%s'. expected:'%s' got:'%s' with error: `%s`", c.in, c.expected, o, err)
			t.Fail()
		}
	}
}

func TestParseTimestamps(t *testing.T) {
	cases := []struct {
		in                        string
		def, expectedS, expectedN int64
		expectedErr               bool
	}{
		// unix timestamps
		{"1136073600", 0, 1136073600, 0, false},
		{"1136073600.000000001", 0, 1136073600, 1, false},
		{"1136073600.0000000010", 0, 1136073600, 1, false},
		{"1136073600.00000001", 0, 1136073600, 10, false},
		{"foo.bar", 0, 0, 0, true},
		{"1136073600.bar", 0, 1136073600, 0, true},
		{"", -1, -1, 0, false},
	}

	for _, c := range cases {
		s, n, err := ParseTimestamps(c.in, c.def)
		if s != c.expectedS ||
			n != c.expectedN ||
			(err == nil && c.expectedErr) ||
			(err != nil && !c.expectedErr) {
			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)
			t.Fail()
		}
	}
}