github.com/go-eden/common@v0.1.15-0.20210617133546-059099253264/etime/etime_now.go (about) 1 package etime 2 3 import ( 4 "syscall" 5 "time" 6 ) 7 8 var zeroUs = NowUs() - 1 9 10 // NowSecond obtains the current second, use syscall for better performance 11 func NowSecond() uint32 { 12 var tv syscall.Timeval 13 if err := syscall.Gettimeofday(&tv); err != nil { 14 return uint32(time.Now().Unix()) 15 } 16 return uint32(tv.Sec) 17 } 18 19 // NowMillisecond obtains the current millisecond, use syscall for better performance 20 func NowMillisecond() int64 { 21 var tv syscall.Timeval 22 if err := syscall.Gettimeofday(&tv); err != nil { 23 return time.Now().UnixNano() / 1e6 24 } 25 return int64(tv.Sec)*1e3 + int64(tv.Usec)/1e3 26 } 27 28 // NowMicrosecond obtains the current microsecond, use syscall for better performance 29 func NowMicrosecond() int64 { 30 var tv syscall.Timeval 31 if err := syscall.Gettimeofday(&tv); err != nil { 32 return time.Now().UnixNano() / 1e3 33 } 34 return int64(tv.Sec)*1e6 + int64(tv.Usec) 35 } 36 37 // NowMs is the alise for NowMillisecond 38 func NowMs() int64 { 39 return NowMillisecond() 40 } 41 42 // NowUs is the alise for NowMicrosecond 43 func NowUs() int64 { 44 return NowMicrosecond() 45 } 46 47 // NowNs returns the current nanosecond 48 func NowNs() int64 { 49 return time.Now().UnixNano() 50 } 51 52 func UptimeUs() int64 { 53 return NowUs() - zeroUs 54 } 55 56 func UptimeMs() int64 { 57 return UptimeUs() / 1000 58 }