go-carbon v2.3.1 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库
carbon 是一个轻量级、语义化、对开发者友好的 Golang 时间处理库,支持链式调用、农历和 gorm、xorm 等主流 orm。
目前已被 awesome-go-cn 收录,如果您觉得不错,请给个 star 吧
github.com/golang-module/carbon
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"` }
实例化模型
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
标签增加对datetime
、date
、time
、iso8601
等字符串的支持 - 在
loadTag
函数中新增tz
标签,用于设置时区 - 在
ParseByLayout
方法中添加对U
、V
、X
、Z
格式化符号的支持 - 在
ToFormatString
或Format
方法中添加对v
、u
、x
格式符号的支持 - 新增
LoadTag
方法,利用反射通过设置 tag 进行 json 自定义编码和解码 - 将
ClearTestNow
方法重命名为UnSetTestNow
- 将
HasTestNow
方法重命名为IsSetTestNow
- 将
xxx_test.go
文件重命名为xxx_unit_test.go
- 将
json.go
文件重命名为encoding.go
,json_test.go
文件重命名为encoding_unit_test.go
- 将
Closest
和Farthest
方法从traveler.go
文件移动到extremum.go
,从traveler_test.go
文件移动到extremum_unit_test.go
- 将
SetTestNow
方法中接收者类型从结构体
更改为指针

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Simple Admin Go 语言分布式后台管理系统 v1.3.0 发布
Simple Admin - Go 语言分布式后台管理系统 v1.3.0 更新 项目介绍 Simple Admin 是一个开箱即用的分布式微服务后端管理系统,基于go-zero开发,为开发中大型后台提供了丰富的功能,支持三端代码生成。 官方自带多种扩展,助力中小企业快速上云,快速迭代。适合用于微服务学习和商用,开源免费。 Simple Admin Core / Job / MCMS / FMS / Common v1.3.0 更新 介绍 核心模块 Core, 定时任务模块 Job, 消息中心模块 MCMS, 以及文件管理模块 FMS 均已升级至 v1.3.0 本次更新 新增(Common): NewRedis 和 NewRedisCluster 方法合并为 NewUniversalRedis, 使得 redis 同时支持单体和集群 优化(Common): Captcha store 使用 universal redis 重构(Core): errorhandler 包重构为 dberrorhandler 新增(Core): token 新增 username 字段,方便后台查看 优化:...
- 下一篇
yuqing v1.2.0 已经发布,舆情监控系统
yuqing v1.2.0 已经发布,舆情监控系统。 此版本更新内容包括: 1.修复了若干bug 2.文章详情新增光学字符识别、图像识别、智写报告功能 3.搜索菜单改为全文搜索 4.新增监测大屏 5.用户识别鉴权逻辑由session改为token实现 详情查看:https://gitee.com/stonedtx/yuqing/releases/v1.2.0
相关文章
文章评论
共有0条评论来说两句吧...