2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: getClockTime.c,v 1.8 1999/10/26 09:34:09 sof Exp $
6 * getClockTime Runtime Support
10 #define NON_POSIX_SOURCE /* gettimeofday */
16 /* Note: skewing this code in favour of non-POSIX calls
17 such as gettimeofday() and getclock() rather than time(),
18 may seem strange/wrong. There's a good reason for it
19 though - the non-POSIX calls gives you better precision --
20 they return usecs (or nsecs) as well as seconds, which
21 the users of getClockTime() is interested in knowing.
24 #if defined(HAVE_GETTIMEOFDAY)
25 # ifdef HAVE_SYS_TIME_H
26 # include <sys/time.h>
28 #elif defined(HAVE_GETCLOCK)
29 # ifdef HAVE_SYS_TIMERS_H
31 # include <sys/timers.h>
33 #elif defined(HAVE_TIME_H)
39 #include <sys/types.h>
40 #include <sys/timeb.h>
44 getClockTime(StgByteArray sec, StgByteArray nsec)
46 #if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
48 * ftime() as implemented by cygwin (in B20.1) is
49 * not right, so stay away & use time() there instead.
55 ((unsigned int *)sec)[0] = (unsigned int)t.time;
56 ((unsigned int *)nsec)[0] = (unsigned int)t.millitm * 1000;
58 #elif defined(HAVE_GETTIMEOFDAY)
61 if (gettimeofday(&tp, NULL) != 0) {
66 ((unsigned long int *)sec)[0] = tp.tv_sec;
67 ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
69 #elif defined(HAVE_GETCLOCK)
72 if (getclock(TIMEOFDAY, &tp) != 0) {
77 ((unsigned long int *)sec)[0] = tp.tv_sec;
78 ((unsigned long int *)nsec)[0] = tp.tv_nsec;
80 #elif defined(HAVE_TIME_H)
82 if ((t = time(NULL)) == (time_t) -1) {
87 ((unsigned long int *)sec)[0] = t;
88 ((unsigned long int *)nsec)[0] = 0;
91 #error "getClockTime: don't know how to get at the clock's time"