carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。
目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧
github.com/golang-module/carbon
gitee.com/golang-module/carbon
// 使用 github 库
go get -u github.com/golang-module/carbon/v2
import "github.com/golang-module/carbon/v2"
// 使用 gitee 库
go get -u gitee.com/golang-module/carbon/v2
import "gitee.com/golang-module/carbon/v2"// 使用 github 库
go get -u github.com/golang-module/carbon
import "github.com/golang-module/carbon"
// 使用 gitee 库
go get -u gitee.com/golang-module/carbon
import "gitee.com/golang-module/carbon"type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"date"`
Birthday2 Carbon `json:"birthday2" carbon:"time"`
Birthday3 Carbon `json:"birthday3" carbon:"dateTime"`
Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"`
}或
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`
Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`
Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`
Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"`
}或
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 Carbon `json:"birthday0"`
Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"`
Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"`
Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"`
Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"`
Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"`
Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s" tz:"PRC"`
}如果
carbon标签没有设置,默认是layout:2006-01-02 15:04:05;如果tz标签没有设置,默认是Local
now := Parse("2020-08-05 13:14:15", PRC)
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday0: now,
Birthday1: now,
Birthday2: now,
Birthday3: now,
Birthday4: now,
Birthday5: now,
Birthday6: now,
}loadErr := carbon.LoadTag(&person)
if loadErr != nil {
// 错误处理
log.Fatal(loadErr)
}
data, marshalErr := json.Marshal(person)
if marshalErr != nil {
// 错误处理
log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// 输出
{
"name": "gouguoyin",
"age": 18,
"birthday0": "2020-08-05 13:14:15",
"birthday1": "2020-08-05",
"birthday2": "13:14:15",
"birthday3": "2020-08-05 13:14:15",
"birthday4": "2020-08-05",
"birthday5": "213:14:15",
"birthday6": "2020-08-05 13:14:15"
}str := `{
"name": "gouguoyin",
"age": 18,
"birthday0": "2020-08-05 13:14:15",
"birthday1": "2020-08-05",
"birthday2": "13:14:15",
"birthday3": "2020-08-05 13:14:15",
"birthday4": "2020-08-05",
"birthday5": "213:14:15",
"birthday6": "2020-08-05 13:14:15"
}`
var person Person
loadErr := carbon.LoadTag(&person)
if loadErr != nil {
// 错误处理
log.Fatal(loadErr)
}
unmarshalErr := json.Unmarshal([]byte(str), &person)
if unmarshalErr != nil {
// 错误处理
log.Fatal(unmarshalErr)
}
fmt.Sprintf("%s", person.Birthday0) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday1) // 2020-08-05
fmt.Sprintf("%s", person.Birthday2) // 13:14:15
fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday4) // 2002-08-05
fmt.Sprintf("%s", person.Birthday5) // 13:14:15
fmt.Sprintf("%s", person.Birthday6) // 2002-08-05 13:14:15Now 方法中设置测试时间时 testNow 为 0,造成 IsSetTestNow 方法返回 false 的 bugxxx_bench_test.goDateTimeFormat, DateFormat, TimeFormat, AtomFormat, ANSICFormat loadTag 函数中 carbon 标签增加对 datetime、date、time、iso8601 等字符串的支持loadTag 函数中新增 tz 标签,用于设置时区ParseByLayout 方法中添加对 U、V、X、Z 格式化符号的支持ToFormatString 或 Format 方法中添加对 v、u、x 格式符号的支持ClearTestNow 方法重命名为 UnSetTestNowHasTestNow 方法重命名为 IsSetTestNowxxx_test.go 文件重命名为 xxx_unit_test.gojson.go 文件重命名为 encoding.go, json_test.go 文件重命名为 encoding_unit_test.goClosest 和 Farthest 方法从 traveler.go 文件移动到 extremum.go,从 traveler_test.go 文件移动到 extremum_unit_test.go SetTestNow 方法中接收者类型从 结构体 更改为 指针原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。