Golang日期库carbon v1.3.1发布:新增人类可读时间差(diffforhumans)与国际(i18n)支持

Golang日期库carbon v1.3.1发布:新增人类可读时间差(diffforhumans)与国际(i18n)支持 go-carbon 是一个简单、语义化且对开发者友好的 Go 语言日期时间包。

人性化时间差
// 默认语言环境为 en
carbon.Now().DiffForHumans() // just now
carbon.Now().SubYears(1).DiffForHumans() // 1 years ago
carbon.Now().SubYears(2).DiffForHumans() // 2 year ago
carbon.Now().AddYears(1).DiffForHumans() // in 1 year
carbon.Now().AddYears(2).DiffForHumans() // in 2 years
// 将语言环境设置为 zh-CN
carbon.Now().SetLocale("zh-CN").DiffForHumans() // 刚刚
carbon.Now().SubMonths(1).SetLocale("zh-CN").DiffForHumans() // 1 月前
carbon.Now().AddMonths(2).SetLocale("zh-CN").DiffForHumans() // 2 月后

github: github.com/golang-module/carbon


更多关于Golang日期库carbon v1.3.1发布:新增人类可读时间差(diffforhumans)与国际(i18n)支持的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于Golang日期库carbon v1.3.1发布:新增人类可读时间差(diffforhumans)与国际(i18n)支持的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


carbon v1.3.1的这次更新确实很实用,特别是DiffForHumans方法和i18n支持,让时间显示更加友好。下面是一个结合国际化的完整示例:

package main

import (
    "fmt"
    "github.com/golang-module/carbon"
)

func main() {
    // 英文环境示例
    fmt.Println("English locale:")
    fmt.Println(carbon.Now().DiffForHumans())                    // just now
    fmt.Println(carbon.Now().SubHours(3).DiffForHumans())       // 3 hours ago
    fmt.Println(carbon.Now().AddDays(5).DiffForHumans())        // in 5 days
    
    // 中文环境示例
    fmt.Println("\n中文环境:")
    cn := carbon.Now().SetLocale("zh-CN")
    fmt.Println(cn.DiffForHumans())                             // 刚刚
    fmt.Println(cn.SubHours(3).DiffForHumans())                 // 3 小时前
    fmt.Println(cn.AddDays(5).DiffForHumans())                  // 5 天后
    
    // 法语环境示例
    fmt.Println("\nFrench locale:")
    fr := carbon.Now().SetLocale("fr")
    fmt.Println(fr.DiffForHumans())                             // à l'instant
    fmt.Println(fr.SubMinutes(15).DiffForHumans())              // il y a 15 minutes
    fmt.Println(fr.AddWeeks(2).DiffForHumans())                 // dans 2 semaines
    
    // 日语环境示例
    fmt.Println("\nJapanese locale:")
    jp := carbon.Now().SetLocale("ja")
    fmt.Println(jp.DiffForHumans())                             // ついさっき
    fmt.Println(jp.SubMonths(1).DiffForHumans())                // 1 ヶ月前
    fmt.Println(jp.AddYears(1).DiffForHumans())                 // 1 年後
}

这个版本还支持相对时间显示,可以精确到秒、分钟、小时、天、周、月、年等不同粒度:

// 精确时间差显示
now := carbon.Now()
past := now.SubMinutes(90)

fmt.Println(past.DiffForHumans())                    // 1 hour ago
fmt.Println(past.SetLocale("zh-CN").DiffForHumans()) // 1 小时前

// 指定比较时间
target := carbon.Parse("2024-01-01 00:00:00")
fmt.Println(target.DiffForHumans(now))               // 2 months ago
fmt.Println(target.SetLocale("zh-CN").DiffForHumans(now)) // 2 月前

对于需要多语言支持的Web应用,可以这样集成:

func getHumanReadableTime(locale string, t time.Time) string {
    c := carbon.CreateFromStdTime(t).SetLocale(locale)
    return c.DiffForHumans()
}

// 根据用户偏好显示时间
userLocale := "es" // 从用户配置获取
createdAt := time.Now().Add(-2 * time.Hour)
message := getHumanReadableTime(userLocale, createdAt)
// 输出: hace 2 horas

carbon的i18n支持目前包含英语、简体中文、繁体中文、日语、韩语、法语、德语、俄语、葡萄牙语、西班牙语等主要语言,覆盖了大部分国际化需求。

回到顶部