forked from zaihui/go-hutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
77 lines (66 loc) · 2.18 KB
/
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
package hutils
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParseTime(t *testing.T) {
datetimeStr := "2021-09-30T12:00:00"
datetime, err := DefaultParseTime(datetimeStr)
assert.Nil(t, err)
assert.Equal(t, time.Duration(0), datetime.Sub(Time(2021, 9, 30, 12, 0)))
dateStr := "2021-09-30"
date, err := ParseTime(DateLayout, dateStr)
assert.Nil(t, err)
assert.Equal(t, time.Duration(0), date.Sub(Time(2021, 9, 30, 0, 0)))
}
func TestParseTimePeriod(t *testing.T) {
var start, end string
_, err := ParseDateTimePeriod(start, end)
assert.NotNil(t, err)
start = "2021-09-30T12:00:00"
end = "2021-10-07T20:00:00"
period, err := ParseDateTimePeriod(start, end)
assert.Nil(t, err)
assert.Equal(t, Time(2021, 9, 30, 12, 0), period.Start)
assert.Equal(t, Time(2021, 10, 7, 20, 0), period.End)
}
func TestGetDayStartAndLatest(t *testing.T) {
now := time.Now()
start, end := GetDayStartAndLatest(now)
assert.Equal(t, Time(now.Year(), now.Month(), now.Day(), 0, 0), start)
assert.Equal(t, time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 59, time.Local), end)
}
func TestDiffDay(t *testing.T) {
begin := time.Now()
end := begin.AddDate(0, 0, 3)
diffDay := DiffDay(begin, end)
assert.Equal(t, 3, diffDay)
diffDay = DiffDay(end, begin)
assert.Equal(t, 3, diffDay)
diffDay = DiffDay(begin, begin)
assert.Equal(t, 0, diffDay)
}
func TestDiffDayWithLayout(t *testing.T) {
begin := time.Now()
end := begin.AddDate(0, 0, 3)
diffDay, err := DiffDayWithLayout(begin.Format(DateTimeLayout), end.Format(DateTimeLayout), DateTimeLayout)
assert.Nil(t, err)
assert.Equal(t, 3, diffDay)
_, err = DiffDayWithLayout(begin.Format(DateTimeLayout), end.Format(DateTimeLayout), DateLayout)
assert.Error(t, err)
}
func TestISO8601Local(t *testing.T) {
datetime, err := ParseTime(ISO8601Local, "2022-01-01 10:00:00")
assert.NoError(t, err)
assert.Equal(t, Time(2022, 1, 1, 10, 0), datetime)
}
func TestDateTimeLayoutChinese(t *testing.T) {
datetime, err := ParseTime(DateTimeLayoutChinese, "2022年01月01日 10时00分00秒")
assert.NoError(t, err)
assert.Equal(t, Time(2022, 1, 1, 10, 0), datetime)
}
func TestShortcut(t *testing.T) {
_ = Tomorrow()
_ = Yesterday()
}