Skip to content

Instantly share code, notes, and snippets.

@christopherobin
Created February 27, 2014 09:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherobin/9247060 to your computer and use it in GitHub Desktop.
Save christopherobin/9247060 to your computer and use it in GitHub Desktop.
Golang Monotonic
package main
// #include <time.h>
import "C"
import (
"fmt"
)
type TimeSpec struct {
Seconds int64
NanoSeconds int64
}
func (ts TimeSpec) String() string {
return fmt.Sprintf("%d.%d", ts.Seconds, ts.NanoSeconds)
}
func GetMonotonicTime() TimeSpec {
var ts C.struct_timespec
// see http://man7.org/linux/man-pages/man2/clock_gettime.2.html for a list of CLOCK_ constants
C.clock_gettime(C.CLOCK_MONOTONIC_RAW, &ts)
// convert to something easier to manipulate
return TimeSpec{int64(ts.tv_sec), int64(ts.tv_nsec)}
}
func main() {
ts := GetMonotonicTime()
fmt.Println(ts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment