Go-carbon v2.3.0 Christmas special version is released. This should be the last version in 2023. I wish everyone a Merry Christmas!
Carbon is a simple, semantic and developer-friendly golang package for datetime.
Carbon has been included by awesome-go , if you think it is helpful, please give me a star.
github.com/golang-module/carbon
Installation
Go version >= 1.16
go get -u github.com/golang-module/carbon/v2
import "github.com/golang-module/carbon/v2"
JSON handling
Define model
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 Carbon `json:"birthday1"`
Birthday2 Carbon `json:"birthday2" carbon:"date" tz:"PRC"`
Birthday3 Carbon `json:"birthday3" carbon:"time" tz:"PRC"`
Birthday4 Carbon `json:"birthday4" carbon:"dateTime" tz:"PRC"`
}
or
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 Carbon `json:"birthday1"`
Birthday2 Carbon `json:"birthday2" carbon:"layout:2006-01-02" tz:"PRC"`
Birthday3 Carbon `json:"birthday3" carbon:"layout:15:04:05" tz:"PRC"`
Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"`
}
or
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 Carbon `json:"birthday1"`
Birthday2 Carbon `json:"birthday2" carbon:"format:Y-m-d" tz:"PRC"`
Birthday3 Carbon `json:"birthday3" carbon:"format:H:i:s" tz:"PRC"`
Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d H:i:s" tz:"PRC"`
}
If the
carbon
tag is not set, the default islayout:2006-01-02 15:04:05
, if thetz
tag is not set, the default isLocal
Instantiate model
now := Parse("2020-08-05 13:14:15", PRC)
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday1: now,
Birthday2: now,
Birthday3: now,
Birthday4: now,
}
JSON encode
loadErr := carbon.LoadTag(&person)
if loadErr != nil {
// Error handle...
log.Fatal(loadErr)
}
data, marshalErr := json.Marshal(person)
if marshalErr != nil {
// Error handle...
log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// Output
{
"name": "gouguoyin",
"age": 18,
"birthday1": "2020-08-05 13:14:15",
"birthday2": "2020-08-05",
"birthday3": "13:14:15",
"birthday4": "2020-08-05 13:14:15"
}
JSON decode
str := `{
"name": "gouguoyin",
"age": 18,
"birthday1": "2020-08-05 13:14:15",
"birthday2": "2020-08-05",
"birthday3": "13:14:15",
"birthday4": "2020-08-05 13:14:15"
}`
var person Person
loadErr := carbon.LoadTag(&person)
if loadErr != nil {
// Error handle...
log.Fatal(loadErr)
}
unmarshalErr := json.Unmarshal([]byte(str), &person)
if unmarshalErr != nil {
// Error handle...
log.Fatal(unmarshalErr)
}
fmt.Sprintf("%s", person.Birthday1) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday2) // 2020-08-05
fmt.Sprintf("%s", person.Birthday3) // 13:14:15
fmt.Sprintf("%s", person.Birthday4) // 2002-08-05 13:14:15
Change log
- Fix
testNow
is 0 when setting test now inNow
method - Add benchmark test files
xxx_bench_test.go
- Add format constants, such as
DateTimeFormat
,DateFormat
,TimeFormat
,AtomFormat
,ANSICFormat
… - Add support
carbon
tag of structcarbon
type field fordatetime
,date
,time
,iso8601
and other strings inLoadTag
method - Add support
tz
tag of structcarbon
type field inloadTag
function, use to set timezone #207 - Add support for
U
,V
,X
,Z
formatting symbols inParseByLayout
method #206 - Add support for
v
,u
,x
formatting symbols inToFormatString
orFormat
method - Rename
ClearTestNow
method toUnSetTestNow
- Rename
HasTestNow
method toIsSetTestNow
- Rename
xxx_test.go
file toxxx_unit_test.go
- Rename
json.go
file toencoding.go
,json_test.go
file toencoding_unit_test.go
- Move
Closest
andFarthest
methods fromtraveler.go
toextremum.go
,traveler_test.go
toextremum_unit_test.go
- Change receiver type from
struct
topointer
inSetTestNow
method