-
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathcarbon_example_test.go
64 lines (50 loc) · 2.23 KB
/
carbon_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
package carbon_test
import (
"fmt"
"time"
"github.com/dromara/carbon/v2"
)
func ExampleNewCarbon() {
loc, _ := time.LoadLocation(carbon.PRC)
t1, _ := time.Parse(carbon.DateTimeLayout, "2020-08-05 13:14:15")
t2, _ := time.ParseInLocation(carbon.DateTimeLayout, "2020-08-05 13:14:15", loc)
fmt.Println("go zero time:", time.Time{}.String())
fmt.Println("go zero time with timezone:", time.Time{}.In(loc).String())
fmt.Println("go valid time:", t1.String())
fmt.Println("go valid time with timezone:", t2.In(loc).String())
fmt.Println("carbon zero time:", carbon.NewCarbon().ToString())
fmt.Println("carbon zero time with timezone:", carbon.NewCarbon().SetLocation(loc).ToString())
fmt.Println("carbon valid time:", carbon.NewCarbon(t1).ToString())
fmt.Println("carbon valid time with timezone:", carbon.NewCarbon(t2).SetLocation(loc).ToString())
// Output:
// go zero time: 0001-01-01 00:00:00 +0000 UTC
// go zero time with timezone: 0001-01-01 08:05:43 +0805 LMT
// go valid time: 2020-08-05 13:14:15 +0000 UTC
// go valid time with timezone: 2020-08-05 13:14:15 +0800 CST
// carbon zero time: 0001-01-01 00:00:00 +0000 UTC
// carbon zero time with timezone: 0001-01-01 08:05:43 +0805 LMT
// carbon valid time: 2020-08-05 13:14:15 +0000 UTC
// carbon valid time with timezone: 2020-08-05 13:14:15 +0800 CST
}
func ExampleCarbon_Copy() {
oldCarbon := carbon.Parse("2020-08-05")
newCarbon := oldCarbon.Copy()
oldCarbon = oldCarbon.AddDay().SetLayout(carbon.DateTimeLayout).SetLocale("zh-CN").SetWeekStartsAt(carbon.Monday)
fmt.Printf("old time: %s\n", oldCarbon.ToString())
fmt.Printf("new time: %s\n", newCarbon.ToString())
fmt.Printf("old layout: %s\n", oldCarbon.CurrentLayout())
fmt.Printf("new layout: %s\n", newCarbon.CurrentLayout())
fmt.Printf("old locale: %s\n", oldCarbon.Locale())
fmt.Printf("new locale: %s\n", newCarbon.Locale())
fmt.Printf("old week starts at: %s\n", oldCarbon.WeekStartsAt())
fmt.Printf("new week starts at: %s\n", newCarbon.WeekStartsAt())
// Output:
// old time: 2020-08-06 00:00:00 +0000 UTC
// new time: 2020-08-05 00:00:00 +0000 UTC
// old layout: 2006-01-02 15:04:05
// new layout: 2006-01-02
// old locale: zh-CN
// new locale: en
// old week starts at: Monday
// new week starts at: Sunday
}