forked from go-chrono/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoffset_time_test.go
205 lines (175 loc) Β· 5.86 KB
/
offset_time_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package chrono_test
import (
"fmt"
"testing"
"github.com/go-chrono/chrono"
)
func TestOffsetTime(t *testing.T) {
time := chrono.OffsetTimeOf(12, 30, 59, 12345678, 2, 30)
hour, min, sec := time.Clock()
if hour != 12 {
t.Errorf("time.Clock() hour = %d, want 12", hour)
}
if min != 30 {
t.Errorf("time.Clock() min = %d, want 30", min)
}
if sec != 59 {
t.Errorf("time.Clock() sec = %d, want 59", sec)
}
if nsec := time.Nanosecond(); nsec != 12345678 {
t.Errorf("time.Nanosecond() = %d, want 12345678", nsec)
}
expectedOffset := chrono.OffsetOf(2, 30)
if offset := time.Offset(); offset != expectedOffset {
t.Errorf("time.Offset() = %s, want %s", offset, expectedOffset)
}
}
func TestOfTimeOffset(t *testing.T) {
expectedLocalTime := chrono.LocalTimeOf(12, 30, 59, 12345678)
expectedOffset := chrono.OffsetOf(3, 30)
offsetTime := chrono.OfTimeOffset(expectedLocalTime, expectedOffset)
localTime := offsetTime.Local()
offset := offsetTime.Offset()
if localTime.Compare(expectedLocalTime) != 0 {
t.Errorf("LocalTime = %s, want %s", localTime, expectedLocalTime)
}
if offset != expectedOffset {
t.Errorf("Offset = %s, want %s", offset, expectedOffset)
}
}
func TestOffsetTime_String(t *testing.T) {
for _, tt := range []struct {
name string
time chrono.OffsetTime
expected string
}{
{"simple", chrono.OffsetTimeOf(9, 0, 0, 0, 2, 30), "09:00:00+02:30"},
{"nanoseconds", chrono.OffsetTimeOf(9, 0, 0, 12345678, 2, 30), "09:00:00.012345678+02:30"},
} {
t.Run(tt.name, func(t *testing.T) {
if output := tt.time.String(); output != tt.expected {
t.Errorf("LocalTime.String() = %s, want %s", output, tt.expected)
}
})
}
}
func TestOffsetTime_BusinessHour(t *testing.T) {
time := chrono.OffsetTimeOf(25, 0, 0, 0, 2, 30)
if hour := time.BusinessHour(); hour != 25 {
t.Errorf("time.Hour() = %d, want 25", hour)
}
if hour, _, _ := time.Clock(); hour != 1 {
t.Errorf("time.Hour() = %d, want 1", hour)
}
}
func TestOffsetTime_Sub(t *testing.T) {
for _, tt := range []struct {
t1 chrono.OffsetTime
t2 chrono.OffsetTime
diff chrono.Extent
}{
{chrono.OffsetTimeOf(12, 0, 0, 0, 0, 0), chrono.OffsetTimeOf(6, 0, 0, 0, 0, 0), 6 * chrono.Hour},
{chrono.OffsetTimeOf(12, 0, 0, 22, 0, 0), chrono.OffsetTimeOf(12, 0, 0, 40, 0, 0), -18 * chrono.Nanosecond},
{chrono.OffsetTimeOf(12, 0, 0, 0, 2, 30), chrono.OffsetTimeOf(6, 0, 0, 0, 1, 0), 4*chrono.Hour + 30*chrono.Minute},
{chrono.OffsetTimeOf(12, 0, 0, 0, -2, 30), chrono.OffsetTimeOf(6, 0, 0, 0, -1, 0), 7*chrono.Hour + 30*chrono.Minute},
} {
t.Run(fmt.Sprintf("%s - %s", tt.t1, tt.t2), func(t *testing.T) {
if d := tt.t1.Sub(tt.t2); d != tt.diff {
t.Errorf("t1.Sub(t2) = %v, want %v", d, tt.diff)
}
})
}
}
func TestOffsetTime_Add(t *testing.T) {
for _, tt := range []struct {
t chrono.OffsetTime
e chrono.Extent
expected chrono.OffsetTime
}{
{chrono.OffsetTimeOf(12, 0, 0, 0, 0, 0), 29 * chrono.Minute, chrono.OffsetTimeOf(12, 29, 0, 0, 0, 0)},
{chrono.OffsetTimeOf(14, 45, 0, 0, 0, 0), -22 * chrono.Minute, chrono.OffsetTimeOf(14, 23, 0, 0, 0, 0)},
{chrono.OffsetTimeOf(5, 0, 0, 0, 0, 0), -7 * chrono.Hour, chrono.OffsetTimeOf(22, 0, 0, 0, 0, 0)},
{chrono.OffsetTimeOf(5, 0, 0, 0, 0, 0), -31 * chrono.Hour, chrono.OffsetTimeOf(22, 0, 0, 0, 0, 0)},
} {
t.Run(fmt.Sprintf("%s + %v", tt.t, tt.e), func(t *testing.T) {
if ok := tt.t.CanAdd(tt.e); !ok {
t.Error("t.CanAdd(e) = false, want true")
}
if added := tt.t.Add(tt.e); added.Compare(tt.expected) != 0 {
t.Errorf("t.Add(e) = %s, want %s", added, tt.expected)
}
})
}
for _, tt := range []struct {
name string
t chrono.OffsetTime
e chrono.Extent
}{
{"invalid duration", chrono.OffsetTimeOf(0, 0, 0, 0, 0, 0), 200 * chrono.Hour},
{"invalid time", chrono.OffsetTimeOf(90, 0, 0, 0, 0, 0), 20 * chrono.Hour},
} {
t.Run(tt.name, func(t *testing.T) {
if ok := tt.t.CanAdd(tt.e); ok {
t.Error("t.CanAdd(e) = true, want false")
}
func() {
defer func() {
if r := recover(); r == nil {
t.Error("expecting panic that didn't occur")
}
}()
tt.t.Add(tt.e)
}()
})
}
}
func TestOffsetTime_Split(t *testing.T) {
offsetTime := chrono.OffsetTimeOf(12, 30, 59, 12345678, 3, 30)
localTime := offsetTime.Local()
offset := offsetTime.Offset()
expectedLocalTime := chrono.LocalTimeOf(12, 30, 59, 12345678)
expectedOffset := chrono.OffsetOf(3, 30)
if localTime.Compare(expectedLocalTime) != 0 {
t.Errorf("LocalTime = %s, want %s", localTime, expectedLocalTime)
}
if offset != expectedOffset {
t.Errorf("Offset = %s, want %s", offset, expectedOffset)
}
}
func TestOffsetTime_UTC(t *testing.T) {
offsetTime := chrono.OffsetTimeOf(12, 30, 59, 12345678, 3, 30)
utc := offsetTime.UTC()
localTime := utc.Local()
offset := utc.Offset()
expectedLocalTime := chrono.LocalTimeOf(9, 0, 59, 12345678)
if localTime.Compare(expectedLocalTime) != 0 {
t.Errorf("time.UTC().Split() time = %s, want %s", localTime, expectedLocalTime)
}
if offset != chrono.UTC {
t.Errorf("time.UTC().Split() offset = %s, want %s", localTime, chrono.UTC)
}
}
func TestOffsetTime_In(t *testing.T) {
originalTime := chrono.OffsetTimeOf(12, 30, 0, 0, 3, 30)
for _, tt := range []struct {
name string
offset chrono.Offset
expected chrono.LocalTime
}{
{"UTC", chrono.UTC, chrono.LocalTimeOf(9, 0, 0, 0)},
{"positive", chrono.OffsetOf(5, 30), chrono.LocalTimeOf(14, 30, 0, 0)},
{"negative", chrono.OffsetOf(-5, 30), chrono.LocalTimeOf(3, 30, 0, 0)},
} {
t.Run(tt.name, func(t *testing.T) {
inOffset := originalTime.In(tt.offset)
localTime := inOffset.Local()
offset := inOffset.Offset()
if localTime.Compare(tt.expected) != 0 {
t.Errorf("time.In(%s).Split() time = %s, want %s", tt.offset, localTime, tt.expected)
}
if offset != tt.offset {
t.Errorf("time.In(%s).Split() offset = %s, want %s", tt.offset, localTime, tt.offset)
}
})
}
}