jinzhu/now 是一个专注于简化 Go 语言时间操作的轻量级库。它扩展了标准库 time 的功能,提供了一系列便捷方法,用于计算时间段的开始/结束时刻、解析灵活的时间字符串,并支持自定义配置(如周起始日、时区等)。适用于日志统计、报表生成等需要复杂时间计算的场景。
时间计算
now.BeginningOfDay() // 当天 00:00:00
now.EndOfMonth() // 当月最后一天的 23:59:59.999999999
now.WeekStartDay = time.Monday
now.BeginningOfWeek() // 返回本周一的 00:00:00
时间解析
• 严格错误处理:Parse 返回错误,MustParse 在解析失败时触发 panic。
now.Parse("2017") // 2017-01-01 00:00:00
now.Parse("10-13") // 当前年份的 10 月 13 日
now.Parse("14") // 当天 14:00:00
灵活配置
loc, _ := time.LoadLocation("Asia/Shanghai")
config := &now.Config{
TimeLocation: loc,
TimeFormats: []string{"2006/01/02"},
}
t := config.Parse("2023/10/01")
基于特定时间计算
t := time.Date(2023, 2, 18, 17, 0, 0, 0, time.UTC)
now.With(t).EndOfQuarter() // 2023-03-31 23:59:59.999999999
package main
import (
"fmt"
"github.com/jinzhu/now"
"time"
)
funcmain() {
// 计算当前时间的周结束时刻(默认周日为起始)
endOfWeek := now.EndOfWeek()
fmt.Println("End of Week (Sun):", endOfWeek)
// 配置周起始日为周一
now.WeekStartDay = time.Monday
fmt.Println("End of Week (Mon):", now.EndOfWeek())
// 解析字符串并处理错误
if t, err := now.Parse("2023-10-12 22:14"); err == nil {
fmt.Println("Parsed Time:", t)
}
}
添加自定义时间格式:通过扩展 TimeFormats 支持更多格式:
now.TimeFormats = append(now.TimeFormats, "2006年01月02日")
t := now.MustParse("2023年10月01日")
EndOf 系列方法返回纳秒级最大值(如 23:59:59.999999999),确保涵盖时间段的最后一刻。Parse 时需检查错误,避免无效格式导致程序中断。Jinzhu/now 凭借其简洁的 API 和灵活的配置,成为 Go 开发者处理复杂时间操作的利器。无论是统计时段的界定,还是动态时间解析,该库都能显著提升开发效率。
[1]GitHub - jinzhu/now: Now is a time toolkit for golang: https://github.com/jinzhu/now