go-carbon v2.3.2 发布,轻量级、语义化、对开发者友好的 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"`

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

    Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"`
    Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"`
    Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"`
    Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" tz:"PRC"`
}

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

    Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"`
    Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"`
    Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"`
    Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" 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"`

    Birthday5 Carbon `json:"birthday5" carbon:"timestamp" tz:"PRC"`
    Birthday6 Carbon `json:"birthday6" carbon:"timestampMilli" tz:"PRC"`
    Birthday7 Carbon `json:"birthday7" carbon:"timestampMicro" tz:"PRC"`
    Birthday8 Carbon `json:"birthday8" carbon:"timestampNano" 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,
    Birthday5: now,
    Birthday6: now,
    Birthday7: now,
    Birthday8: 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",
    "birthday5": 1596604455,
    "birthday6": 1596604455999,
    "birthday7": 1596604455999999,
    "birthday8": 1596604455999999999
}
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",
    "birthday5": 1596604455,
    "birthday6": 1596604455999,
    "birthday7": 1596604455999999,
    "birthday8": 1596604455999999999
}`
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

fmt.Sprintf("%d", person.Birthday5) // 1596604455
fmt.Sprintf("%d", person.Birthday6) // 1596604455999
fmt.Sprintf("%d", person.Birthday7) // 1596604455999999
fmt.Sprintf("%d", person.Birthday8) // 1596604455999999999

更新日志

  • 新增 tag 结构体,优化 LoadTag 方法
  • encoding.go 文件中 MarshalJSON 方法新增对 U,V,X,Z 的解析
  • 优化 database.go 文件中的 Scan 方法,将默认时区设置为 Local
  • 优化 outputer.go 文件中 String 方法
  • CreateFromStdTime 方法新增 timezone 可选参数
  • 新增 India 时区常量
  • 提高性能测试覆盖率
目录
相关文章
|
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月前
|
Go 调度 开发者
Go语言并发基础:轻量级线程与通道通信
【2月更文挑战第6天】本文介绍了Go语言在并发编程方面的基础知识和核心概念。我们将深入探讨goroutine(轻量级线程)的创建与调度,以及如何利用channel进行goroutine间的通信与同步。此外,还将简要提及select语句的使用,并解释其在处理多个channel操作时的优势。
|
2月前
|
存储 缓存 算法
Golang高性能内存缓存库BigCache设计与分析
【2月更文挑战第4天】分析Golang高性能内存缓存库BigCache设计
72 0
|
3月前
|
安全 Go 调度
Go 语言 Goroutine - 轻量级线程
Go 语言 Goroutine - 轻量级线程
20 0
|
3月前
|
JSON Go 开发工具
Golang日志库Zap基本使用
Golang日志库Zap基本使用
36 0
|
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