Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
work around lack of clock_gettime in os x
/*
author: jbenet
os x, compile with: gcc -o testo test.c
linux, compile with: gcc -o testo test.c -lrt
*/
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
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
}
int main(int argc, char **argv) {
struct timespec ts;
current_utc_time(&ts);
printf("s: %lu\n", ts.tv_sec);
printf("ns: %lu\n", ts.tv_nsec);
return 0;
}
@MacDada

This comment has been minimized.

Show comment Hide comment
@MacDada

MacDada Jan 23, 2014

thx, it works! ;-)

MacDada commented Jan 23, 2014

thx, it works! ;-)

@danielgindi

This comment has been minimized.

Show comment Hide comment
@danielgindi

danielgindi Jul 14, 2014

Thanks!!!

Thanks!!!

@danielgindi

This comment has been minimized.

Show comment Hide comment
@danielgindi

danielgindi Jul 14, 2014

I really appreciate it.

I really appreciate it.

@kazumaishio

This comment has been minimized.

Show comment Hide comment
@kazumaishio

kazumaishio Dec 1, 2014

Thanks!!!

Thanks!!!

@wujun

This comment has been minimized.

Show comment Hide comment
@wujun

wujun Feb 3, 2015

thanks your code, thanks

wujun commented Feb 3, 2015

thanks your code, thanks

@Superlokkus

This comment has been minimized.

Show comment Hide comment
@Superlokkus

Superlokkus Mar 25, 2015

THX

THX

@tawantinsuyu

This comment has been minimized.

Show comment Hide comment
@tawantinsuyu

tawantinsuyu May 9, 2015

Awesome! Thanks

Awesome! Thanks

@alfwatt

This comment has been minimized.

Show comment Hide comment
@matrumz

This comment has been minimized.

Show comment Hide comment
@matrumz

matrumz Nov 17, 2015

Many thanks. My senior project is that much closer to completion!

matrumz commented Nov 17, 2015

Many thanks. My senior project is that much closer to completion!

@raymondtay

This comment has been minimized.

Show comment Hide comment
@raymondtay

raymondtay Nov 24, 2015

thanks for sharing,appreciate it.

thanks for sharing,appreciate it.

@rraallvv

This comment has been minimized.

Show comment Hide comment
@rraallvv

rraallvv Apr 29, 2016

I need the return value in clock_get_time(..), how can it be done os OS X?
For details please see https://github.com/microflo/microflo/blob/master/microflo/linux.hpp#L68

I need the return value in clock_get_time(..), how can it be done os OS X?
For details please see https://github.com/microflo/microflo/blob/master/microflo/linux.hpp#L68

@aczzdx

This comment has been minimized.

Show comment Hide comment
@aczzdx

aczzdx Nov 14, 2017

Thanks for sharing. This code helps me a lot.

aczzdx commented Nov 14, 2017

Thanks for sharing. This code helps me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment