forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_example_test.go
111 lines (92 loc) · 4.23 KB
/
parser_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package carbon_test
import (
"fmt"
"github.com/dromara/carbon/v2"
)
func ExampleParse() {
fmt.Println(carbon.Parse("2020-8-5").ToString())
fmt.Println(carbon.Parse("2020-8-05").ToString())
fmt.Println(carbon.Parse("2020-08-05").ToString())
fmt.Println(carbon.Parse("2020-8-5 1:2:3").ToString())
fmt.Println(carbon.Parse("2020-08-05 1:2:03").ToString())
fmt.Println(carbon.Parse("2020-08-05 1:02:03").ToString())
fmt.Println(carbon.Parse("2020-08-05 01:02:03").ToString())
fmt.Println(carbon.Parse("2023-01-08T09:02:48").ToString())
fmt.Println(carbon.Parse("2023-1-8T09:02:48").ToString())
fmt.Println(carbon.Parse("2023-01-08T9:2:48").ToString())
fmt.Println(carbon.Parse("2023-01-8T9:2:48").ToString())
fmt.Println(carbon.Parse("0000-01-01 00:00:00").ToString())
fmt.Println(carbon.Parse("0001-01-01 00:00:00").ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 01:02:03 +0000 UTC
// 2020-08-05 01:02:03 +0000 UTC
// 2020-08-05 01:02:03 +0000 UTC
// 2020-08-05 01:02:03 +0000 UTC
// 2023-01-08 09:02:48 +0000 UTC
// 2023-01-08 09:02:48 +0000 UTC
// 2023-01-08 09:02:48 +0000 UTC
// 2023-01-08 09:02:48 +0000 UTC
// 0000-01-01 00:00:00 +0000 UTC
// 0001-01-01 00:00:00 +0000 UTC
}
func ExampleParseByLayout() {
fmt.Println(carbon.ParseByLayout("2020-08-05", carbon.DateLayout).ToString())
fmt.Println(carbon.ParseByLayout("2020-08-05 13:14:15", carbon.DateTimeLayout, carbon.PRC).ToString())
fmt.Println(carbon.ParseByLayout("2020|08|05 13:14:15", "2006|01|02 15:04:05").ToString())
fmt.Println(carbon.ParseByLayout("It is 2020-08-05 13:14:15", "It is 2006-01-02 15:04:05").ToString())
fmt.Println(carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒").ToString())
fmt.Println(carbon.ParseByLayout("1699677240", carbon.TimestampLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666", carbon.TimestampMilliLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666666", carbon.TimestampMicroLayout).ToString())
fmt.Println(carbon.ParseByLayout("1699677240666666666", carbon.TimestampNanoLayout).ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 13:14:15 +0800 CST
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2023-11-11 04:34:00 +0000 UTC
// 2023-11-11 04:34:00.666 +0000 UTC
// 2023-11-11 04:34:00.666666 +0000 UTC
// 2023-11-11 04:34:00.666666666 +0000 UTC
}
func ExampleParseByFormat() {
fmt.Println(carbon.ParseByFormat("2020-08-05", carbon.DateFormat).ToString())
fmt.Println(carbon.ParseByFormat("2020-08-05 13:14:15", carbon.DateTimeFormat, carbon.PRC).ToString())
fmt.Println(carbon.ParseByFormat("2020|08|05 13:14:15", "Y|m|d H:i:s").ToString())
fmt.Println(carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToString())
fmt.Println(carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToString())
fmt.Println(carbon.ParseByFormat("1699677240", carbon.TimestampFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666", carbon.TimestampMilliFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666666", carbon.TimestampMicroFormat).ToString())
fmt.Println(carbon.ParseByFormat("1699677240666666666", carbon.TimestampNanoFormat).ToString())
// Output:
// 2020-08-05 00:00:00 +0000 UTC
// 2020-08-05 13:14:15 +0800 CST
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2020-08-05 13:14:15 +0000 UTC
// 2023-11-11 04:34:00 +0000 UTC
// 2023-11-11 04:34:00.666 +0000 UTC
// 2023-11-11 04:34:00.666666 +0000 UTC
// 2023-11-11 04:34:00.666666666 +0000 UTC
}
func ExampleParseWithLayouts() {
c := carbon.ParseWithLayouts("2020|08|05 13|14|15", []string{"2006|01|02 15|04|05", "2006|1|2 3|4|5"})
fmt.Println(c.ToString())
fmt.Println(c.CurrentLayout())
// Output:
// 2020-08-05 13:14:15 +0000 UTC
// 2006|01|02 15|04|05
}
func ExampleParseWithFormats() {
c := carbon.ParseWithFormats("2020|08|05 13|14|15", []string{"Y|m|d H|i|s", "y|m|d h|i|s"})
fmt.Println(c.ToString())
fmt.Println(c.CurrentLayout())
// Output:
// 2020-08-05 13:14:15 +0000 UTC
// 2006|01|02 15|04|05
}