From f5fddb92e850320506308160c09fa72d47e6c485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 8 Apr 2023 14:38:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E5=B7=A5=E5=85=B7=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BC=A0=E5=85=A5layout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/time.go b/time.go index bd3ba82..876da61 100644 --- a/time.go +++ b/time.go @@ -18,6 +18,7 @@ import ( // // Date : 15:24 2022/5/14 type ownTime struct { + format string } // GetCurrentFormatTime 获取当前时间的格式化时间(秒) @@ -34,10 +35,13 @@ func (ot *ownTime) GetCurrentFormatTime() string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:54 2022/7/14 -func (ot *ownTime) FormatUnixNano(timestamp int64) string { +func (ot *ownTime) FormatUnixNano(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05.000"} + } nano := timestamp % 1e6 milli := timestamp / 1e6 - return ot.FormatUnixMilli(milli) + fmt.Sprintf(" %v", nano) + return ot.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) } // FormatUnixMilli 格式化毫秒时间戳 @@ -45,8 +49,11 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:55 2022/7/14 -func (ot *ownTime) FormatUnixMilli(timestamp int64) string { - return time.UnixMilli(timestamp).In(time.Local).Format("2006-01-02 15:04:05.000") +func (ot *ownTime) FormatUnixMilli(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05.000"} + } + return time.UnixMilli(timestamp).In(time.Local).Format(layout[0]) } // FormatUnixSec ... @@ -54,8 +61,11 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string { // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2022/7/14 -func (ot *ownTime) FormatUnixSec(timestamp int64) string { - return time.Unix(timestamp, 0).In(time.Local).Format("2006-01-02 15:04:05") +func (ot *ownTime) FormatUnixSec(timestamp int64, layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05"} + } + return time.Unix(timestamp, 0).In(time.Local).Format(layout[0]) } // ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05