service.xpcool.com/common/tools/timex/timex.go
夏犀麟 1486bb903f feat(api): 新增公共接口 api/common/v1(tools 工具端点)并修复 gtime 格式化
- api/common/v1/tools.go 契约 + internal/controller/common 实现:uuid/md5/random/time/ip
- cmd.go 注册 /api/common/v1 公开分组(无鉴权),复用 common/tools Go 包
- 修复 gtime v2.10.2 坑:Format 为 PHP 风格,Go layout 须用 Layout(),timex 封装为 Layout 语义
- 冒烟测试 5 端点全部通过;AGENTS.md / PROJECT_STRUCTURE.md / change-log 同步更新
2026-08-24 17:29:30 +08:00

47 lines
1.3 KiB
Go

// Package timex provides time formatting and computation helpers on top of gtime.
package timex
import (
"github.com/gogf/gf/v2/os/gtime"
)
const (
// LayoutDateTime is the conventional datetime layout: 2006-01-02 15:04:05.
LayoutDateTime = "2006-01-02 15:04:05"
// LayoutDate is the conventional date layout: 2006-01-02.
LayoutDate = "2006-01-02"
)
// Now returns the current time.
func Now() *gtime.Time {
return gtime.Now()
}
// Format returns t formatted with the given Go layout.
// When layout is empty, LayoutDateTime is used.
//
// Note: gtime v2.10.2's Format() takes PHP-style format ("Y-m-d H:i:s"),
// so this helper uses the Layout() method which accepts Go layouts.
func Format(t *gtime.Time, layout ...string) string {
ly := LayoutDateTime
if len(layout) > 0 && layout[0] != "" {
ly = layout[0]
}
return t.Layout(ly)
}
// Timestamp returns the current Unix timestamp in seconds.
func Timestamp() int64 {
return gtime.Now().Timestamp()
}
// StartOfDay returns the beginning (00:00:00) of the day containing t.
func StartOfDay(t *gtime.Time) *gtime.Time {
return gtime.NewFromStr(t.Layout(LayoutDate) + " 00:00:00")
}
// EndOfDay returns the end (23:59:59) of the day containing t.
func EndOfDay(t *gtime.Time) *gtime.Time {
return gtime.NewFromStr(t.Layout(LayoutDate) + " 23:59:59")
}