-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathparser_example_test.go
More file actions
93 lines (76 loc) · 3.2 KB
/
parser_example_test.go
File metadata and controls
93 lines (76 loc) · 3.2 KB
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
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())
// 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
}
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())
// 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
}
func ExampleParseByLayouts() {
c := carbon.ParseByLayouts("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 ExampleParseByFormats() {
c := carbon.ParseByFormats("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
}