github.com/nikandfor/hacked@v0.0.0-20230429073333-a318d546207a/htime/low.go (about) 1 package htime 2 3 import ( 4 "time" 5 "unsafe" 6 _ "unsafe" 7 ) 8 9 type ( 10 timer struct { 11 C <-chan time.Time 12 r runtimeTimer 13 } 14 15 // Interface to timers implemented in package runtime. 16 // Must be in sync with ../runtime/time.go:/^type timer 17 runtimeTimer struct { 18 pp uintptr 19 when int64 20 period int64 21 f func(interface{}, uintptr) // NOTE: must not be closure 22 arg interface{} 23 seq uintptr 24 nextwhen int64 25 status uint32 26 } 27 ) 28 29 //go:linkname Now time.now 30 func Now() (sec int64, nsec int32, mono int64) 31 32 func UnixNano() (t int64) { 33 t, nsec, _ := Now() 34 35 return t*1e9 + int64(nsec) 36 } 37 38 func Monotonic() (c int64) { 39 _, _, c = Now() 40 41 return 42 } 43 44 func MonotonicOf(t time.Time) int64 { 45 return mono(&t) 46 } 47 48 // DateClock is faster version of t.Date(); t.Clock(). 49 func DateClock(t time.Time) (year, month, day, hour, min, sec int) { //nolint:gocritic 50 u := timeAbs(t) 51 year, month, day, _ = absDate(u, true) 52 hour, min, sec = absClock(u) 53 return 54 } 55 56 func NewTimerSync(d time.Duration, f func()) *time.Timer { 57 t := &timer{ 58 r: runtimeTimer{ 59 when: when(d), 60 f: doSync, 61 arg: f, 62 }, 63 } 64 65 startTimer(&t.r) 66 67 return (*time.Timer)(unsafe.Pointer(t)) 68 } 69 70 func doSync(arg interface{}, seq uintptr) { 71 arg.(func())() 72 } 73 74 //go:linkname timeAbs time.Time.abs 75 func timeAbs(time.Time) uint64 76 77 //go:linkname absClock time.absClock 78 func absClock(uint64) (hour, min, sec int) 79 80 //go:linkname absDate time.absDate 81 func absDate(uint64, bool) (year, month, day, yday int) 82 83 //go:noescape 84 //go:linkname mono time.(*Time).mono 85 func mono(*time.Time) int64 86 87 //go:linkname startTimer time.startTimer 88 func startTimer(*runtimeTimer) 89 90 //go:linkname when time.when 91 func when(time.Duration) int64