diff --git a/time.go b/time.go new file mode 100644 index 0000000..b67fa6e --- /dev/null +++ b/time.go @@ -0,0 +1,148 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-08-09 18:22 +package wrapper + +import ( + "fmt" + "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 { + instance := &Time{} + instance.Time = inputTime + return instance +} + +// 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) +} + +// 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<白茶清欢> +// +// Date : 18:23 2023/8/9 +type Time struct { + time.Time +} + +// GetCurrentFormatTime 获取当前时间的格式化时间(秒) +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 1:34 上午 2021/10/7 +func (t *Time) GetCurrentFormatTime(layout ...string) string { + return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...)) +} + +// FormatUnixNano 格式化纳秒时间戳 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:54 2022/7/14 +func (t *Time) FormatUnixNano(layout ...string) string { + nano := t.UnixNano() % 1e6 + return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano) +} + +// FormatUnixMilli 格式化毫秒时间戳 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:55 2022/7/14 +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 格式化秒级时间戳 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:06 2022/7/14 +func (t *Time) FormatUnixSec(layout ...string) string { + return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...)) +} + +// getTimeFormat 获取时间格式 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:37 2023/8/9 +func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string { + if len(layout) > 0 && len(layout[0]) > 0 { + return layout[0] + } + return defaultFormat +}