File size: 5,018 Bytes
fea99b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package datetime

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestParseRFC9557(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		wantTime time.Time
		wantErr  bool
	}{
		{
			name:     "RFC9557 with America/New_York summer time",
			input:    "2021-07-01T12:34:56-04:00[America/New_York]",
			wantTime: time.Date(2021, 7, 1, 12, 34, 56, 0, MustLoadLocation(t, "America/New_York")),
			wantErr:  false,
		},
		{
			name:     "RFC9557 with America/New_York winter time",
			input:    "2021-12-01T12:34:56-05:00[America/New_York]",
			wantTime: time.Date(2021, 12, 1, 12, 34, 56, 0, MustLoadLocation(t, "America/New_York")),
			wantErr:  false,
		},
		{
			name:     "RFC9557 with Europe/Berlin",
			input:    "2021-07-01T18:34:56+02:00[Europe/Berlin]",
			wantTime: time.Date(2021, 7, 1, 18, 34, 56, 0, MustLoadLocation(t, "Europe/Berlin")),
			wantErr:  false,
		},
		{
			name:     "RFC9557 with Asia/Tokyo",
			input:    "2021-07-01T21:34:56+09:00[Asia/Tokyo]",
			wantTime: time.Date(2021, 7, 1, 21, 34, 56, 0, MustLoadLocation(t, "Asia/Tokyo")),
			wantErr:  false,
		},
		{
			name:     "RFC9557 with UTC timezone",
			input:    "2021-07-01T16:34:56Z[UTC]",
			wantTime: time.Date(2021, 7, 1, 16, 34, 56, 0, time.UTC),
			wantErr:  false,
		},
		{
			name:     "RFC9557 with fractional seconds",
			input:    "2021-07-01T12:34:56.123456789-04:00[America/New_York]",
			wantTime: time.Date(2021, 7, 1, 12, 34, 56, 123456789, MustLoadLocation(t, "America/New_York")),
			wantErr:  false,
		},

		{
			name:     "standard RFC3339 without timezone suffix",
			input:    "2021-07-01T16:34:56Z",
			wantTime: time.Date(2021, 7, 1, 16, 34, 56, 0, time.UTC),
			wantErr:  false,
		},
		{
			name:     "standard RFC3339 with offset, no suffix",
			input:    "2021-07-01T12:34:56-04:00",
			wantTime: time.Date(2021, 7, 1, 12, 34, 56, 0, time.FixedZone("", -4*3600)),
			wantErr:  false,
		},
		{
			name:     "ISO8601 with fractional seconds",
			input:    "2156-10-05T18:36:46.924Z",
			wantTime: time.Date(2156, 10, 5, 18, 36, 46, 924000000, time.UTC),
			wantErr:  false,
		},
		{
			name:     "ISO8601 format without timezone suffix",
			input:    "2021-07-01T16:34:56Z",
			wantTime: time.Date(2021, 7, 1, 16, 34, 56, 0, time.UTC),
			wantErr:  false,
		},
		{
			name:     "ISO8601 with milliseconds",
			input:    "2021-07-01T16:34:56.123Z",
			wantTime: time.Date(2021, 7, 1, 16, 34, 56, 123000000, time.UTC),
			wantErr:  false,
		},
		{
			name:     "ISO8601 with microseconds",
			input:    "2021-07-01T16:34:56.123456Z",
			wantTime: time.Date(2021, 7, 1, 16, 34, 56, 123456000, time.UTC),
			wantErr:  false,
		},

		// Error cases
		{
			name:     "invalid timezone in suffix",
			input:    "2021-07-01T12:34:56-04:00[Invalid/Timezone]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "malformed timestamp",
			input:    "invalid-timestamp[America/New_York]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "missing closing bracket",
			input:    "2021-07-01T12:34:56-04:00[America/New_York",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "missing opening bracket",
			input:    "2021-07-01T12:34:56-04:00America/New_York]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "empty timezone suffix",
			input:    "2021-07-01T12:34:56-04:00[]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "brackets not at end",
			input:    "2021-07-01T12:34:56-04:00[America/New_York]extra",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "nested brackets - malformed",
			input:    "2021-07-01T12:34:56-04:00[America/[nested]New_York]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "multiple bracket pairs",
			input:    "2021-07-01T12:34:56-04:00[first][America/New_York]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "only opening bracket",
			input:    "2021-07-01T12:34:56-04:00[America/New_York",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "only closing bracket",
			input:    "2021-07-01T12:34:56-04:00America/New_York]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "empty string",
			input:    "",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "very short timestamp with brackets",
			input:    "2021[UTC]",
			wantTime: time.Time{},
			wantErr:  true,
		},
		{
			name:     "timezone with special characters",
			input:    "2021-07-01T12:34:56-04:00[America/New_York-Test]",
			wantTime: time.Time{},
			wantErr:  true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := Parse(tt.input)

			if tt.wantErr {
				assert.Error(t, err, "expected error for input: %s", tt.input)
				return
			}

			assert.NoError(t, err, "expected no error for input: %s, got: %v", tt.input, err)
			assert.True(t, got.Equal(tt.wantTime), "parsed time mismatch: got %v, want %v", got, tt.wantTime)
		})
	}
}