github.com/ScaleFT/monotime@v0.0.0-20180209174256-2da272f3dad0/clock_mach_absolute_time.go (about)

     1  // +build darwin
     2  // +build !go1.9
     3  
     4  package monotime
     5  
     6  // #include <mach/mach_time.h>
     7  import "C"
     8  
     9  import (
    10  	"time"
    11  )
    12  
    13  var tbinfo C.struct_mach_timebase_info
    14  
    15  func init() {
    16  	C.mach_timebase_info(&tbinfo)
    17  }
    18  
    19  func monotime() Monotime {
    20  	return Monotime(C.mach_absolute_time())
    21  }
    22  
    23  func duration(start Monotime, end Monotime) time.Duration {
    24  	elapsed := end - start
    25  	elapsedNano := elapsed * uint64(tbinfo.numer) / uint64(tbinfo.denom)
    26  	return time.Duration(elapsedNano)
    27  }