forked from rclone/rclone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsetime_test.go
178 lines (166 loc) · 5.36 KB
/
parsetime_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
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
package fs
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Check it satisfies the interfaces
var (
_ Flagger = (*Time)(nil)
_ FlaggerNP = Time{}
)
func TestParseTime(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
oldTimeNowFunc := timeNowFunc
timeNowFunc = func() time.Time { return now }
defer func() { timeNowFunc = oldTimeNowFunc }()
for _, test := range []struct {
in string
want time.Time
err bool
}{
{"", time.Time{}, true},
{"1ms", now.Add(-time.Millisecond), false},
{"1s", now.Add(-time.Second), false},
{"1", now.Add(-time.Second), false},
{"1m", now.Add(-time.Minute), false},
{"1.5m", now.Add(-(3 * time.Minute) / 2), false},
{"1h", now.Add(-time.Hour), false},
{"1d", now.Add(-time.Hour * 24), false},
{"1w", now.Add(-time.Hour * 24 * 7), false},
{"1M", now.Add(-time.Hour * 24 * 30), false},
{"1y", now.Add(-time.Hour * 24 * 365), false},
{"1.5y", now.Add(-time.Hour * 24 * 365 * 3 / 2), false},
{"-1.5y", now.Add(time.Hour * 24 * 365 * 3 / 2), false},
{"-1s", now.Add(time.Second), false},
{"-1", now.Add(time.Second), false},
{"0", now, false},
{"100", now.Add(-100 * time.Second), false},
{"-100", now.Add(100 * time.Second), false},
{"1.s", now.Add(-time.Second), false},
{"1x", time.Time{}, true},
{"-1x", time.Time{}, true},
{"off", time.Time{}, false},
{"1h2m3s", now.Add(-(time.Hour + 2*time.Minute + 3*time.Second)), false},
{"2001-02-03", time.Date(2001, 2, 3, 0, 0, 0, 0, time.Local), false},
{"2001-02-03 10:11:12", time.Date(2001, 2, 3, 10, 11, 12, 0, time.Local), false},
{"2001-08-03 10:11:12", time.Date(2001, 8, 3, 10, 11, 12, 0, time.Local), false},
{"2001-02-03T10:11:12", time.Date(2001, 2, 3, 10, 11, 12, 0, time.Local), false},
{"2001-02-03T10:11:12.123Z", time.Date(2001, 2, 3, 10, 11, 12, 123000000, time.UTC), false},
{"2001-02-03T10:11:12.123+00:00", time.Date(2001, 2, 3, 10, 11, 12, 123000000, time.UTC), false},
} {
parsedTime, err := ParseTime(test.in)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.True(t, test.want.Equal(parsedTime), "%v should be parsed as %v instead of %v", test.in, test.want, parsedTime)
}
}
func TestTimeString(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
oldTimeNowFunc := timeNowFunc
timeNowFunc = func() time.Time { return now }
defer func() { timeNowFunc = oldTimeNowFunc }()
for _, test := range []struct {
in time.Time
want string
}{
{now, "2020-09-05T08:15:05.00000025Z"},
{time.Date(2021, 8, 5, 8, 15, 5, 0, time.UTC), "2021-08-05T08:15:05Z"},
{time.Time{}, "off"},
} {
got := Time(test.in).String()
assert.Equal(t, test.want, got)
// Test the reverse
reverse, err := ParseTime(test.want)
assert.NoError(t, err)
assert.Equal(t, test.in, reverse)
}
}
func TestTimeScan(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
oldTimeNowFunc := timeNowFunc
timeNowFunc = func() time.Time { return now }
defer func() { timeNowFunc = oldTimeNowFunc }()
for _, test := range []struct {
in string
want Time
}{
{"17m", Time(now.Add(-17 * time.Minute))},
{"-12h", Time(now.Add(12 * time.Hour))},
{"0", Time(now)},
{"off", Time(time.Time{})},
{"2022-03-26T17:48:19Z", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC))},
{"2022-03-26 17:48:19", Time(time.Date(2022, 03, 26, 17, 48, 19, 0, time.Local))},
} {
var got Time
n, err := fmt.Sscan(test.in, &got)
require.NoError(t, err)
assert.Equal(t, 1, n)
assert.Equal(t, test.want, got)
}
}
func TestParseTimeUnmarshalJSON(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
oldTimeNowFunc := timeNowFunc
timeNowFunc = func() time.Time { return now }
defer func() { timeNowFunc = oldTimeNowFunc }()
for _, test := range []struct {
in string
want time.Time
err bool
}{
{`""`, time.Time{}, true},
{"0", time.Time{}, true},
{"1", time.Time{}, true},
{"1", time.Time{}, true},
{`"2022-03-26T17:48:19Z"`, time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC), false},
{`"0"`, now, false},
{`"1ms"`, now.Add(-time.Millisecond), false},
{`"1s"`, now.Add(-time.Second), false},
{`"1"`, now.Add(-time.Second), false},
{`"1m"`, now.Add(-time.Minute), false},
{`"1h"`, now.Add(-time.Hour), false},
{`"-1h"`, now.Add(time.Hour), false},
{`"1d"`, now.Add(-time.Hour * 24), false},
{`"1w"`, now.Add(-time.Hour * 24 * 7), false},
{`"1M"`, now.Add(-time.Hour * 24 * 30), false},
{`"1y"`, now.Add(-time.Hour * 24 * 365), false},
{`"off"`, time.Time{}, false},
{`"error"`, time.Time{}, true},
{"error", time.Time{}, true},
} {
var parsedTime Time
err := json.Unmarshal([]byte(test.in), &parsedTime)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, Time(test.want), parsedTime, test.in)
}
}
func TestParseTimeMarshalJSON(t *testing.T) {
for _, test := range []struct {
in time.Time
want string
err bool
}{
{time.Time{}, `"0001-01-01T00:00:00Z"`, false},
{time.Date(2022, 03, 26, 17, 48, 19, 0, time.UTC), `"2022-03-26T17:48:19Z"`, false},
} {
gotBytes, err := json.Marshal(test.in)
got := string(gotBytes)
if test.err {
require.Error(t, err, test.in)
} else {
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, got, test.in)
}
}