File: timestamp_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (142 lines) | stat: -rw-r--r-- 4,819 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//go:build go1.7
// +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),
		},
		"ISO8601Test3": {
			formatName:     ISO8601TimeFormatName,
			expectedOutput: "2000-01-02T20:34:56.123Z",
			input:          time.Date(2000, time.January, 2, 20, 34, 56, .123456e9, 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),
		},
		"ISO8601Test milliseconds": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123Z",
			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
		},
		"ISO8601Test milliseconds, no Z": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123",
			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC),
		},
		"ISO8601Test nanoseconds": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123456789Z",
			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123456789e9, time.UTC),
		},
		"ISO8601Test millisecond utc offset": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123-07:00",
			expectedOutput: time.Date(2000, time.January, 3, 3, 34, 56, .123e9, time.UTC),
		},
		"ISO8601Test millisecond positive utc offset": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123+07:00",
			expectedOutput: time.Date(2000, time.January, 2, 13, 34, 56, .123e9, time.UTC),
		},
		"ISO8601Test nanosecond utc offset": {
			formatName:     ISO8601TimeFormatName,
			input:          "2000-01-02T20:34:56.123456789-07:00",
			expectedOutput: time.Date(2000, time.January, 3, 3, 34, 56, .123456789e9, time.UTC),
		},
		"RFC822Test single digit day": {
			formatName:     RFC822TimeFormatName,
			input:          "Sun, 2 Jan 2000 20:34:56 GMT",
			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
		},
		"RFC822Test two digit day": {
			formatName:     RFC822TimeFormatName,
			input:          "Sun, 02 Jan 2000 20:34:56 GMT",
			expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC),
		},
		"RFC822Test two digit day year": {
			formatName:     RFC822TimeFormatName,
			input:          "Sun, 2 Jan 00 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)
			}
		})
	}
}