2022-05-14 13:45:51 +08:00
|
|
|
// Package util ...
|
|
|
|
//
|
|
|
|
// Description : 时间相关操作
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-10-07 1:33 上午
|
|
|
|
package util
|
|
|
|
|
2022-07-14 13:49:40 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2022-05-14 13:45:51 +08:00
|
|
|
|
2022-05-14 15:26:13 +08:00
|
|
|
// ownTime ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 15:24 2022/5/14
|
|
|
|
type ownTime struct {
|
2023-04-08 14:38:13 +08:00
|
|
|
format string
|
2022-05-14 15:26:13 +08:00
|
|
|
}
|
|
|
|
|
2022-05-14 13:45:51 +08:00
|
|
|
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 1:34 上午 2021/10/7
|
2023-04-08 14:48:53 +08:00
|
|
|
func (ot *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])
|
2022-05-14 13:45:51 +08:00
|
|
|
}
|
2022-07-14 13:49:40 +08:00
|
|
|
|
|
|
|
// FormatUnixNano 格式化纳秒时间戳
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:54 2022/7/14
|
2023-04-08 14:38:13 +08:00
|
|
|
func (ot *ownTime) FormatUnixNano(timestamp int64, layout ...string) string {
|
|
|
|
if len(layout) == 0 {
|
|
|
|
layout = []string{"2006-01-02 15:04:05.000"}
|
|
|
|
}
|
2022-07-14 13:49:40 +08:00
|
|
|
nano := timestamp % 1e6
|
|
|
|
milli := timestamp / 1e6
|
2023-04-08 14:38:13 +08:00
|
|
|
return ot.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano)
|
2022-07-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatUnixMilli 格式化毫秒时间戳
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 11:55 2022/7/14
|
2023-04-08 14:38:13 +08:00
|
|
|
func (ot *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])
|
2022-07-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatUnixSec ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:06 2022/7/14
|
2023-04-08 14:38:13 +08:00
|
|
|
func (ot *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])
|
2022-07-14 13:49:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (ot *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 ""
|
|
|
|
}
|
|
|
|
|
2023-03-19 14:13:11 +08:00
|
|
|
return formatTime.In(time.Local).Format("2006-01-02 15:04:05")
|
2022-07-14 13:49:40 +08:00
|
|
|
}
|