From ac0d81e47cd968e95799b8843cd7e8974cbabc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Aug 2023 18:29:41 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=90=AC=E4=B8=80=E6=B3=A2time=E7=9A=84?= =?UTF-8?q?=E6=93=8D=E4=BD=9C,=20=E9=80=BB=E8=BE=91=E5=BE=85=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 time.go diff --git a/time.go b/time.go new file mode 100644 index 0000000..def769f --- /dev/null +++ b/time.go @@ -0,0 +1,102 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-08-09 18:22 +package wrapper + +import ( + "fmt" + "time" +) + +// Time ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:28 2023/8/9 +func Time(inputTime time.Time) *OwnTime { + return &OwnTime{ + inputTime: inputTime, + } +} + +// OwnTime 时间类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:23 2023/8/9 +type OwnTime struct { + inputTime time.Time +} + +// GetCurrentFormatTime 获取当前时间的格式化时间(秒) +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 1:34 上午 2021/10/7 +func (t *OwnTime) GetCurrentFormatTime(layout ...string) string { + if len(layout) == 0 { + layout = []string{"2006-01-02 15:04:05"} + } + return time.Now().In(time.Local).Format(layout[0]) +} + +// FormatUnixNano 格式化纳秒时间戳 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:54 2022/7/14 +func (t *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 t.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) +} + +// FormatUnixMilli 格式化毫秒时间戳 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:55 2022/7/14 +func (t *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 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:06 2022/7/14 +func (t *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 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 13:48 2022/7/14 +func (t *OwnTime) 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") +} -- 2.36.6 From 9d8c708852405b5f834e2996b7a3a610820ffdb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Aug 2023 18:43:03 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/time.go b/time.go index def769f..3664c2e 100644 --- a/time.go +++ b/time.go @@ -19,7 +19,8 @@ import ( // Date : 18:28 2023/8/9 func Time(inputTime time.Time) *OwnTime { return &OwnTime{ - inputTime: inputTime, + inputTime, + "2006-01-02 15:04:05", // 标准的时间格式 } } @@ -29,7 +30,8 @@ func Time(inputTime time.Time) *OwnTime { // // Date : 18:23 2023/8/9 type OwnTime struct { - inputTime time.Time + time.Time + standTimeFormat string } // GetCurrentFormatTime 获取当前时间的格式化时间(秒) @@ -38,10 +40,7 @@ type OwnTime struct { // // Date : 1:34 上午 2021/10/7 func (t *OwnTime) GetCurrentFormatTime(layout ...string) string { - if len(layout) == 0 { - layout = []string{"2006-01-02 15:04:05"} - } - return time.Now().In(time.Local).Format(layout[0]) + return time.Now().In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) } // FormatUnixNano 格式化纳秒时间戳 @@ -49,12 +48,9 @@ func (t *OwnTime) GetCurrentFormatTime(layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:54 2022/7/14 -func (t *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 +func (t *OwnTime) FormatUnixNano(layout ...string) string { + nano := t.UnixNano() % 1e6 + milli := t.UnixNano() / 1e6 return t.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) } @@ -64,10 +60,7 @@ func (t *OwnTime) FormatUnixNano(timestamp int64, layout ...string) string { // // Date : 11:55 2022/7/14 func (t *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]) + return time.UnixMilli(timestamp).In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) } // FormatUnixSec ... @@ -100,3 +93,15 @@ func (t *OwnTime) ParseISO8601Time(parseTime string) string { return formatTime.In(time.Local).Format("2006-01-02 15:04:05") } + +// getTimeFormat 获取时间格式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:37 2023/8/9 +func (t *OwnTime) getTimeFormat(defaultFormat string, layout ...string) string { + if len(layout) > 0 && len(layout[0]) > 0 { + return layout[0] + } + return defaultFormat +} -- 2.36.6 From 685dec4f3900cc496b22a5fadbf04b1c46e20318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Aug 2023 18:43:57 +0800 Subject: [PATCH 3/5] rename --- time.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/time.go b/time.go index 3664c2e..48d807f 100644 --- a/time.go +++ b/time.go @@ -12,24 +12,24 @@ import ( "time" ) -// Time ... +// OwnTime ... // // Author : go_developer@163.com<白茶清欢> // // Date : 18:28 2023/8/9 -func Time(inputTime time.Time) *OwnTime { - return &OwnTime{ +func OwnTime(inputTime time.Time) *Time { + return &Time{ inputTime, "2006-01-02 15:04:05", // 标准的时间格式 } } -// OwnTime 时间类型 +// Time 时间类型 // // Author : go_developer@163.com<白茶清欢> // // Date : 18:23 2023/8/9 -type OwnTime struct { +type Time struct { time.Time standTimeFormat string } @@ -39,7 +39,7 @@ type OwnTime struct { // Author : go_developer@163.com<白茶清欢> // // Date : 1:34 上午 2021/10/7 -func (t *OwnTime) GetCurrentFormatTime(layout ...string) string { +func (t *Time) GetCurrentFormatTime(layout ...string) string { return time.Now().In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) } @@ -48,7 +48,7 @@ func (t *OwnTime) GetCurrentFormatTime(layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:54 2022/7/14 -func (t *OwnTime) FormatUnixNano(layout ...string) string { +func (t *Time) FormatUnixNano(layout ...string) string { nano := t.UnixNano() % 1e6 milli := t.UnixNano() / 1e6 return t.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano) @@ -59,7 +59,7 @@ func (t *OwnTime) FormatUnixNano(layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 11:55 2022/7/14 -func (t *OwnTime) FormatUnixMilli(timestamp int64, layout ...string) string { +func (t *Time) FormatUnixMilli(timestamp int64, layout ...string) string { return time.UnixMilli(timestamp).In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...)) } @@ -68,7 +68,7 @@ func (t *OwnTime) FormatUnixMilli(timestamp int64, layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 12:06 2022/7/14 -func (t *OwnTime) FormatUnixSec(timestamp int64, layout ...string) string { +func (t *Time) FormatUnixSec(timestamp int64, layout ...string) string { if len(layout) == 0 { layout = []string{"2006-01-02 15:04:05"} } @@ -80,7 +80,7 @@ func (t *OwnTime) FormatUnixSec(timestamp int64, layout ...string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 13:48 2022/7/14 -func (t *OwnTime) ParseISO8601Time(parseTime string) string { +func (t *Time) ParseISO8601Time(parseTime string) string { var ( timeLayout = "2006-01-02T15:04:05+08:00" formatTime time.Time @@ -99,7 +99,7 @@ func (t *OwnTime) ParseISO8601Time(parseTime string) string { // Author : go_developer@163.com<白茶清欢> // // Date : 18:37 2023/8/9 -func (t *OwnTime) getTimeFormat(defaultFormat string, layout ...string) string { +func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string { if len(layout) > 0 && len(layout[0]) > 0 { return layout[0] } -- 2.36.6 From d64bcfbfd8f3c20fc096af1b40260fdc1af01d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 9 Aug 2023 19:02:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=84=E7=A7=8D?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E7=9A=84=E8=8E=B7=E5=8F=96time=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- time.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/time.go b/time.go index 48d807f..c0a42f1 100644 --- a/time.go +++ b/time.go @@ -24,6 +24,46 @@ func OwnTime(inputTime time.Time) *Time { } } +// OwnTimeFromSecond 从秒获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:47 2023/8/9 +func OwnTimeFromSecond(second int64) *Time { + t := time.UnixMilli(second * 1000) + return OwnTime(t) +} + +// OwnTimeFromMilli 从ms获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:56 2023/8/9 +func OwnTimeFromMilli(milli int64) *Time { + t := time.UnixMilli(milli) + return OwnTime(t) +} + +// OwnTimeFromMicro 从微秒获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:00 2023/8/9 +func OwnTimeFromMicro(micro int64) *Time { + t := time.UnixMicro(micro) + return OwnTime(t) +} + +// OwnTimeFromNano 从纳秒获取实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:00 2023/8/9 +func OwnTimeFromNano(nano int64) *Time { + t := time.Unix(nano/1e9, nano%1e9) + return OwnTime(t) +} + // Time 时间类型 // // Author : go_developer@163.com<白茶清欢> -- 2.36.6 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 5/5] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9A=84=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 获取时间格式 -- 2.36.6