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 时区常量
  • 提高性能测试覆盖率
目录
相关文章
|
存储 前端开发 JavaScript
Go语言实战案例-项目实战篇:编写一个轻量级在线聊天室
本文介绍如何用Go语言从零实现一个轻量级在线聊天室,基于WebSocket实现实时通信,支持多人消息广播。涵盖前后端开发、技术选型与功能扩展,助你掌握Go高并发与实时通信核心技术。
1210 158
|
Go
在golang中发起http请求以获取访问域名的ip地址实例(使用net, httptrace库)
这只是追踪我们的行程的简单方法,不过希望你跟着探险家的脚步,即使是在互联网的隧道中,也可以找到你想去的地方。接下来就是你的探险之旅了,祝你好运!
814 26
|
测试技术 Go 开发者
go-carbon v2.6.10发布,轻量级、语义化、对开发者友好的 golang 时间处理库
Carbon 是一个轻量级、语义化的 Golang 时间处理库,支持时间穿越、差值计算、极值判断、星座、农历、儒略日、波斯历等特性。现由开源组织 dromara 维护,获 Gitee 2024 GVP 与 Gitcode G-Star 认可。优化了时间冻结方法、位运算替代条件判断,并新增 Sleep 方法、韩语文档及多项常量,提升性能与易用性。官网与源码详见:carbon.go-pkg.com,GitHub、Gitee、Gitcode 均可访问。
288 2
|
数据采集 监控 Go
用 Go 实现一个轻量级并发任务调度器(支持限速)
本文介绍了如何用 Go 实现一个轻量级的并发任务调度器,解决日常开发中批量任务处理的需求。调度器支持最大并发数控制、速率限制、失败重试及结果收集等功能。通过示例代码展示了其使用方法,并分析了核心组件设计,包括任务(Task)和调度器(Scheduler)。该工具适用于网络爬虫、批量请求等场景。文章最后总结了 Go 并发模型的优势,并提出了扩展功能的方向,如失败回调、超时控制等,欢迎读者交流改进。
706 25
|
Go 开发者
go-carbon v2.6.0 重大版本更新,轻量级、语义化、对开发者友好的 golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 Golang 时间处理库,提供了对时间穿越、时间差值、时间极值、时间判断、星座、星座、农历、儒略日 / 简化儒略日、波斯历 / 伊朗历的支持
588 3
|
JSON Go 开发者
go-carbon v2.5.0 发布,轻量级、语义化、对开发者友好的 golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 Golang 时间处理库,提供了对时间穿越、时间差值、时间极值、时间判断、星座、星座、农历、儒略日 / 简化儒略日、波斯历 / 伊朗历的支持。
471 4
|
网络协议 测试技术 Linux
Golang 实现轻量、快速的基于 Reactor 模式的非阻塞 TCP 网络库
gev 是一个基于 epoll 和 kqueue 实现的高性能事件循环库,适用于 Linux 和 macOS(Windows 暂不支持)。它支持多核多线程、动态扩容的 Ring Buffer 读写缓冲区、异步读写和 SO_REUSEPORT 端口重用。gev 使用少量 goroutine,监听连接并处理读写事件。性能测试显示其在不同配置下表现优异。安装命令:`go get -u github.com/Allenxuxu/gev`。
591 0
|
存储 Cloud Native Shell
go库介绍:Golang中的Viper库
Viper 是 Golang 中的一个强大配置管理库,支持环境变量、命令行参数、远程配置等多种配置来源。本文详细介绍了 Viper 的核心特点、应用场景及使用方法,并通过示例展示了其强大功能。无论是简单的 CLI 工具还是复杂的分布式系统,Viper 都能提供优雅的配置管理方案。
676 6
|
Unix Go
Golang语言标准库time之日期和时间相关函数
这篇文章是关于Go语言日期和时间处理的文章,介绍了如何使用Go标准库中的time包来处理日期和时间。
1013 3
|
存储 设计模式 安全
空结构体:Go 语言中的轻量级占位符
【8月更文挑战第31天】
472 0

热门文章

最新文章

推荐镜像

更多