-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTime_test.go
161 lines (153 loc) · 5.08 KB
/
DateTime_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
package acquiringsdk
import (
"fmt"
"testing"
"time"
)
func TestParseDate(t *testing.T) {
parameters := []struct {
input string
expected time.Time
}{
{"2023-09-20", time.Date(2023, 9, 20, 0, 0, 0, 0, time.UTC)},
{"2024-02-29", time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC)},
}
for i := range parameters {
input := parameters[i].input
expected := parameters[i].expected
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
actual, err := ParseDate(input)
if err != nil {
t.Fatalf("TestParseDate: %v", err)
}
if !areEqual(actual, expected) {
t.Errorf("expected: '%s', actual: '%s'", expected, actual)
}
})
}
}
func TestFormatDate(t *testing.T) {
parameters := []struct {
input time.Time
expected string
}{
{time.Date(2023, 9, 20, 0, 0, 0, 0, time.UTC), "2023-09-20"},
{time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC), "2024-02-29"},
{time.Date(2024, 2, 29, 0, 0, 0, 0, time.FixedZone("", 3600)), "2024-02-29"},
}
for i := range parameters {
input := parameters[i].input
expected := parameters[i].expected
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
actual := FormatDate(input)
if actual != expected {
t.Errorf("expected: '%s', actual: '%s'", expected, actual)
}
})
}
}
func TestFormatAndParseDate(t *testing.T) {
parameters := []time.Time{
time.Date(2023, 9, 20, 0, 0, 0, 0, time.UTC),
time.Date(2024, 2, 29, 0, 0, 0, 0, time.UTC),
}
for i := range parameters {
input := parameters[i]
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
formatted := FormatDate(input)
actual, err := ParseDate(formatted)
if err != nil {
t.Fatalf("TestFormatAndParseDate: %v", err)
}
if !areEqual(actual, input) {
t.Errorf("expected: '%s', actual: '%s'", input, actual)
}
})
}
}
func TestParseDateTime(t *testing.T) {
parameters := []struct {
input string
expected time.Time
}{
{"2023-10-10T08:00+02:00", time.Date(2023, 10, 10, 8, 0, 0, 0, time.FixedZone("", 7200))},
{"2023-10-10T08:00Z", time.Date(2023, 10, 10, 8, 0, 0, 0, time.UTC)},
{"2020-01-01T12:00:00Z", time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC)},
{"2023-10-10T08:00:01.234+02:00", time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.FixedZone("", 7200))},
{"2023-10-10T08:00:01.234Z", time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.UTC)},
{"2020-01-01T12:00:00.123456Z", time.Date(2020, 1, 1, 12, 0, 0, 123456000, time.UTC)},
}
for i := range parameters {
input := parameters[i].input
expected := parameters[i].expected
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
actual, err := ParseDateTime(input)
if err != nil {
t.Fatalf("TestParseDateTime: %v", err)
}
if !areEqual(actual, expected) {
t.Errorf("expected: '%s', actual: '%s'", expected, actual)
}
})
}
}
func TestFormatDateTime(t *testing.T) {
parameters := []struct {
input time.Time
expected string
}{
{time.Date(2023, 10, 10, 8, 0, 0, 0, time.FixedZone("", 7200)), "2023-10-10T08:00:00+02:00"},
{time.Date(2023, 10, 10, 8, 0, 0, 0, time.UTC), "2023-10-10T08:00:00Z"},
{time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC), "2020-01-01T12:00:00Z"},
{time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.FixedZone("", 7200)), "2023-10-10T08:00:01.234+02:00"},
{time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.UTC), "2023-10-10T08:00:01.234Z"},
{time.Date(2020, 1, 1, 12, 0, 0, 123456000, time.UTC), "2020-01-01T12:00:00.123Z"},
}
for i := range parameters {
input := parameters[i].input
expected := parameters[i].expected
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
actual := FormatDateTime(input)
if actual != expected {
t.Errorf("expected: '%s', actual: '%s'", expected, actual)
}
})
}
}
func TestFormatAndParseDateTime(t *testing.T) {
parameters := []time.Time{
time.Date(2023, 10, 10, 8, 0, 0, 0, time.FixedZone("", 7200)),
time.Date(2023, 10, 10, 8, 9, 9, 9, time.UTC),
time.Date(2020, 1, 1, 12, 0, 0, 0, time.UTC),
time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.FixedZone("", 7200)),
time.Date(2023, 10, 10, 8, 0, 1, 234000000, time.UTC),
time.Date(2020, 1, 1, 12, 0, 0, 123456000, time.UTC),
}
for i := range parameters {
input := parameters[i]
t.Run(fmt.Sprintf("applied to '%s'", input), func(t *testing.T) {
formatted := FormatDateTime(input)
actual, err := ParseDateTime(formatted)
// truncate the input nanos to millis
nanos := input.Nanosecond() / 1000000 * 1000000
expected := time.Date(input.Year(), input.Month(), input.Day(), input.Hour(), input.Minute(), input.Second(), nanos, input.Location())
if err != nil {
t.Fatalf("TestFormatAndParseDateTime: %v", err)
}
if !areEqual(actual, expected) {
t.Errorf("expected: '%s', actual: '%s'", input, actual)
}
})
}
}
func areEqual(a, b time.Time) bool {
if a.UnixNano() != b.UnixNano() {
return false
}
if a.Year() != b.Year() || a.Month() != b.Month() || a.Day() != b.Day() || a.Hour() != b.Hour() || a.Minute() != b.Minute() || a.Nanosecond() != b.Nanosecond() {
return false
}
_, offsetA := a.Zone()
_, offsetB := b.Zone()
return offsetA == offsetB
}