完成时间的包装类型
This commit is contained in:
parent
d64bcfbfd8
commit
5e4a884374
71
time.go
71
time.go
@ -12,16 +12,22 @@ import (
|
||||
"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 {
|
||||
return &Time{
|
||||
inputTime,
|
||||
"2006-01-02 15:04:05", // 标准的时间格式
|
||||
}
|
||||
instance := &Time{}
|
||||
instance.Time = inputTime
|
||||
return instance
|
||||
}
|
||||
|
||||
// OwnTimeFromSecond 从秒获取实例
|
||||
@ -64,6 +70,24 @@ func OwnTimeFromNano(nano int64) *Time {
|
||||
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<白茶清欢>
|
||||
@ -71,7 +95,6 @@ func OwnTimeFromNano(nano int64) *Time {
|
||||
// Date : 18:23 2023/8/9
|
||||
type Time struct {
|
||||
time.Time
|
||||
standTimeFormat string
|
||||
}
|
||||
|
||||
// GetCurrentFormatTime 获取当前时间的格式化时间(秒)
|
||||
@ -80,7 +103,7 @@ type Time struct {
|
||||
//
|
||||
// Date : 1:34 上午 2021/10/7
|
||||
func (t *Time) GetCurrentFormatTime(layout ...string) string {
|
||||
return time.Now().In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...))
|
||||
return time.Now().In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
|
||||
}
|
||||
|
||||
// FormatUnixNano 格式化纳秒时间戳
|
||||
@ -90,8 +113,7 @@ func (t *Time) GetCurrentFormatTime(layout ...string) string {
|
||||
// Date : 11:54 2022/7/14
|
||||
func (t *Time) FormatUnixNano(layout ...string) string {
|
||||
nano := t.UnixNano() % 1e6
|
||||
milli := t.UnixNano() / 1e6
|
||||
return t.FormatUnixMilli(milli, layout...) + fmt.Sprintf(" %v", nano)
|
||||
return t.FormatUnixMilli(layout...) + fmt.Sprintf(" %v", nano)
|
||||
}
|
||||
|
||||
// FormatUnixMilli 格式化毫秒时间戳
|
||||
@ -99,39 +121,18 @@ func (t *Time) FormatUnixNano(layout ...string) string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:55 2022/7/14
|
||||
func (t *Time) FormatUnixMilli(timestamp int64, layout ...string) string {
|
||||
return time.UnixMilli(timestamp).In(time.Local).Format(t.getTimeFormat(t.standTimeFormat, layout...))
|
||||
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 ...
|
||||
// FormatUnixSec 格式化秒级时间戳
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:06 2022/7/14
|
||||
func (t *Time) 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 *Time) 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")
|
||||
func (t *Time) FormatUnixSec(layout ...string) string {
|
||||
return time.Unix(t.Unix(), 0).In(time.Local).Format(t.getTimeFormat(StandTimeFormat, layout...))
|
||||
}
|
||||
|
||||
// getTimeFormat 获取时间格式
|
||||
|
Loading…
Reference in New Issue
Block a user