CTIME for Mac OS X

Apologies if this has been posted before. I just wanted to share the code for making Casey's CTIME application compile in the Mac OS X. All you have to do is add the following lines to the top of the ctime.cpp file, below the other includes:

1
2
3
4
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif


and then replace the #endif at the end of this part of the code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#ifdef _WIN32

#include <windows.h>

static int unsigned
GetClock(void)
{
    if(sizeof(int unsigned) != sizeof(DWORD))
    {
        fprintf(stderr, "ERROR: Unexpected integer size - timing will not work on this platform!\n");
    }

    return(timeGetTime());
}

#endif


with this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#elif defined(__MACH__)

static int unsigned
GetClock(void)
{
   // from:
   // http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
   //
   struct timespec TimeSpec;
   clock_serv_t CClock;
   mach_timespec_t Mts;
   int unsigned Result;
   
   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &CClock);
   clock_get_time(CClock, &Mts);
   mach_port_deallocate(mach_task_self(), CClock);
   TimeSpec.tv_sec = Mts.tv_sec;
   TimeSpec.tv_nsec = Mts.tv_nsec;
   Result = TimeSpec.tv_sec * 1000 + TimeSpec.tv_nsec / 1000000;

   return Result;
}

#endif


Hope this helps somebody.

A.

Edited by AM on


Edited by AM on
Hah, good to know, sorry for the noise. I could not find a search function to see if this was discussed anywhere in here.