From 5e4a8843740b007b4cf04ec08feb81282022f8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 10 Aug 2023 11:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=97=B6=E9=97=B4=E7=9A=84?= =?UTF-8?q?=E5=8C=85=E8=A3=85=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 71 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/time.go b/time.go index c0a42f1..b67fa6e 100644 --- a/time.go +++ b/time.go @@ -12,16 +12,22 @@ import ( "time" ) +const ( + // StandTimeFormat 标准时间格式 + StandTimeFormat = "2006-01-02 15:04:05" + // StandISO8601Time 标准ISO时间格式 + StandISO8601Time = "2006-01-02T15:04:05+08:00" +) + // OwnTime ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:28 2023/8/9 func OwnTime(inputTime time.Time) *Time { - return &Time{ - inputTime, - "2006-01-02 15:04:05", // 标准的时间格式 - } + instance := &Time{} + instance.Time = inputTime + return instance } // OwnTimeFromSecond 从秒获取实例 @@ -64,6 +70,24 @@ func OwnTimeFromNano(nano int64) *Time { return OwnTime(t) } +// OwnTimeFromISO8601Time ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:02 2023/8/10 +func OwnTimeFromISO8601Time(parseTime string, layout ...string) *Time { + var ( + err error + ) + + ownTime := &Time{} + + if ownTime.Time, err = time.Parse(ownTime.getTimeFormat(StandISO8601Time, layout...), parseTime); nil != err { + return ownTime + } + return ownTime +} + // Time 时间类型 // // Author : go_developer@163.com<白茶清欢> @@ -71,7 +95,6 @@ func OwnTimeFromNano(nano int64) *Time { // Date : 18:23 2023/8/9 type Time struct { time.Time - standTimeFormat string } // GetCurrentFormatTime 获取当前时间的格式化时间(秒) @@ -80,7 +103,7 @@ type Time struct { // // Date : 1:34 上午 2021/10/7 func (t *Time) GetCurrentFormatTime(layout ...string) string { - return time.Now().In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) + return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...)) } // FormatUnixNano 格式化纳秒时间戳 @@ -90,8 +113,7 @@ func (t *Time) GetCurrentFormatTime(layout ...string) string { // Date : 11:54 2022/7/14 func (t *Time) FormatUnixNano(layout ...string) string { nano := t.UnixNano() % 1e6 - milli := t.UnixNano() / 1e6 - return t.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) + return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano) } // FormatUnixMilli 格式化毫秒时间戳 @@ -99,39 +121,18 @@ func (t *Time) FormatUnixNano(layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:55 2022/7/14 -func (t *Time) FormatUnixMilli(timestamp int64, layout ...string) string { - return time.UnixMilli(timestamp).In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) +func (t *Time) FormatUnixMilli(layout ...string) string { + return time.UnixMilli(t.UnixMilli()).In(time.Local). + Format(t.getTimeFormat(StandTimeFormat, layout...)) + fmt.Sprintf(".%v", t.UnixMilli()%1000) } -// FormatUnixSec ... +// FormatUnixSec 格式化秒级时间戳 // // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2022/7/14 -func (t *Time) 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 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 13:48 2022/7/14 -func (t *Time) ParseISO8601Time(parseTime string) string { - var ( - timeLayout = "2006-01-02T15:04:05+08:00" - formatTime time.Time - err error - ) - - if formatTime, err = time.Parse(timeLayout, parseTime); nil != err { - return "" - } - - return formatTime.In(time.Local).Format("2006-01-02 15:04:05") +func (t *Time) FormatUnixSec(layout ...string) string { + return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...)) } // getTimeFormat 获取时间格式