-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
143 lines (136 loc) · 3.13 KB
/
main_test.go
File metadata and controls
143 lines (136 loc) · 3.13 KB
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
// Copyright Chrono Technologies LLC
// SPDX-License-Identifier: MIT
package main
import (
"testing"
"time"
)
func TestParseTimeInput(t *testing.T) {
tests := []struct {
name string
input string
wantTime time.Time
wantErr bool
}{
{
name: "with empty string",
input: "",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with invalid string",
input: "not-a-time",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 date string",
input: "1969-12-31",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 rfc3339 format",
input: "1969-12-31T23:59:59Z",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with pre-1970 iso8601 with offset",
input: "1969-12-31T18:59:59-05:00",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with just before unix epoch",
input: "1969-12-31T23:59:59.999Z",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with negative unix timestamp (seconds)",
input: "-1687689000",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with negative unix timestamp (milliseconds)",
input: "-1687689000123",
wantTime: time.Time{},
wantErr: true,
},
{
name: "with unix epoch boundary",
input: "1970-01-01T00:00:00Z",
wantTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with only date",
input: "2023-06-15",
wantTime: time.Date(2023, 6, 15, 0, 0, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with timezone-less datetime",
input: "2023-06-15 10:30:00",
wantTime: time.Date(2023, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with seconds-less datetime",
input: "2023-06-15 10:30",
wantTime: time.Date(2023, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with rfc3339 format",
input: "2025-06-15T10:30:00.123Z",
wantTime: time.Date(2025, 6, 15, 10, 30, 0, 123000000, time.UTC),
wantErr: false,
},
{
name: "with iso8601 format including timezone",
input: "2025-06-15T10:30:00Z",
wantTime: time.Date(2025, 6, 15, 10, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with iso8601 format including offset",
input: "2023-06-15T10:30:00-07:00",
wantTime: time.Date(2023, 6, 15, 17, 30, 0, 0, time.UTC),
wantErr: false,
},
{
name: "with unix timestamp (seconds)",
input: "1687689000",
wantTime: time.Unix(1687689000, 0),
wantErr: false,
},
{
name: "with unix timestamp (milliseconds)",
input: "1687689000123",
wantTime: time.UnixMilli(1687689000123),
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
parsed, err := parseTimeInput(test.input)
if test.wantErr {
if err == nil {
t.Errorf("expected error but got none")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if !parsed.Equal(test.wantTime) {
t.Errorf("got: %v, want: %v", parsed, test.wantTime)
}
})
}
}