github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/tools/time.go (about) 1 package tools 2 3 import ( 4 "sync/atomic" 5 "time" 6 _ "unsafe" 7 ) 8 9 /* 10 Creation Time: 2020 - Apr - 09 11 Created by: (ehsan) 12 Maintainers: 13 1. Ehsan N. Moosa (E2) 14 Auditor: Ehsan N. Moosa (E2) 15 Copyright Ronak Software Group 2020 16 */ 17 18 var ( 19 timeInSec int64 20 ) 21 22 func init() { 23 timeInSec = time.Now().Unix() 24 go func() { 25 for { 26 time.Sleep(time.Second) 27 atomic.AddInt64(&timeInSec, time.Now().Unix()-atomic.LoadInt64(&timeInSec)) 28 } 29 }() 30 } 31 32 func TimeUnix() int64 { 33 return atomic.LoadInt64(&timeInSec) 34 } 35 36 func Duration(t int64) time.Duration { 37 return time.Duration(CPUTicks() - t) 38 } 39 40 // NanoTime returns the current time in nanoseconds from a monotonic clock. 41 //go:linkname NanoTime runtime.nanotime 42 func NanoTime() int64 43 44 // CPUTicks is a faster alternative to NanoTime to measure time duration. 45 //go:linkname CPUTicks runtime.cputicks 46 func CPUTicks() int64