2020-05-14 19:01:51 +00:00
|
|
|
package fasttime
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for tm := range ticker.C {
|
|
|
|
t := uint64(tm.Unix())
|
2024-02-24 00:07:51 +00:00
|
|
|
currentTimestamp.Store(t)
|
2020-05-14 19:01:51 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2024-02-24 00:07:51 +00:00
|
|
|
var currentTimestamp = func() *atomic.Uint64 {
|
|
|
|
var x atomic.Uint64
|
|
|
|
x.Store(uint64(time.Now().Unix()))
|
|
|
|
return &x
|
|
|
|
}()
|
2020-05-14 19:01:51 +00:00
|
|
|
|
|
|
|
// UnixTimestamp returns the current unix timestamp in seconds.
|
|
|
|
//
|
|
|
|
// It is faster than time.Now().Unix()
|
|
|
|
func UnixTimestamp() uint64 {
|
2024-02-24 00:07:51 +00:00
|
|
|
return currentTimestamp.Load()
|
2020-05-14 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnixDate returns date from the current unix timestamp.
|
|
|
|
//
|
|
|
|
// The date is calculated by dividing unix timestamp by (24*3600)
|
|
|
|
func UnixDate() uint64 {
|
|
|
|
return UnixTimestamp() / (24 * 3600)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnixHour returns hour from the current unix timestamp.
|
|
|
|
//
|
|
|
|
// The hour is calculated by dividing unix timestamp by 3600
|
|
|
|
func UnixHour() uint64 {
|
|
|
|
return UnixTimestamp() / 3600
|
|
|
|
}
|