2023-08-09 18:29:41 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-08-09 18:22
|
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-08-10 11:08:01 +08:00
|
|
|
const (
|
|
|
|
// StandTimeFormat 标准时间格式
|
|
|
|
StandTimeFormat = "2006-01-02 15:04:05"
|
|
|
|
// StandISO8601Time 标准ISO时间格式
|
|
|
|
StandISO8601Time = "2006-01-02T15:04:05+08:00"
|
|
|
|
)
|
|
|
|
|
2023-08-09 18:43:57 +08:00
|
|
|
// OwnTime ...
|
2023-08-09 18:29:41 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:28 2023/8/9
|
2023-08-09 18:43:57 +08:00
|
|
|
func OwnTime(inputTime time.Time) *Time {
|
2023-08-10 11:08:01 +08:00
|
|
|
instance := &Time{}
|
|
|
|
instance.Time = inputTime
|
|
|
|
return instance
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-10 12:45:12 +08:00
|
|
|
// OwnTimeFromNow 从当前时间获取实例
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:44 2023/8/10
|
|
|
|
func OwnTimeFromNow() *Time {
|
|
|
|
return OwnTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2023-08-09 19:02:23 +08:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-08-10 11:08:01 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-08-09 18:43:57 +08:00
|
|
|
// Time 时间类型
|
2023-08-09 18:29:41 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:23 2023/8/9
|
2023-08-09 18:43:57 +08:00
|
|
|
type Time struct {
|
2023-08-09 18:43:03 +08:00
|
|
|
time.Time
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 1:34 上午 2021/10/7
|
2023-08-09 18:43:57 +08:00
|
|
|
func (t *Time) GetCurrentFormatTime(layout ...string) string {
|
2023-08-10 11:08:01 +08:00
|
|
|
return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatUnixNano 格式化纳秒时间戳
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:54 2022/7/14
|
2023-08-09 18:43:57 +08:00
|
|
|
func (t *Time) FormatUnixNano(layout ...string) string {
|
2023-08-09 18:43:03 +08:00
|
|
|
nano := t.UnixNano() % 1e6
|
2023-08-10 11:08:01 +08:00
|
|
|
return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano)
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatUnixMilli 格式化毫秒时间戳
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:55 2022/7/14
|
2023-08-10 11:08:01 +08:00
|
|
|
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)
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
|
|
|
|
2023-08-10 11:08:01 +08:00
|
|
|
// FormatUnixSec 格式化秒级时间戳
|
2023-08-09 18:29:41 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:06 2022/7/14
|
2023-08-10 11:08:01 +08:00
|
|
|
func (t *Time) FormatUnixSec(layout ...string) string {
|
|
|
|
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
|
2023-08-09 18:29:41 +08:00
|
|
|
}
|
2023-08-09 18:43:03 +08:00
|
|
|
|
2023-08-10 12:18:15 +08:00
|
|
|
// ToString 转字符串
|
2023-08-10 12:17:09 +08:00
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:16 2023/8/10
|
2023-08-10 12:18:15 +08:00
|
|
|
func (t *Time) ToString(layout ...string) string {
|
2023-08-10 12:17:09 +08:00
|
|
|
return t.FormatUnixSec(layout...)
|
|
|
|
}
|
|
|
|
|
2023-08-09 18:43:03 +08:00
|
|
|
// getTimeFormat 获取时间格式
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:37 2023/8/9
|
2023-08-09 18:43:57 +08:00
|
|
|
func (t *Time) getTimeFormat(defaultFormat string, layout ...string) string {
|
2023-08-09 18:43:03 +08:00
|
|
|
if len(layout) > 0 && len(layout[0]) > 0 {
|
|
|
|
return layout[0]
|
|
|
|
}
|
|
|
|
return defaultFormat
|
|
|
|
}
|