Go-carbon 2.2.13 发布:简洁、语义化且开发者友好的 Golang 日期时间处理包
Go-carbon 2.2.13 发布:简洁、语义化且开发者友好的 Golang 日期时间处理包 Carbon 是一个简单、语义化且对开发者友好的 Golang 日期时间处理包。
Carbon 已被 awesome-go 收录,如果您觉得它有帮助,请给我一个星标。
github.com/golang-module/carbon
安装
Go 版本 >= 1.16
go get -u github.com/golang-module/carbon/v2
import "github.com/golang-module/carbon/v2"
Go 版本 < 1.16
go get -u github.com/golang-module/carbon
import "github.com/golang-module/carbon"
更新日志
- 修复了
lang/fr.json和lang/jp.json文件中的翻译错误 - 修复了
IsZero方法中的错误 - 新增了
IsDST方法 - 新增了
Closest和Farthest方法 - 将
modifier.go重命名为boundary.go,modifier_test.go重命名为boundary_test.go
更多关于Go-carbon 2.2.13 发布:简洁、语义化且开发者友好的 Golang 日期时间处理包的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于Go-carbon 2.2.13 发布:简洁、语义化且开发者友好的 Golang 日期时间处理包的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Go-carbon 2.2.13 的更新确实解决了一些关键问题并增加了实用功能。修复 IsZero 方法和翻译错误能提升包的稳定性,而新增的 IsDST、Closest 和 Farthest 方法则扩展了日期时间处理的场景覆盖。
示例代码:
package main
import (
"fmt"
"github.com/golang-module/carbon/v2"
)
func main() {
// 使用 IsZero 检查零值时间
t1 := carbon.CreateFromTimestamp(0)
fmt.Println(t1.IsZero()) // true
// 使用 IsDST 检查夏令时
t2 := carbon.Now().SetTimezone("America/New_York")
fmt.Println(t2.IsDST()) // 根据当前时间返回 true/false
// 使用 Closest 和 Farthest 比较时间
base := carbon.Parse("2023-10-01 12:00:00")
t3 := carbon.Parse("2023-10-01 10:00:00")
t4 := carbon.Parse("2023-10-01 14:00:00")
closest := base.Closest(t3, t4)
farthest := base.Farthest(t3, t4)
fmt.Println(closest.ToDateTimeString()) // 2023-10-01 10:00:00
fmt.Println(farthest.ToDateTimeString()) // 2023-10-01 14:00:00
}
文件重命名从 modifier.go 到 boundary.go 更准确地反映了其处理时间边界的功能,比如开始/结束日、周、月等操作:
// 边界操作示例
now := carbon.Now()
startOfMonth := now.StartOfMonth()
endOfMonth := now.EndOfMonth()
fmt.Println(startOfMonth.ToDateTimeString()) // 当月第一天 00:00:00
fmt.Println(endOfMonth.ToDateTimeString()) // 当月最后一天 23:59:59
这些改进使 Go-carbon 在日期时间处理上更加完整和可靠。

