[优化] 时间戳解析

This commit is contained in:
PandaX-Go
2024-08-10 12:06:12 +08:00
parent 0b5322dd2d
commit 0ee53e15f2

View File

@@ -2,6 +2,7 @@ package tool
import (
"encoding/json"
"fmt"
"pandax/pkg/global"
"reflect"
"strings"
@@ -131,9 +132,13 @@ func StringToStruct(m string, s interface{}) error {
// TimeToFormat convert time to formatted string
func TimeToFormat(val interface{}) string {
switch v := val.(type) {
case int:
t := timestampToTime(int64(v))
// 格式化时间字符串
formattedTime := t.Format("2006-01-02 15:04:05")
return formattedTime
case int64:
// 如果是时间戳类型,将其转换为时间对象
t := time.Unix(v, 0)
t := timestampToTime(v)
// 格式化时间字符串
formattedTime := t.Format("2006-01-02 15:04:05")
return formattedTime
@@ -151,3 +156,15 @@ func TimeToFormat(val interface{}) string {
return ""
}
}
func timestampToTime(val int64) time.Time {
// 如果是时间戳类型,将其转换为时间对象
timeLen := len(fmt.Sprintf("%d", val))
if timeLen == 10 {
return time.Unix(val, 0)
}
if timeLen == 13 {
return time.UnixMilli(val)
}
return time.Now()
}