时间工具支持传入layout

This commit is contained in:
白茶清欢 2023-04-08 14:38:13 +08:00
parent 812faa2e21
commit f5fddb92e8
1 changed files with 16 additions and 6 deletions

22
time.go
View File

@ -18,6 +18,7 @@ import (
//
// Date : 15:24 2022/5/14
type ownTime struct {
format string
}
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
@ -34,10 +35,13 @@ func (ot *ownTime) GetCurrentFormatTime() string {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/14
func (ot *ownTime) FormatUnixNano(timestamp int64) string {
func (ot *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 ot.FormatUnixMilli(milli) + fmt.Sprintf(" %v", nano)
return ot.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano)
}
// FormatUnixMilli 格式化毫秒时间戳
@ -45,8 +49,11 @@ func (ot *ownTime) FormatUnixNano(timestamp int64) string {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:55 2022/7/14
func (ot *ownTime) FormatUnixMilli(timestamp int64) string {
return time.UnixMilli(timestamp).In(time.Local).Format("2006-01-02 15:04:05.000")
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])
}
// FormatUnixSec ...
@ -54,8 +61,11 @@ func (ot *ownTime) FormatUnixMilli(timestamp int64) string {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2022/7/14
func (ot *ownTime) FormatUnixSec(timestamp int64) string {
return time.Unix(timestamp, 0).In(time.Local).Format("2006-01-02 15:04:05")
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])
}
// ParseISO8601Time 解析 2006-01-02T15:04:05+08:00 格式时间 -> 2006-01-02 15:04:05