2 Star 0 Fork 70

golang-package/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
carbon_example_test.go 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
package carbon_test
import (
"fmt"
"time"
"gitee.com/golang-package/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.Sunday)
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: Sunday
// new week starts at: Monday
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golang-package/carbon.git
git@gitee.com:golang-package/carbon.git
golang-package
carbon
carbon
master

搜索帮助