主要变更: - 采用 go-zero 框架替代 Gin,提升开发效率 - 重构项目结构,API 文件模块化组织 - 将 model 移至 api/internal/model 目录 - 移除 common 包,改为标准 pkg 目录结构 - 实现统一的仓储模式,支持配置驱动数据库切换 - 简化测试策略,专注 API 集成测试 - 更新 CLAUDE.md 文档,提供详细的开发指导 技术栈更新: - 框架: Gin → go-zero v1.6.0+ - 代码生成: 引入 goctl 工具 - 架构模式: 四层架构 → go-zero 三层架构 (Handler→Logic→Model) - 项目布局: 遵循 Go 社区标准和 go-zero 最佳实践
153 lines
3.6 KiB
Go
153 lines
3.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// FormatTime 格式化时间为字符串
|
|
func FormatTime(t time.Time, layout string) string {
|
|
if layout == "" {
|
|
layout = "2006-01-02 15:04:05"
|
|
}
|
|
return t.Format(layout)
|
|
}
|
|
|
|
// ParseTime 解析时间字符串
|
|
func ParseTime(timeStr, layout string) (time.Time, error) {
|
|
if layout == "" {
|
|
layout = "2006-01-02 15:04:05"
|
|
}
|
|
return time.Parse(layout, timeStr)
|
|
}
|
|
|
|
// GetTimeAgo 获取相对时间描述
|
|
func GetTimeAgo(t time.Time) string {
|
|
now := time.Now()
|
|
diff := now.Sub(t)
|
|
|
|
if diff < time.Minute {
|
|
return "刚刚"
|
|
}
|
|
|
|
if diff < time.Hour {
|
|
minutes := int(diff.Minutes())
|
|
return fmt.Sprintf("%d分钟前", minutes)
|
|
}
|
|
|
|
if diff < 24*time.Hour {
|
|
hours := int(diff.Hours())
|
|
return fmt.Sprintf("%d小时前", hours)
|
|
}
|
|
|
|
if diff < 30*24*time.Hour {
|
|
days := int(diff.Hours() / 24)
|
|
return fmt.Sprintf("%d天前", days)
|
|
}
|
|
|
|
if diff < 365*24*time.Hour {
|
|
months := int(diff.Hours() / (24 * 30))
|
|
return fmt.Sprintf("%d个月前", months)
|
|
}
|
|
|
|
years := int(diff.Hours() / (24 * 365))
|
|
return fmt.Sprintf("%d年前", years)
|
|
}
|
|
|
|
// IsToday 检查时间是否为今天
|
|
func IsToday(t time.Time) bool {
|
|
now := time.Now()
|
|
return t.Year() == now.Year() && t.YearDay() == now.YearDay()
|
|
}
|
|
|
|
// IsThisWeek 检查时间是否为本周
|
|
func IsThisWeek(t time.Time) bool {
|
|
now := time.Now()
|
|
year, week := now.ISOWeek()
|
|
tYear, tWeek := t.ISOWeek()
|
|
return year == tYear && week == tWeek
|
|
}
|
|
|
|
// IsThisMonth 检查时间是否为本月
|
|
func IsThisMonth(t time.Time) bool {
|
|
now := time.Now()
|
|
return t.Year() == now.Year() && t.Month() == now.Month()
|
|
}
|
|
|
|
// IsThisYear 检查时间是否为今年
|
|
func IsThisYear(t time.Time) bool {
|
|
now := time.Now()
|
|
return t.Year() == now.Year()
|
|
}
|
|
|
|
// GetWeekRange 获取本周的开始和结束时间
|
|
func GetWeekRange(t time.Time) (start, end time.Time) {
|
|
// 获取周一作为周开始
|
|
weekday := int(t.Weekday())
|
|
if weekday == 0 {
|
|
weekday = 7 // 周日为7
|
|
}
|
|
|
|
start = t.AddDate(0, 0, -(weekday-1))
|
|
start = time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, start.Location())
|
|
|
|
end = start.AddDate(0, 0, 6)
|
|
end = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, end.Location())
|
|
|
|
return start, end
|
|
}
|
|
|
|
// GetMonthRange 获取本月的开始和结束时间
|
|
func GetMonthRange(t time.Time) (start, end time.Time) {
|
|
start = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
|
end = start.AddDate(0, 1, -1)
|
|
end = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 999999999, end.Location())
|
|
|
|
return start, end
|
|
}
|
|
|
|
// GetYearRange 获取本年的开始和结束时间
|
|
func GetYearRange(t time.Time) (start, end time.Time) {
|
|
start = time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
|
|
end = time.Date(t.Year(), 12, 31, 23, 59, 59, 999999999, t.Location())
|
|
|
|
return start, end
|
|
}
|
|
|
|
// Timestamp 获取当前时间戳(秒)
|
|
func Timestamp() int64 {
|
|
return time.Now().Unix()
|
|
}
|
|
|
|
// TimestampMilli 获取当前时间戳(毫秒)
|
|
func TimestampMilli() int64 {
|
|
return time.Now().UnixNano() / 1e6
|
|
}
|
|
|
|
// FromTimestamp 从时间戳创建时间对象
|
|
func FromTimestamp(timestamp int64) time.Time {
|
|
return time.Unix(timestamp, 0)
|
|
}
|
|
|
|
// FromTimestampMilli 从毫秒时间戳创建时间对象
|
|
func FromTimestampMilli(timestamp int64) time.Time {
|
|
return time.Unix(0, timestamp*1e6)
|
|
}
|
|
|
|
// FormatDuration 格式化持续时间
|
|
func FormatDuration(d time.Duration) string {
|
|
if d < time.Minute {
|
|
return fmt.Sprintf("%.0f秒", d.Seconds())
|
|
}
|
|
|
|
if d < time.Hour {
|
|
return fmt.Sprintf("%.0f分钟", d.Minutes())
|
|
}
|
|
|
|
if d < 24*time.Hour {
|
|
return fmt.Sprintf("%.1f小时", d.Hours())
|
|
}
|
|
|
|
days := d.Hours() / 24
|
|
return fmt.Sprintf("%.1f天", days)
|
|
} |