forked from aws/aws-sdk-go
/
timestamp_test.go
106 lines (100 loc) · 3.17 KB
/
timestamp_test.go
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
96
97
98
99
100
101
102
103
104
105
106
// +build go1.7
package protocol
import (
"testing"
"time"
)
func TestFormatTime(t *testing.T) {
cases := map[string]struct {
formatName string
expectedOutput string
input time.Time
}{
"UnixTest1": {
formatName: UnixTimeFormatName,
expectedOutput: "946845296",
input: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
},
"UnixTest2": {
formatName: UnixTimeFormatName,
expectedOutput: "946845296.123",
input: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
"ISO8601Test1": {
formatName: ISO8601TimeFormatName,
expectedOutput: "2000-01-02T20:34:56Z",
input: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
},
"RFC822Test1": {
formatName: RFC822TimeFormatName,
expectedOutput: "Sun, 02 Jan 2000 20:34:56 GMT",
input: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
},
"ISO8601Test2": {
formatName: ISO8601TimeFormatName,
expectedOutput: "2000-01-02T20:34:56.123Z",
input: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
if e, a := c.expectedOutput, FormatTime(c.formatName, c.input); e != a {
t.Errorf("expected %s, got %s for %s format ", e, a, c.formatName)
}
})
}
}
func TestParseTime(t *testing.T) {
//input and output times are considered equal if they are equal until three decimal places
cases := map[string]struct {
formatName, input string
expectedOutput time.Time
}{
"UnixTestExponent": {
formatName: UnixTimeFormatName,
input: "1.583858715232899e9",
expectedOutput: time.Date(2020, time.March, 10, 16, 45, 15, .233e9, time.UTC),
},
"UnixTest1": {
formatName: UnixTimeFormatName,
input: "946845296.123",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
"UnixTest2": {
formatName: UnixTimeFormatName,
input: "946845296.12344",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
"UnixTest3": {
formatName: UnixTimeFormatName,
input: "946845296.1229999",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
"ISO8601Test1": {
formatName: ISO8601TimeFormatName,
input: "2000-01-02T20:34:56.123Z",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
},
"ISO8601Test2": {
formatName: ISO8601TimeFormatName,
input: "2000-01-02T20:34:56.123456789Z",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123456789e9, time.UTC),
},
"RFC822Test1": {
formatName: RFC822TimeFormatName,
input: "Sun, 2 Jan 2000 20:34:56 GMT",
expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
timeVal, err := ParseTime(c.formatName, c.input)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := c.expectedOutput, timeVal.UTC(); !e.Equal(a) {
t.Errorf("expect %v time, got %v", e, a)
}
})
}
}