github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/time.go (about)

     1  package utils
     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  */
    16  
    17  var timeInSec int64
    18  
    19  func init() {
    20  	timeInSec = time.Now().Unix()
    21  
    22  	go func() {
    23  		for {
    24  			time.Sleep(time.Second)
    25  			atomic.AddInt64(&timeInSec, time.Now().Unix()-atomic.LoadInt64(&timeInSec))
    26  		}
    27  	}()
    28  }
    29  
    30  func TimeUnix() int64 {
    31  	return atomic.LoadInt64(&timeInSec)
    32  }
    33  
    34  func TimeUnixSubtract(unixTime int64, d time.Duration) int64 {
    35  	return int64(time.Duration(unixTime) - d/time.Second)
    36  }
    37  
    38  // NanoTime returns the current time in nanoseconds from a monotonic clock.
    39  //
    40  //go:linkname NanoTime runtime.nanotime
    41  func NanoTime() int64
    42  
    43  // CPUTicks is a faster alternative to NanoTime to measure time duration.
    44  //
    45  //go:linkname CPUTicks runtime.cputicks
    46  func CPUTicks() int64