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

本文主要是介绍go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 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/v2import "github.com/golang-module/carbon/v2"// 使用 gitee 库
go get -u gitee.com/golang-module/carbon/v2import "gitee.com/golang-module/carbon/v2"
Golang 版本小于 1.16
// 使用 github 库
go get -u github.com/golang-module/carbonimport "github.com/golang-module/carbon"// 使用 gitee 库
go get -u gitee.com/golang-module/carbonimport  "gitee.com/golang-module/carbon"
定义模型
type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"date"`Birthday2 Carbon `json:"birthday2" carbon:"time"`Birthday3 Carbon `json:"birthday3" carbon:"dateTime"`Birthday4 Carbon `json:"birthday4" carbon:"date" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"time" tz:"PRC"`Birthday6 Carbon `json:"birthday6" carbon:"dateTime" tz:"PRC"`
}

type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`Birthday4 Carbon `json:"birthday4" carbon:"layout:2006-01-02" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"layout:15:04:05" tz:"PRC"`Birthday6 Carbon `json:"birthday6" carbon:"layout:2006-01-02 15:04:05" tz:"PRC"`
}

type Person struct {Name string `json:"name"`Age  int    `json:"age"`Birthday0 Carbon `json:"birthday0"`Birthday1 Carbon `json:"birthday1" carbon:"format:Y-m-d"`Birthday2 Carbon `json:"birthday2" carbon:"format:H:i:s"`Birthday3 Carbon `json:"birthday3" carbon:"format:Y-m-d H:i:s"`Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d" tz:"PRC"`Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s" tz:"PRC"`Birthday6 Carbon `json:"birthday6" 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,Birthday0: now,Birthday1: now,Birthday2: now,Birthday3: now,Birthday4: now,Birthday5: now,Birthday6: 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,"birthday0": "2020-08-05 13:14:15","birthday1": "2020-08-05","birthday2": "13:14:15","birthday3": "2020-08-05 13:14:15","birthday4": "2020-08-05","birthday5": "213:14:15","birthday6": "2020-08-05 13:14:15"
}
JSON 解码
str := `{"name": "gouguoyin","age": 18,"birthday0": "2020-08-05 13:14:15","birthday1": "2020-08-05","birthday2": "13:14:15","birthday3": "2020-08-05 13:14:15","birthday4": "2020-08-05","birthday5": "213:14:15","birthday6": "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.Birthday0) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday1) // 2020-08-05
fmt.Sprintf("%s", person.Birthday2) // 13:14:15
fmt.Sprintf("%s", person.Birthday3) // 2002-08-05 13:14:15
fmt.Sprintf("%s", person.Birthday4) // 2002-08-05
fmt.Sprintf("%s", person.Birthday5) // 13:14:15
fmt.Sprintf("%s", person.Birthday6) // 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 方法中接收者类型从 结构体 更改为 指针

这篇关于go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/552212

相关文章

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http