Created
December 1, 2013 16:38
-
-
Save dreid/7736422 to your computer and use it in GitHub Desktop.
This file contains 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
from cffi import FFI | |
ffi = FFI() | |
ffi.cdef(""" | |
typedef long int time_t; | |
struct timespec { | |
time_t tv_sec; | |
long tv_nsec; | |
}; | |
int gettime(struct timespec *); | |
""") | |
lib = ffi.verify(""" | |
#include <time.h> | |
#include <mach/clock.h> | |
#include <mach/mach.h> | |
int gettime(struct timespec* ts) { | |
clock_serv_t cclock; | |
mach_timespec_t mts; | |
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); | |
clock_get_time(cclock, &mts); | |
mach_port_deallocate(mach_task_self(), cclock); | |
ts->tv_sec = mts.tv_sec; | |
ts->tv_nsec = mts.tv_nsec; | |
return 0; | |
}; | |
""") | |
def gettime(): | |
ts = ffi.new('struct timespec *') | |
lib.gettime(ts) | |
return (ts.tv_sec * 1000000000) + ts.tv_nsec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment