Since Mac OS X doesn't support get time, I found this gist that made a portable way to use the clock_gettime fund here:
void current_utc_time(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_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;
#else
clock_gettime(CLOCK_REALTIME, ts);
#endif
}
using it like this
struct timespec requestStart;
current_utc_time(&requestStart);
printf("start: s: %lu\n", requestStart.tv_sec);
printf("start: ns: %lu\n", requestStart.tv_nsec);
start: s: 1435988139
start: ns: 202015000
I am trying to get the seconds and milliseconds value from this code, is this the correct way to get milliseconds from the nanoseconds value?
printf("total: ms: %lu\n", (requestStart.tv_nsec - requestEnd.tv_nsec) / (unsigned long)1000000);
If so, how do I get the seconds value? If not how can I get the milliseconds and seconds value?
edit In response to the first comment, I am mainly looking for a way to have the code as portable as possible.