1
+ package xtime_test
2
+
3
+ import (
4
+ xjson "github.com/goclub/json"
5
+ xtime "github.com/goclub/time"
6
+ "log"
7
+ "testing"
8
+ "time"
9
+ )
10
+
11
+ func ExampleParse () {
12
+ log .Print ("ExampleParse" )
13
+ year , err := xtime .ParseChinaYear ("2020" ) ; if err != nil {panic (err )}
14
+ log .Print (year .String ())
15
+ yearAndMonth , err := xtime .ParseChinaYearAndMonth ("2020-11" ) ; if err != nil {panic (err )}
16
+ log .Print (yearAndMonth .String ())
17
+ date , err := xtime .ParseChinaDate ("2020-11-11" ) ; if err != nil {panic (err )}
18
+ log .Print (date .String ())
19
+ sometime , err := xtime .ParseChinaTime ("2020-11-11 21:52:24" ) ; if err != nil {panic (err )}
20
+ log .Print (sometime .String ())
21
+ }
22
+ func ExampleFormat () {
23
+ log .Print ("ExampleFormat" )
24
+ sometime := time .Date (2020 ,12 ,31 ,23 ,23 ,23 , 0 ,time .UTC )
25
+ log .Print (xtime .FormatChinaYear (sometime )) // 2021
26
+ log .Print (xtime .FormatChinaYearAndMonth (sometime )) // 2021-01
27
+ log .Print (xtime .FormatChinaDate (sometime )) // 2021-01-01
28
+ log .Print (xtime .FormatChinaTime (sometime )) // 2021-01-01 07:23:23
29
+ log .Print (xtime .FormatChinaHourMinuteSecond (sometime )) // 07:23:23
30
+ }
31
+
32
+ func ExampleLocation () {
33
+ log .Print ("now time is :" + time .Now ().In (xtime .LocationChina ).String ())
34
+ }
35
+ // 直接使用 time.Time 作为json 字段时因为 time.Time{}.UnmarshalJSON() 和 time.Time{}.MarshalJSON() 的原因会以 time.RFC3339 格式作为 layout
36
+ func ExampleJSON_RFC3339 () {
37
+ log .Print ("ExampleJSON_RFC3339" )
38
+ reqeust := struct {
39
+ Time time.Time `json:"time"`
40
+ }{}
41
+ err := xjson .Unmarshal ([]byte (`{"time": "2020-12-31T23:23:23Z"}` ), & reqeust ) ; if err != nil {panic (err )}
42
+ log .Printf ("reqeust: %+v" , reqeust )
43
+
44
+ response := struct {
45
+ Time time.Time `json:"time"`
46
+ }{Time : time .Date (2020 ,12 ,31 ,23 ,23 ,23 , 0 ,time .UTC )}
47
+ data , err := xjson .Marshal (response ) ; if err != nil {panic (err )}
48
+ log .Print ("response json : " + string (data )) // {"time":"2020-12-31T23:23:23Z"}
49
+ }
50
+ func ExampleJSONChinaTime () {
51
+ log .Print ("ExampleJSONChinaTime" )
52
+ reqeust := struct {
53
+ Time xtime.ChinaTime `json:"time"`
54
+ }{}
55
+ err := xjson .Unmarshal ([]byte (`{"time": "2020-12-31 23:23:23"}` ), & reqeust ) ; if err != nil {panic (err )}
56
+ log .Printf ("reqeust: %+v" , reqeust )
57
+
58
+ response := struct {
59
+ Time xtime.ChinaTime `json:"time"`
60
+ }{Time : xtime .NewChinaTime (time .Date (2020 ,12 ,31 ,23 ,23 ,23 , 0 ,time .UTC ))}
61
+ data , err := xjson .Marshal (response ) ; if err != nil {panic (err )}
62
+ log .Print ("response json : " + string (data )) // {"time":"2020-12-31 23:23:23"}
63
+ }
64
+ func TestExample (t * testing.T ) {
65
+ ExampleFormat ()
66
+ ExampleLocation ()
67
+ ExampleParse ()
68
+ ExampleJSON_RFC3339 ()
69
+ ExampleJSONChinaTime ()
70
+ }
0 commit comments