diff --git a/time.go b/time.go index f428975..d0d606a 100644 --- a/time.go +++ b/time.go @@ -5,19 +5,32 @@ import ( "time" ) +func FormatTime(ts int64) string { + if ts < 1e10 { // 秒级(10位) + return time.Unix(ts, 0).Format("2006-01-02 15:04:05") + } else if ts < 1e13 { // 毫秒级(13位) + return time.UnixMilli(ts).Format("2006-01-02 15:04:05") + } else if ts < 1e16 { // 微秒级(16位) + return time.UnixMicro(ts).Format("2006-01-02 15:04:05") + } else { // 纳秒级(19位) + return time.Unix(0, ts).Format("2006-01-02 15:04:05") + } + return "" +} + // 时间戳转为日期 -func TimeToDate(t int64) string { - tm := time.Unix(t, 0) - return tm.Format("2006-01-02") +func TimeToDate(ts int64) string { + if ts < 1e10 { // 秒级(10位) + return time.Unix(ts, 0).Format("2006-01-02") + } else if ts < 1e13 { // 毫秒级(13位) + return time.UnixMilli(ts).Format("2006-01-02") + } + return "" } // 时间戳转为datetime格式 -func TimeToDateTime(t int64) string { - if t == 0 { - return "" - } - tm := time.Unix(t, 0) - return tm.Format("2006-01-02 15:04:05") +func TimeToDateTime(ts int64) string { + return FormatTime(ts) } // 日期转为时间戳