go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库

简介: carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。

carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。

目前已被 awesome-go 收录,如果您觉得不错,请给个 star 吧

github.com/golang-module/carbon

gitee.com/golang-module/carbon

安装使用

Golang 版本大于等于 1.16
// 使用 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"
Golang 版本小于 1.16
// 使用 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"`
  Birthday carbon.Carbon `json:"birthday" carbon:"layout:2006-01-02"`
  GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"layout:15:04:05"`
  CreatedAt carbon.Carbon `json:"created_at" carbon:"layout:2006-01-02 15:04:05"`
}

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"`
}

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"`
}

如果 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,

  Birthday1: now,
  Birthday2: now,
  Birthday3: now,
  Birthday4: now,
}
JSON 编码
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,
  "birthday1": "2020-08-05 13:14:15",
  "birthday2": "2020-08-05",
  "birthday3": "13:14:15",
  "birthday4": "2020-08-05 13:14:15"
}
JSON 解码
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 {
   
    // 错误处理
    log.Fatal(loadErr)
}

unmarshalErr := json.Unmarshal([]byte(str), &person)
if unmarshalErr != nil {
   
    // 错误处理
    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

更新日志

  • 修复在 Now 方法中设置测试时间时 testNow 为 0,造成 IsSetTestNow 方法返回 false 的 bug
  • 添加性能测试文件 xxx_bench_test.go
  • 增加格式模板常量,如 DateTimeFormat, DateFormat, TimeFormat, AtomFormat, ANSICFormat
  • loadTag 函数中 carbon 标签增加对 datetimedatetimeiso8601 等字符串的支持
  • loadTag 函数中新增 tz 标签,用于设置时区
  • ParseByLayout 方法中添加对 UVXZ 格式化符号的支持
  • ToFormatStringFormat 方法中添加对 vux 格式符号的支持
  • ClearTestNow 方法重命名为 UnSetTestNow
  • HasTestNow 方法重命名为 IsSetTestNow
  • xxx_test.go 文件重命名为 xxx_unit_test.go
  • json.go 文件重命名为 encoding.go, json_test.go 文件重命名为 encoding_unit_test.go
  • ClosestFarthest 方法从 traveler.go 文件移动到 extremum.go,从 traveler_test.go 文件移动到 extremum_unit_test.go
  • SetTestNow 方法中接收者类型从 结构体 更改为 指针
目录
相关文章
|
2月前
|
测试技术 Go 开发者
go-carbon v2.3.8 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。
27 0
|
3月前
|
Go
golang数据结构篇之栈和队列以及简单标准库
golang数据结构篇之栈和队列以及简单标准库
35 0
|
3月前
|
测试技术 Go 开发者
go-carbon v2.3.6 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。
24 1
|
3月前
|
测试技术 Go 开发者
go-carbon v2.3.7 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。
19 2
|
2月前
|
存储 缓存 算法
Golang高性能内存缓存库BigCache设计与分析
【2月更文挑战第4天】分析Golang高性能内存缓存库BigCache设计
72 0
|
3月前
|
JSON Go 开发工具
Golang日志库Zap基本使用
Golang日志库Zap基本使用
36 0
|
3月前
|
JSON 测试技术 Go
go-carbon v2.3.2 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 golang 时间处理库,支持链式调用。
36 2
|
1月前
|
SQL 前端开发 Go
编程笔记 GOLANG基础 001 为什么要学习Go语言
编程笔记 GOLANG基础 001 为什么要学习Go语言
|
3月前
|
物联网 Go 网络性能优化
使用Go语言(Golang)可以实现MQTT协议的点对点(P2P)消息发送。MQTT协议本身支持多种消息收发模式
使用Go语言(Golang)可以实现MQTT协议的点对点(P2P)消息发送。MQTT协议本身支持多种消息收发模式【1月更文挑战第21天】【1月更文挑战第104篇】
102 1
|
2天前
|
Go 开发者
Golang深入浅出之-Go语言上下文(context)包:处理取消与超时
【4月更文挑战第23天】Go语言的`context`包提供`Context`接口用于处理任务取消、超时和截止日期。通过传递`Context`对象,开发者能轻松实现复杂控制流。本文解析`context`包特性,讨论常见问题和解决方案,并给出代码示例。关键点包括:1) 确保将`Context`传递给所有相关任务;2) 根据需求选择适当的`Context`创建函数;3) 定期检查`Done()`通道以响应取消请求。正确使用`context`包能提升Go程序的控制流管理效率。
7 1