-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoneoffset_test.go
104 lines (90 loc) · 2.57 KB
/
zoneoffset_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
package main
import (
"testing"
"time"
)
func TestParseZoneOffset(t *testing.T) {
offset, err := ParseZoneOffset("+08:00")
if err != nil {
t.Errorf("want error %v, got error %v", nil, err)
} else {
want := 8*60*60 + 0*60
if offset != want {
t.Errorf("got %d, want %d", offset, want)
}
}
offset, err = ParseZoneOffset("-01:30")
if err != nil {
t.Errorf("want error %v, got error %v", nil, err)
} else {
want := -(1*60*60 + 30*60)
if offset != want {
t.Errorf("got %d, want %d", offset, want)
}
}
offset, err = ParseZoneOffset("\u221251:59")
if err != nil {
t.Errorf("want error %v, got error %v", nil, err)
} else {
want := -(51*60*60 + 59*60)
if offset != want {
t.Errorf("got %d, want %d", offset, want)
}
}
_, err = ParseZoneOffset("-0030")
if err != ErrZoneOffsetInvalid {
t.Errorf("want error %v, got error %v", ErrZoneOffsetInvalid, err)
}
_, err = ParseZoneOffset("00:30")
if err != ErrZoneOffsetInvalid {
t.Errorf("want error %v, got error %v", ErrZoneOffsetInvalid, err)
}
_, err = ParseZoneOffset("+08:60")
if err != ErrZoneOffsetInvalid {
t.Errorf("want error %v, got error %v", ErrZoneOffsetInvalid, err)
}
_, err = ParseZoneOffset("+08:-6")
if err != ErrZoneOffsetInvalid {
t.Errorf("want error %v, got error %v", ErrZoneOffsetInvalid, err)
}
offset, err = ParseZoneOffset("+08:00")
if err != nil {
t.Errorf("want error %v, got error %v", nil, err)
} else {
loc := time.FixedZone("UTC "+FormatZoneOffset(offset), offset)
time := time.Date(2020, time.November, 8, 23, 9, 0, 0, loc).Unix()
want := int64(1604848140)
if time != want {
t.Errorf("got %d, want %d", time, want)
}
}
offset, err = ParseZoneOffset("-00:30")
if err != nil {
t.Errorf("want error %v, got error %v", nil, err)
} else {
loc := time.FixedZone("UTC "+FormatZoneOffset(offset), offset)
time := time.Date(2020, time.November, 8, 14, 39, 0, 0, loc).Unix()
want := int64(1604848140)
if time != want {
t.Errorf("got %d, want %d", time, want)
}
}
}
func TestFormatZoneOffset(t *testing.T) {
want, got := "+06:06", FormatZoneOffset(6*60*60+6*60)
if want != got {
t.Fatalf("got offset %v, want offset %v", got, want)
}
want, got = "\u221212:15", FormatZoneOffset(-(12*60*60 + 15*60))
if want != got {
t.Fatalf("got offset %v, want offset %v", got, want)
}
want, got = "\u00B100:00", FormatZoneOffset(-(0*60*60 + 0*60))
if want != got {
t.Fatalf("got offset %v, want offset %v", got, want)
}
want, got = "+00:01", FormatZoneOffset(0*60*60+1*60)
if want != got {
t.Fatalf("got offset %v, want offset %v", got, want)
}
}