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

55 lines
933 B
Go

4 years ago
package types
import (
"time"
)
// 格式化
const (
FormatDatetime = "2006-01-02 15:04:05"
)
4 years ago
// Local 本地時區
4 years ago
var Local = time.Local
4 years ago
// SetLocation 設定時區
4 years ago
func SetLocation(name string) {
Local, _ = time.LoadLocation(name)
}
// Timestamp 時間戳
type Timestamp int64
4 years ago
4 years ago
// GetUnixNow 取得現在時間戳記
4 years ago
func GetUnixNow() Timestamp {
return Timestamp(time.Now().Unix())
}
4 years ago
// Int64 轉為64位元整數
4 years ago
func (t Timestamp) Int64() int64 {
return int64(t)
}
4 years ago
// Time 轉為time類型
4 years ago
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)
}
4 years ago
// NowSub 與現在的時間差
4 years ago
func (t Timestamp) NowSub() time.Duration {
return time.Since(t.Time())
}
4 years ago
// Sub 時間差
4 years ago
func (t Timestamp) Sub(t2 time.Time) time.Duration {
return t2.Sub(t.Time())
}