Golang时间处理库carbon v2.0.0发布,要求Go版本不低于1.16
Golang时间处理库carbon v2.0.0发布,要求Go版本不低于1.16 Carbon 是一个简单、语义化且对开发者友好的 Go 语言日期时间处理包。
Carbon 已被收录至 awesome-go,如果您觉得它有帮助,请给我一个星标。
github.com/golang-module/carbon
安装
Go 版本 < 1.16
go get -u github.com/golang-module/carbon
import (
"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。利用
embedded特性,无需再将 Lang 语言目录复制到当前项目中 - Carbon 结构体中的公共字段
time改为私有字段time - 移除
ToTimestamp()方法,仅保留Timestamp()方法 - 移除
ToTimestampWithSecond()方法,仅保留TimestampWithSecond()方法 - 移除
ToTimestampWithMillisecond()方法,仅保留TimestampWithMillisecond()方法 - 移除
ToTimestampWithMicrosecond()方法,仅保留TimestampWithMicrosecond()方法 - 移除
ToTimestampWithNanosecond()方法,仅保留TimestampWithNanosecond()方法 - 移除
ToTimestamp结构体,仅保留Timestamp结构体 - 移除
ToTimestampWithSecond结构体,仅保留TimestampWithSecond结构体 - 移除
ToTimestampWithMillisecond结构体,仅保留TimestampWithMillisecond结构体 - 移除
ToTimestampWithMicrosecond结构体,仅保留TimestampWithMicrosecond结构体 - 移除
ToTimestampWithNanosecond结构体,仅保留ToTimestampWithNanosecond结构体 - 移除
ToDateTimeString结构体,仅保留DateTime结构体 - 移除
ToDateString结构体,仅保留Date结构体 - 移除
ToTimeString结构体,仅保留Time结构体
更多关于Golang时间处理库carbon v2.0.0发布,要求Go版本不低于1.16的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于Golang时间处理库carbon v2.0.0发布,要求Go版本不低于1.16的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Carbon v2.0.0 的发布确实是一个重要的版本更新,主要利用了 Go 1.16 引入的 embed 特性来简化国际化文件的处理。以下是一些关键变更的示例代码:
1. 导入方式变更 对于 Go 1.16+ 的项目,现在需要使用 v2 导入路径:
import (
"fmt"
"github.com/golang-module/carbon/v2"
)
func main() {
now := carbon.Now()
fmt.Println(now.ToDateTimeString()) // 输出: 2023-10-01 15:04:05
}
2. 方法名简化
移除了所有 To 前缀的方法,直接使用更简洁的命名:
// v1.x 版本
timestamp := carbon.Now().ToTimestamp()
// v2.0.0 版本
timestamp := carbon.Now().Timestamp()
fmt.Println(timestamp) // 输出: 1696151045
3. 时间戳方法统一
now := carbon.Now()
// 不同精度的时间戳获取
second := now.Timestamp() // 秒级时间戳
milli := now.TimestampWithMillisecond() // 毫秒时间戳
micro := now.TimestampWithMicrosecond() // 微秒时间戳
nano := now.TimestampWithNanosecond() // 纳秒时间戳
fmt.Printf("秒: %d, 毫秒: %d, 微秒: %d, 纳秒: %d\n",
second, milli, micro, nano)
4. 格式化输出简化
now := carbon.Now()
// 日期时间格式化
datetime := now.ToDateTimeString() // v1.x
datetime := now.DateTime() // v2.0.0
// 日期格式化
date := now.ToDateString() // v1.x
date := now.Date() // v2.0.0
// 时间格式化
time := now.ToTimeString() // v1.x
time := now.Time() // v2.0.0
fmt.Println(datetime) // 输出: 2023-10-01 15:04:05
fmt.Println(date) // 输出: 2023-10-01
fmt.Println(time) // 输出: 15:04:05
5. 国际化支持(利用 embed 特性) v2.0.0 通过 embed 自动包含语言文件,无需手动复制:
// 设置语言
carbon.SetLocale("zh-CN")
now := carbon.Now()
fmt.Println(now.DiffForHumans()) // 输出: 刚刚
// 支持多语言输出
carbon.SetLocale("en")
fmt.Println(now.DiffForHumans()) // 输出: just now
6. 时间计算示例
now := carbon.Now()
// 加减时间
nextWeek := now.AddWeek()
lastMonth := now.SubMonth()
// 时间比较
isFuture := now.Gt(nextWeek)
isPast := now.Lt(lastMonth)
// 时间段计算
diff := now.DiffInDays(nextWeek)
fmt.Printf("距离下周还有 %d 天\n", diff)
7. 时区处理
// 创建指定时区的时间
nyTime := carbon.Now(carbon.NewYork)
londonTime := carbon.Now(carbon.London)
// 时区转换
utcTime := nyTime.ToUTC()
localTime := utcTime.ToLocal()
fmt.Println(nyTime.DateTime()) // 纽约时间
fmt.Println(londonTime.DateTime()) // 伦敦时间
这个版本通过方法名的简化和 embed 特性的使用,确实让代码更加整洁。对于新项目,建议直接使用 v2.0.0;对于现有项目升级,需要注意修改所有相关的方法调用。

