From 0ee53e15f28485fbbd25fa1ef2d9a750e2c5c235 Mon Sep 17 00:00:00 2001 From: PandaX-Go Date: Sat, 10 Aug 2024 12:06:12 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/tool/conv.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/tool/conv.go b/pkg/tool/conv.go index bea2823..16915e8 100644 --- a/pkg/tool/conv.go +++ b/pkg/tool/conv.go @@ -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 @@ -150,4 +155,16 @@ func TimeToFormat(val interface{}) string { default: return "" } -} \ No newline at end of file +} + +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() +}