-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimeconv_test.go
147 lines (119 loc) · 3.78 KB
/
timeconv_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
package timeconv
import (
"testing"
"time"
)
func TestUpTime(tt *testing.T) {
time.Sleep(time.Second)
d := ProcUpDuration()
if d > time.Second && d <= (time.Second+10*time.Millisecond) {
tt.Logf("proc uptime: %v", d) // OK
} else {
tt.Errorf("expected up time about 1 second, but got %v", d)
return
}
}
func TestAddDate(tt *testing.T) {
loc, _ := time.LoadLocation("Asia/Shanghai")
var t, expected time.Time
t = time.Date(2019, 1, 31, 0, 0, 0, 0, loc)
expected = time.Date(2019, 2, 28, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 1, 0)
check(tt, t, expected)
t = time.Date(2019, 1, 29, 0, 0, 0, 0, loc)
expected = time.Date(2019, 2, 28, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 1, 0)
check(tt, t, expected)
t = time.Date(2000, 1, 30, 0, 0, 0, 0, loc)
expected = time.Date(2000, 2, 29, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 1, 0)
check(tt, t, expected)
t = time.Date(1900, 1, 29, 0, 0, 0, 0, loc)
expected = time.Date(1900, 2, 28, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 1, 0)
check(tt, t, expected)
t = time.Date(2020, 2, 1, 0, 0, 0, 0, loc)
expected = time.Date(2020, 3, 3, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 0, 31)
check(tt, t, expected)
t = time.Date(2020, 3, 1, 0, 0, 0, 0, loc)
expected = time.Date(2020, 6, 1, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 0, 92)
check(tt, t, expected)
t = time.Date(2020, 2, 29, 0, 0, 0, 0, loc)
expected = time.Date(2021, 2, 28, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 12, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 31, 0, 0, 0, 0, loc)
expected = time.Date(2020, 2, 29, 0, 0, 0, 0, loc)
t = AddDate(t, 0, 2, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 31, 0, 0, 0, 0, loc)
expected = time.Date(2016, 11, 30, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -37, 0)
check(tt, t, expected)
t = time.Date(2021, 1, 29, 0, 0, 0, 0, loc)
expected = time.Date(2020, 2, 29, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -11, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 30, 0, 0, 0, 0, loc)
expected = time.Date(2019, 4, 30, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -8, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 30, 0, 0, 0, 0, loc)
expected = time.Date(2019, 2, 28, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -10, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 29, 0, 0, 0, 0, loc)
expected = time.Date(2019, 4, 29, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -8, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 31, 0, 0, 0, 0, loc)
expected = time.Date(2019, 10, 31, 0, 0, 0, 0, loc)
t = AddDate(t, 0, -2, 0)
check(tt, t, expected)
t = time.Date(2019, 12, 31, 0, 0, 0, 0, loc)
expected = time.Date(2019, 10, 31, 0, 0, 0, 0, loc)
AddDateP(&t, 0, -2, 0)
check(tt, t, expected)
}
func TestDate(t *testing.T) {
loc, _ := time.LoadLocation("Asia/Shanghai")
tm := Date(2019, 12, 31)
expected := time.Date(2019, 12, 31, 0, 0, 0, 0, time.UTC)
check(t, tm, expected)
tm = Date(2019, 12, 31, loc)
expected = time.Date(2019, 12, 31, 0, 0, 0, 0, loc)
check(t, tm, expected)
}
func check(tt *testing.T, t, expected time.Time) {
if false == t.Equal(expected) {
tt.Errorf("Expected time %v, but got %v, error", expected, t)
}
}
func TestUnix(t *testing.T) {
now := time.Now()
nano := now.UnixNano()
if nano/1000 != UnixMicro(now) {
t.Errorf("%d != %d !!!", nano/1000, UnixMicro(now))
}
if nano/1000000 != UnixMilli(now) {
t.Errorf("%d != %d !!!", nano/1000000, UnixMilli(now))
}
}
func TestFormat(t *testing.T) {
now := time.Now()
test := func(s, format string) {
expect := now.Format(format)
if s != expect {
t.Errorf("format '%s', expect '%s', but got '%s'", format, expect, s)
}
}
test(YYYYMMDD(now, "-"), "2006-01-02")
test(YYMMDD(now, ","), "06,01,02")
test(HHMM(now, "."), "03.04")
test(HHMMSS(now, ":"), "03:04:05")
test(HHMMSS(now, ":", 3), "03:04:05.000")
test(HHMMSS(now, ":", 9), "03:04:05.000000000")
test(HHMMSS(now, ":", 11), "03:04:05.000000000")
}