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") +}