Go-carbon v2.3.1 was released, a simple, semantic and developer-friendly golang package for datetime

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 is layout:2006-01-02 15:04:05, if the tz tag is not set, the default is Local

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 in Now method
  • Add benchmark test files xxx_bench_test.go
  • Add format constants, such as DateTimeFormat, DateFormat, TimeFormat, AtomFormat, ANSICFormat
  • Add support carbon tag of struct carbon type field for datetime, date, time, iso8601 and other strings in LoadTag method
  • Add support tz tag of struct carbon type field in loadTag function, use to set timezone #207
  • Add support for U, V, X, Z formatting symbols in ParseByLayout method #206
  • Add support for v, u, x formatting symbols in ToFormatString or Format method
  • Rename ClearTestNow method to UnSetTestNow
  • Rename HasTestNow method to IsSetTestNow
  • Rename xxx_test.go file to xxx_unit_test.go
  • Rename json.go file to encoding.go, json_test.go file to encoding_unit_test.go
  • Move Closest and Farthest methods from traveler.go to extremum.go, traveler_test.go to extremum_unit_test.go
  • :warning:Change receiver type from struct to pointer in SetTestNow method