|
|
@ -10,12 +10,12 @@ const (
|
|
|
|
FormatDatetime = "2006-01-02 15:04:05"
|
|
|
|
FormatDatetime = "2006-01-02 15:04:05"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Location 本地時區
|
|
|
|
// Local 本地時區
|
|
|
|
var Location = time.Local
|
|
|
|
var Local = time.Local
|
|
|
|
|
|
|
|
|
|
|
|
// SetLocation 設定時區
|
|
|
|
// SetLocation 設定時區
|
|
|
|
func SetLocation(name string) {
|
|
|
|
func SetLocation(name string) {
|
|
|
|
Location, _ = time.LoadLocation(name)
|
|
|
|
Local, _ = time.LoadLocation(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Timestamp 時間戳
|
|
|
|
// Timestamp 時間戳
|
|
|
@ -26,14 +26,14 @@ func GetUnixNow() Timestamp {
|
|
|
|
return Timestamp(time.Now().Unix())
|
|
|
|
return Timestamp(time.Now().Unix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetUnixMilliNow 取得現在時間戳記
|
|
|
|
// GetUnixNow 取得現在時間戳記
|
|
|
|
func GetUnixMilliNow() Timestamp {
|
|
|
|
func GetUnixNanoNow() Timestamp {
|
|
|
|
return Timestamp(time.Now().UnixMilli())
|
|
|
|
return Timestamp(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetUnix 嘗試取得時間戳記
|
|
|
|
// GetUnix 嘗試取得時間戳記
|
|
|
|
func GetUnix(format, datetime string) (Timestamp, error) {
|
|
|
|
func GetUnix(format, datetime string) (Timestamp, error) {
|
|
|
|
t, err := time.ParseInLocation(format, datetime, Location)
|
|
|
|
t, err := time.ParseInLocation(format, datetime, Local)
|
|
|
|
return Timestamp(t.Unix()), err
|
|
|
|
return Timestamp(t.Unix()), err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -53,11 +53,6 @@ func (t Timestamp) Time() time.Time {
|
|
|
|
return time.Unix(t.Int64(), 0)
|
|
|
|
return time.Unix(t.Int64(), 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TimeMilli 轉為time類型
|
|
|
|
|
|
|
|
func (t Timestamp) TimeMilli() time.Time {
|
|
|
|
|
|
|
|
return time.UnixMilli(t.Int64())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Date 回傳日期字串
|
|
|
|
// Date 回傳日期字串
|
|
|
|
func (t Timestamp) Date() string {
|
|
|
|
func (t Timestamp) Date() string {
|
|
|
|
if t == 0 {
|
|
|
|
if t == 0 {
|
|
|
@ -65,7 +60,7 @@ func (t Timestamp) Date() string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.
|
|
|
|
return time.
|
|
|
|
Unix(t.Int64(), 0).
|
|
|
|
Unix(t.Int64(), 0).
|
|
|
|
In(Location).
|
|
|
|
In(Local).
|
|
|
|
Format(FormatDate)
|
|
|
|
Format(FormatDate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -76,46 +71,26 @@ func (t Timestamp) Datetime() string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.
|
|
|
|
return time.
|
|
|
|
Unix(t.Int64(), 0).
|
|
|
|
Unix(t.Int64(), 0).
|
|
|
|
In(Location).
|
|
|
|
In(Local).
|
|
|
|
Format(FormatDatetime)
|
|
|
|
Format(FormatDatetime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Since 與現在的時間差
|
|
|
|
// NowSub 與現在的時間差
|
|
|
|
func (t Timestamp) Since() time.Duration {
|
|
|
|
func (t Timestamp) NowSub() time.Duration {
|
|
|
|
return time.Since(t.Time())
|
|
|
|
return time.Since(t.Time())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SinceMilli 與現在的時間差
|
|
|
|
|
|
|
|
func (t Timestamp) SinceMilli() time.Duration {
|
|
|
|
|
|
|
|
return time.Since(t.TimeMilli())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Sub 時間差
|
|
|
|
// Sub 時間差
|
|
|
|
func (t Timestamp) Sub(t2 time.Time) time.Duration {
|
|
|
|
func (t Timestamp) Sub(t2 time.Time) time.Duration {
|
|
|
|
return t2.Sub(t.Time())
|
|
|
|
return t2.Sub(t.Time())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SubMilli 時間差
|
|
|
|
|
|
|
|
func (t Timestamp) SubMilli(t2 time.Time) time.Duration {
|
|
|
|
|
|
|
|
return t2.Sub(t.TimeMilli())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// After 是否在指定時間後
|
|
|
|
// After 是否在指定時間後
|
|
|
|
func (t Timestamp) After(t2 time.Time) bool {
|
|
|
|
func (t Timestamp) After(t2 time.Time) bool {
|
|
|
|
return t.Time().After(t2)
|
|
|
|
return t.Time().After(t2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AfterMilli 是否在指定時間後
|
|
|
|
|
|
|
|
func (t Timestamp) AfterMilli(t2 time.Time) bool {
|
|
|
|
|
|
|
|
return t.TimeMilli().After(t2)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AfterNow 是否在目前時間後
|
|
|
|
// AfterNow 是否在目前時間後
|
|
|
|
func (t Timestamp) AfterNow() bool {
|
|
|
|
func (t Timestamp) AfterNow() bool {
|
|
|
|
return t.After(time.Now())
|
|
|
|
return t.After(time.Now())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AfterMilliNow 是否在目前時間後
|
|
|
|
|
|
|
|
func (t Timestamp) AfterMilliNow() bool {
|
|
|
|
|
|
|
|
return t.AfterMilli(time.Now())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|