Go-carbon v2.3.0 Christmas particular model is launched. This needs to be the final model in 2023. I want everybody a Merry Christmas!
Carbon is an easy, semantic and developer-friendly golang bundle for datetime.
Carbon has been included by awesome-go , should you assume it’s useful, please give me a star.
github.com/golang-module/carbon
Set up
Go model >= 1.16
go get -u github.com/golang-module/carbon/v2
import "github.com/golang-module/carbon/v2"
JSON dealing with
Outline mannequin
kind Particular person struct {
Title string `json:"identify"`
Age int `json:"age"`
Birthday carbon.Carbon `json:"birthday" carbon:"format:2006-01-02"`
GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"format:15:04:05"`
CreatedAt carbon.Carbon `json:"created_at" carbon:"format:2006-01-02 15:04:05"`
}
or
kind Particular person struct {
Title string `json:"identify"`
Age int `json:"age"`
Birthday carbon.Carbon `json:"birthday" carbon:"format:Y-m-d"`
GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"format:H:i:s"`
CreatedAt carbon.Carbon `json:"created_at" carbon:"format:Y-m-d H:i:s"`
}
Instantiate mannequin
now := Parse("2020-08-05 13:14:15", PRC)
particular person := Particular person {
Title: "gouguoyin",
Age: 18,
Birthday: now,
GraduatedAt: now,
CreatedAt: now,
}
JSON encode
err1 := carbon.LoadTag(&particular person)
if err1 != nil {
// Error deal with...
log.Deadly(err1)
}
knowledge, err2 := json.Marshal(particular person)
if err2 != nil {
// Error deal with...
log.Deadly(err2)
}
fmt.Printf("%s", knowledge)
// Output
{
"identify": "gouguoyin",
"age": 18,
"birthday": "2020-08-05",
"graduated_at": "13:14:15",
"created_at": "2020-08-05 13:14:15"
}
JSON decode
str := `{
"identify": "gouguoyin",
"age": 18,
"birthday": "2020-08-05",
"graduated_at": "13:14:15",
"created_at": "2020-08-05 13:14:15"
}`
var particular person Particular person
err1 := carbon.LoadTag(&particular person)
if err1 != nil {
// Error deal with...
log.Deadly(err1)
}
err2 := json.Unmarshal([]byte(str), &particular person)
if err2 != nil {
// Error deal with...
log.Deadly(err2)
}
fmt.Sprintf("%s", particular person.Birthday) // 2002-08-05
fmt.Sprintf("%s", particular person.GraduatedAt) // 13:14:15
fmt.Sprintf("%s", particular person.CreatedAt) // 2002-08-05 13:14:15
Change log
- Repair bug in
DiffInYears
andAge
strategies #181 - Repair bug with misplaced time zone after
json.Unmarshal
#178 - Transfer
CreateFromStdTime
technique fromcarbon.go
tocreator.go
- Transfer
ToStdTime
technique fromcarbon.go
tooutputer.go
- Transfer deprecated strategies as
FromStdTime
,Time2Carbon
andCarbon2Time
todeprecated.go
- Replace
stretchr/testify
to v1.8.4