mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[优化] 时间戳解析
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user