You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/types/timestamp.go

50 lines
778 B
Go

package types
import (
"time"
)
// 格式化
const (
FormatDatetime = "2006-01-02 15:04:05"
)
var Local = time.Local
func SetLocation(name string) {
Local, _ = time.LoadLocation(name)
}
// Timestamp 時間戳
type Timestamp int64
func GetUnixNow() Timestamp {
return Timestamp(time.Now().Unix())
}
func (t Timestamp) Int64() int64 {
return int64(t)
}
func (t Timestamp) Time() time.Time {
return time.Unix(t.Int64(), 0)
}
// Datetime 回傳時間字串
func (t Timestamp) Datetime() string {
return time.
Unix(t.Int64(), 0).
In(Local).
Format(FormatDatetime)
}
// 與現在的時間差
func (t Timestamp) NowSub() time.Duration {
return time.Since(t.Time())
}
// 時間差
func (t Timestamp) Sub(t2 time.Time) time.Duration {
return t2.Sub(t.Time())
}