-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
95 lines (88 loc) · 3.45 KB
/
example_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
package xtime_test
import (
json "encoding/json"
xtime "github.com/goclub/time"
"log"
"testing"
"time"
)
func ExampleParseChinaYear() {
log.Print("ExampleParseChinaYear")
year, err := xtime.ParseChinaYear("2020") ; if err != nil {panic(err)}
log.Print(year.String()) // 2020-01-01 00:00:00 +0800 CST
}
func ExampleParseChinaYearAndMonth() {
log.Print("ExampleParseChinaYearAndMonth")
yearAndMonth, err := xtime.ParseChinaYearAndMonth("2020-11") ; if err != nil {panic(err)}
log.Print(yearAndMonth.String()) // 2020-11-01 00:00:00 +0800 CST
}
func ExampleParseChinaDate() {
log.Print("ExampleParseChinaDate")
date, err := xtime.ParseChinaDate("2020-11-11") ; if err != nil {panic(err)}
log.Print(date.String()) // 2020-11-11 00:00:00 +0800 CST
}
func ExampleParseChinaTime() {
log.Print("ExampleParseChinaTime")
sometime, err := xtime.ParseChinaTime("2020-11-11 21:52:24") ; if err != nil {panic(err)}
log.Print(sometime.String()) // 2020-11-11 21:52:24 +0800 CST
}
func ExampleFormatChinaYear() {
log.Print("ExampleFormatChinaYear")
sometime := time.Date(2020,12,31,23,23,23, 0,time.UTC)
log.Print(xtime.FormatChinaYear(sometime)) // 2021
}
func ExampleFormatChinaYearAndMonth() {
log.Print("ExampleFormatChinaYearAndMonth")
sometime := time.Date(2020,12,31,23,23,23, 0,time.UTC)
log.Print(xtime.FormatChinaYearAndMonth(sometime)) // 2021
}
func ExampleFormatChinaDate() {
log.Print("ExampleFormatChinaDate")
sometime := time.Date(2020,12,31,23,23,23, 0,time.UTC)
log.Print(xtime.FormatChinaDate(sometime)) // 2021-01-01
}
func ExampleFormatChinaTime() {
log.Print("ExampleFormatChinaTime")
sometime := time.Date(2020,12,31,23,23,23, 0,time.UTC)
log.Print(xtime.FormatChinaTime(sometime)) // 2021-01-01 07:23:23
}
func ExampleFormatChinaHourMinuteSecond() {
log.Print("ExampleFormatChinaHourMinuteSecond")
sometime := time.Date(2020,12,31,23,23,23, 0,time.UTC)
log.Print(xtime.FormatChinaHourMinuteSecond(sometime)) // 07:23:23
}
func ExampleLocationChina() {
log.Print("now time is :" + time.Now().In(xtime.LocChina).String())
}
// 直接使用 time.Time 作为json 字段时因为 time.Time{}.UnmarshalJSON() 和 time.Time{}.MarshalJSON() 的原因会以 time.RFC3339 格式作为 layout
func ExampleJSON_RFC3339() {
log.Print("ExampleJSON_RFC3339")
request := struct {
Time time.Time `json:"time"`
}{}
err := json.Unmarshal([]byte(`{"time": "2020-12-31T23:23:23Z"}`), &request) ; if err != nil {panic(err)}
log.Printf("request: %+v", request) // request: {Time:2020-12-31 23:23:23 +0000 UTC}
response := struct {
Time time.Time `json:"time"`
}{Time: time.Date(2020,12,31,23,23,23, 0,time.UTC)}
data, err := json.Marshal(response) ; if err != nil {panic(err)}
log.Print("response json : " + string(data)) // response json : {"time":"2020-12-31T23:23:23Z"}
}
func ExampleChinaTime () {
log.Print("ExampleChinaTime")
request := struct {
Time xtime.ChinaTime `json:"time"`
}{}
err := json.Unmarshal([]byte(`{"time": "2020-12-31 23:23:23"}`), &request) ; if err != nil {panic(err)}
log.Printf("request: %+v", request) // request: {Time:2020-12-31 23:23:23 +0800 CST}
var sometime time.Time
sometime = request.Time.Time
log.Print(sometime)
response := struct {
Time xtime.ChinaTime `json:"time"`
}{Time: xtime.NewChinaTime(time.Date(2020,12,31,23,23,23, 0,time.UTC))}
data, err := json.Marshal(response) ; if err != nil {panic(err)}
log.Print("response json : " + string(data)) // response json : {"time":"2021-01-01 07:23:23"}
}
func TestExample(t *testing.T) {
}