feat(db): 添加数据库配置自动查找和缓存功能
- 实现配置文件自动查找功能,支持yaml、yml、toml、ini、json格式 - 添加查询缓存机制,提高重复查询性能 - 新增构建脚本build.sh和build.bat用于跨平台编译 - 添加完整的数据库连接配置和时间字段配置功能 - 实现DAO基类提供通用CRUD操作方法 - 添加配置文件示例和相关测试用例
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeFormat 默认时间格式
|
||||
const TimeFormat = "2006-01-02 15:04:05"
|
||||
|
||||
// FormatTime 格式化时间为默认格式
|
||||
func FormatTime(t time.Time) string {
|
||||
return t.Format(TimeFormat)
|
||||
}
|
||||
|
||||
// ParseTime 解析时间字符串
|
||||
func ParseTime(timeStr string) (time.Time, error) {
|
||||
return time.Parse(TimeFormat, timeStr)
|
||||
}
|
||||
|
||||
// Now 返回当前时间(默认格式)
|
||||
func Now() string {
|
||||
return time.Now().Format(TimeFormat)
|
||||
}
|
||||
|
||||
// Timestamp 返回当前时间戳
|
||||
func Timestamp() int64 {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
// FormatTimestamp 格式化时间戳为默认格式
|
||||
func FormatTimestamp(timestamp int64) string {
|
||||
return time.Unix(timestamp, 0).Format(TimeFormat)
|
||||
}
|
||||
|
||||
// IsZeroTime 检查是否是零值时间
|
||||
func IsZeroTime(t time.Time) bool {
|
||||
return t.IsZero() || t.UnixNano() == 0
|
||||
}
|
||||
|
||||
// SafeTime 安全获取时间,如果是零值则返回当前时间
|
||||
func SafeTime(t time.Time) time.Time {
|
||||
if IsZeroTime(t) {
|
||||
return time.Now()
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// FormatToDefault 将任意时间格式化为默认格式
|
||||
func FormatToDefault(t time.Time) string {
|
||||
if IsZeroTime(t) {
|
||||
return ""
|
||||
}
|
||||
return FormatTime(t)
|
||||
}
|
||||
Reference in New Issue
Block a user