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

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

时间:2023-12-29 09:55:58浏览次数:19  
标签:15 05 v2.3 Golang json Carbon go carbon 轻量级

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"`
    
    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 方法中接收者类型从 结构体 更改为 指针

标签:15,05,v2.3,Golang,json,Carbon,go,carbon,轻量级
From: https://www.cnblogs.com/gouguoyin/p/17934095.html

相关文章

  • 低代码之光!轻量级 GUI 的设计与实现
    前言每当提起低代码,很多人都会下意识的出现过激反应,吐槽低代码都是**,唯恐避之不及。可能大部分人觉得低代码就是替代手写代码,对于程序员来说这是不可接受的。其实低代码表述的含义非常宽泛,我相信很多人可能都在低代码平台中受益过,而且确实可以提升效率。像原型工具(Figma)、建站平......
  • 轻量级力量:深入MiniZip库,实现C++中ZIP文件的简便压缩与解压
     MiniZip是一个轻量级的压缩库,它是zlib库的一部分,用于在C++中进行ZIP文件的压缩和解压缩操作。以下是MiniZip的一些功能和优点:功能:创建ZIP文件: MiniZip可以用于创建包含一个或多个文件的ZIP归档。压缩: MiniZip支持使用不同的压缩算法对文件进行压缩,例如DEFLATE。解压缩......
  • 【golang】怎么在Go语言中实现锁机制
    Go语言的锁在Go语言中,最常用的锁是互斥锁(Mutex)。互斥锁是一种特殊的二进制信号量,用于控制对共享资源的访问。Go语言通过标准库中的"sync"包提供了互斥锁的功能。互斥锁的类型定义如下:type Mutex struct {    state int32    sema  uint32}其中state字段用于记......
  • 【Golang】Golang的跳一跳
    一、基本概念在Go语言中,代码跳转是指从一个位置跳转到另一个位置。一般而言,我们可以通过函数调用进行代码跳转,也可以通过goto语句进行跳转。不同的跳转方式有不同的适用场景。二、函数调用函数是Go语言中的基本构建块之一。调用函数时,程序的执行流程会跳转到被调用的函数中,并在......
  • 【golang】Go语言实现数据转发功能
    首先,我们需要考虑数据实体的格式。在许多情况下,使用JSON格式最为方便。Go语言中有一个标准库“encoding/json”可以提供JSON编解码功能。我们可以使用它来将我们的数据序列化为JSON格式,并将其发送到目标组件中。接下来,我们需要选择一种合适的网络协议来实现数据传输。常用的协议......
  • 【golang】Go语言中interface类型怎么使用
    1、Go语言中interface类型的定义 在Go语言中,interface类型是一个抽象的类型,它是一组方法签名的集合,只要某个类型实现了这些方法,它就属于该interface类型。在Go语言中定义一个interface类型的方法,需要使用interface关键字。下面是interface类型的完整定义方式:type interfac......
  • Golang channel的关闭
    使用内置函数close可以关闭channel,当channel关闭后,就不能再向channel写数据了,但是仍然可以从channel中读取数据。一旦将channel关闭了,只能读不能写。相当于关闭管道就数据不能进入到队列里面了,只能进行读操作,只读不写。channel支持for-range的方式进行遍历,请注意两个细节(1)在遍历时,......
  • Golang秒读32GB大文件,如何读取?
    在Go中,处理大文件时,一般采用分块读取的方式,以避免一次性加载整个文件到内存中。以下是读取大文件的简洁步骤:1 打开文件: 使用os.Open打开文件。file, err := os.Open("largefile.txt")if err != nil {    log.Fatal(err)}defer file.Close()2 获取文件信息: 使用......
  • Golang基础(一)
    粗略了解Golang的核心特性Go语言的特性一、并发编程不同于传统的多进程或多线程,golang的并发执行单元是一种称为goroutine的协程。其在语言级别提供关键字:go——用于启动协程。chan——golang中用于并发的通道,用于协程的通信。select——golang提供的多路复用机制。close......
  • Windows平台如何实现RTSP拉流添加动态水印|视频处理后转推RTMP或轻量级RTSP服务
     技术背景我们在做Windows平台流数据转发的时候,除了常规的RTSP转RTMP推送外,还有个场景就是,好多开发者希望拉取的RTSP流,做二次视频分析,或者加动态水印等,处理后的数据,再二次编码推送到RTMP服务或轻量级RTSP服务。技术实现本文就以Windows平台拉取RTSP流,回调yuv数据到上层,处理后的数......