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
|
package timefmt
import (
"testing"
"time"
)
func TestParseAtRelativeValues(t *testing.T) {
now := time.Date(2026, 3, 3, 15, 4, 5, 0, time.FixedZone("EET", 2*3600))
today, err := ParseAt("today", now)
if err != nil {
t.Fatalf("ParseAt(today) error = %v", err)
}
wantToday := time.Date(2026, 3, 3, 0, 0, 0, 0, now.Location())
if !today.Equal(wantToday) {
t.Fatalf("ParseAt(today) = %v, want %v", today, wantToday)
}
yesterday, err := ParseAt("yesterday", now)
if err != nil {
t.Fatalf("ParseAt(yesterday) error = %v", err)
}
wantYesterday := time.Date(2026, 3, 2, 0, 0, 0, 0, now.Location())
if !yesterday.Equal(wantYesterday) {
t.Fatalf("ParseAt(yesterday) = %v, want %v", yesterday, wantYesterday)
}
}
func TestParseUnixTimestamp(t *testing.T) {
got, err := ParseAt("1714424400", time.Now())
if err != nil {
t.Fatalf("ParseAt(unix) error = %v", err)
}
want := time.Unix(1714424400, 0)
if !got.Equal(want) {
t.Fatalf("ParseAt(unix) = %v, want %v", got, want)
}
}
func TestParseISOValues(t *testing.T) {
loc := time.FixedZone("EET", 2*3600)
now := time.Date(2026, 3, 3, 12, 0, 0, 0, loc)
tests := []struct {
name string
input string
want time.Time
}{
{
name: "date only",
input: "2024-01-15",
want: time.Date(2024, 1, 15, 0, 0, 0, 0, loc),
},
{
name: "datetime minutes",
input: "2024-01-15T09:30",
want: time.Date(2024, 1, 15, 9, 30, 0, 0, loc),
},
{
name: "datetime with seconds and space",
input: "2024-01-15 09:30:45",
want: time.Date(2024, 1, 15, 9, 30, 45, 0, loc),
},
{
name: "rfc3339",
input: "2024-01-15T09:30:00Z",
want: time.Date(2024, 1, 15, 9, 30, 0, 0, time.UTC),
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got, err := ParseAt(test.input, now)
if err != nil {
t.Fatalf("ParseAt(%q) error = %v", test.input, err)
}
if !got.Equal(test.want) {
t.Fatalf("ParseAt(%q) = %v, want %v", test.input, got, test.want)
}
})
}
}
func TestParseInvalidValues(t *testing.T) {
inputs := []string{
"",
" ",
"banana",
"2024-99-99",
}
for _, input := range inputs {
input := input
t.Run(input, func(t *testing.T) {
t.Parallel()
if _, err := ParseAt(input, time.Now()); err == nil {
t.Fatalf("ParseAt(%q) error = nil, want error", input)
}
})
}
}
func FuzzParseAt(f *testing.F) {
seeds := []string{
"today",
"yesterday",
"2024-01-15",
"2024-01-15T09:30",
"1714424400",
"banana",
"",
}
for _, seed := range seeds {
f.Add(seed)
}
now := time.Date(2026, 3, 3, 12, 0, 0, 0, time.Local)
f.Fuzz(func(t *testing.T, input string) {
_, _ = ParseAt(input, now)
})
}
|