Created
February 27, 2014 09:33
-
-
Save christopherobin/9247060 to your computer and use it in GitHub Desktop.
Golang Monotonic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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