+14
-4
lines changedFilter options
+14
-4
lines changed Original file line number Diff line number Diff line change
@@ -11,14 +11,14 @@ import (
11
11
func TestParseRelativeTime(t *testing.T) {
12
12
const day = 24 * time.Hour
13
13
const week = 7 * day
14
-
const year = time.Duration(float64(day) * 365)
15
-
const month = time.Duration(float64(day) * 30)
16
14
17
15
testCases := []struct {
18
16
name string
19
17
input string
20
18
expectedError bool
21
19
expectedDelta time.Duration // Expected time difference from now
20
+
isMonthCase bool // Special handling for month arithmetic
21
+
isYearCase bool // Special handling for year arithmetic
22
22
}{
23
23
{
24
24
name: "now",
@@ -54,13 +54,13 @@ func TestParseRelativeTime(t *testing.T) {
54
54
name: "now-1M",
55
55
input: "now-1M",
56
56
expectedError: false,
57
-
expectedDelta: -month,
57
+
isMonthCase: true,
58
58
},
59
59
{
60
60
name: "now-1y",
61
61
input: "now-1y",
62
62
expectedError: false,
63
-
expectedDelta: -year,
63
+
isYearCase: true,
64
64
},
65
65
{
66
66
name: "now-1.5h",
@@ -96,6 +96,16 @@ func TestParseRelativeTime(t *testing.T) {
96
96
// Allow a small tolerance for execution time
97
97
diff := result.Sub(now)
98
98
assert.Less(t, diff.Abs(), 2*time.Second, "Time difference should be less than 2 seconds")
99
+
} else if tc.isMonthCase {
100
+
// For month calculations, use proper calendar arithmetic
101
+
expected := now.AddDate(0, -1, 0)
102
+
diff := result.Sub(expected)
103
+
assert.Less(t, diff.Abs(), 2*time.Second, "Time difference should be less than 2 seconds")
104
+
} else if tc.isYearCase {
105
+
// For year calculations, use proper calendar arithmetic
106
+
expected := now.AddDate(-1, 0, 0)
107
+
diff := result.Sub(expected)
108
+
assert.Less(t, diff.Abs(), 2*time.Second, "Time difference should be less than 2 seconds")
99
109
} else {
100
110
// For other relative times, compare with the expected delta from now
101
111
expected := now.Add(tc.expectedDelta)
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4