dcfd24b624
v1.0.3 定位为 bug fix release,收口 v1.0.2 引入的破坏性清理并修复 4 个轻量 bug + 依赖复查 + 版本号治理 + 文档对齐。同时包含此前未提交 的 v1.0.2 全部工作。 v1.0.3 变更: Fixed: - generateJTI 忽略 rand.Read 错误(jwt)— 改为 (string, error) 并传播 - QueryBuilder.Page Count 受残留 Limit 截断(repository)— countDB 加 Limit(-1).Offset(-1) - OSS/本地存储文件名冲突(storage)— 新增 uniqueFilename 加 8 字节随机后缀 - 数据库重试策略对不可恢复错误无效(database)— 新增 isTransientDBError Dependencies: - go mod tidy 补全 postgres 方言传递依赖;安全补丁升级 x/crypto v0.49→v0.53、golang-jwt/jwt/v5 v5.2.1→v5.3.1、gorilla/websocket v1.5.1→v1.5.3 Removed: - Breaking: 清理 v1.0.2 兼容别名(InitMySQL* / driverName) - 死代码 database.DBResolver - 代码中 292 行"评分/理由"自夸注释(#26,23 个文件) Changed: - Breaking: 错误码体系重构 CodeSuccess 1→0、CodeFail 0→1,删除 CodeInvalidParams,加编译期防撞码 - database/mysql.go → manager.go;Logger 拆分三独立 core 修复 Tee 重复写入 - 版本号常量化:app.go 新增 const Version 作为唯一来源,CLI/脚手架模板引用之,不再散落字面量 Security: - CORS 中间件按 W3C 规范修复 Allow-Credentials / Vary: Origin / 非白名单不回显 Added: - console 包显式 level 控制(SetLevel/WithLevel/LevelSilent,atomic.Int32 并发安全) - 新增测试 jwt/repository/storage/database/router/middleware/app - 文档:新增 CHANGELOG.md、Version_v1.0.2_report.md;更新 README/GUIDE 顶部更新日志 - 文档对齐:删除对不存在的 config.DefaultManager() getter 的描述(保持 API 与文档一致) 详见 CHANGELOG.md 与 README.md 更新日志。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// NowUnix 返回当前秒级时间戳
|
|
func NowUnix() int64 {
|
|
return time.Now().Unix()
|
|
}
|
|
|
|
// NowTimestamp 返回当前毫秒时间戳
|
|
func NowTimestamp() int64 {
|
|
return time.Now().UnixMilli()
|
|
}
|
|
|
|
// FromUnix 秒级时间戳转 time.Time
|
|
func FromUnix(unix int64) time.Time {
|
|
return time.Unix(unix, 0)
|
|
}
|
|
|
|
// FromTimestamp 毫秒时间戳转 time.Time
|
|
func FromTimestamp(timestamp int64) time.Time {
|
|
return time.UnixMilli(timestamp)
|
|
}
|
|
|
|
// FormatTime 格式化时间
|
|
func FormatTime(t time.Time, layout string) string {
|
|
return t.Format(layout)
|
|
}
|
|
|
|
// ParseTime 解析时间字符串
|
|
func ParseTime(timeStr, layout string) (time.Time, error) {
|
|
return time.Parse(layout, timeStr)
|
|
}
|
|
|
|
// FormatDateTime 格式化为标准日期时间 "2006-01-02 15:04:05"
|
|
func FormatDateTime(t time.Time) string {
|
|
return t.Format("2006-01-02 15:04:05")
|
|
}
|
|
|
|
// FormatDate 格式化为日期 "2006-01-02"
|
|
func FormatDate(t time.Time) string {
|
|
return t.Format("2006-01-02")
|
|
}
|
|
|
|
// FormatTimeOnly 格式化为时间 "15:04:05"
|
|
func FormatTimeOnly(t time.Time) string {
|
|
return t.Format("15:04:05")
|
|
}
|
|
|
|
// StartOfDay 返回指定时间当天的开始时间 (00:00:00)
|
|
func StartOfDay(t time.Time) time.Time {
|
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
|
}
|
|
|
|
// EndOfDay 返回指定时间当天的结束时间 (23:59:59.999999999)
|
|
func EndOfDay(t time.Time) time.Time {
|
|
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, t.Location())
|
|
}
|
|
|
|
// StartOfWeek 返回指定时间当周的开始时间(周一为第一天)
|
|
func StartOfWeek(t time.Time) time.Time {
|
|
weekday := int(t.Weekday())
|
|
if weekday == 0 {
|
|
weekday = 7
|
|
}
|
|
d := time.Duration(weekday-1) * 24 * time.Hour
|
|
return StartOfDay(t.Add(-d))
|
|
}
|
|
|
|
// StartOfMonth 返回指定时间当月的开始时间
|
|
func StartOfMonth(t time.Time) time.Time {
|
|
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
|
}
|
|
|
|
// EndOfMonth 返回指定时间当月的结束时间
|
|
func EndOfMonth(t time.Time) time.Time {
|
|
return time.Date(t.Year(), t.Month()+1, 0, 23, 59, 59, 999999999, t.Location())
|
|
}
|
|
|
|
// GetDateInt 返回 yyyyMMdd 格式的日期整数
|
|
func GetDateInt(t time.Time) int {
|
|
ret, _ := strconv.Atoi(t.Format("20060102"))
|
|
return ret
|
|
}
|
|
|
|
// ParseDateInt 将 yyyyMMdd 格式的整数转为时间
|
|
func ParseDateInt(date int) time.Time {
|
|
year := date / 10000
|
|
month := (date % 10000) / 100
|
|
day := date % 100
|
|
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
|
|
}
|