mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-01 14:47:38 +00:00
41 lines
860 B
Go
41 lines
860 B
Go
|
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())
|
||
|
atomic.StoreUint64(¤tTimestamp, t)
|
||
|
}
|
||
|
}()
|
||
|
}
|
||
|
|
||
|
var currentTimestamp = uint64(time.Now().Unix())
|
||
|
|
||
|
// UnixTimestamp returns the current unix timestamp in seconds.
|
||
|
//
|
||
|
// It is faster than time.Now().Unix()
|
||
|
func UnixTimestamp() uint64 {
|
||
|
return atomic.LoadUint64(¤tTimestamp)
|
||
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|