2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: getClockTime.c,v 1.7 1999/10/20 10:08:33 sof Exp $
6 * getClockTime Runtime Support
12 /* Note: skewing this code in favour of non-POSIX calls
13 such as gettimeofday() and getclock() rather than time(),
14 may seem strange/wrong. There's a good reason for it
15 though - the non-POSIX calls gives you better precision --
16 they return usecs (or nsecs) as well as seconds, which
17 the users of getClockTime() is interested in knowing.
20 #if defined(HAVE_GETTIMEOFDAY)
21 # ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
24 #elif defined(HAVE_GETCLOCK)
25 # ifdef HAVE_SYS_TIMERS_H
27 # include <sys/timers.h>
29 #elif defined(HAVE_TIME_H)
35 #include <sys/types.h>
36 #include <sys/timeb.h>
40 getClockTime(StgByteArray sec, StgByteArray nsec)
42 #if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
44 * ftime() as implemented by cygwin (in B20.1) is
45 * not right, so stay away & use time() there instead.
51 ((unsigned int *)sec)[0] = (unsigned int)t.time;
52 ((unsigned int *)nsec)[0] = (unsigned int)t.millitm * 1000;
54 #elif defined(HAVE_GETTIMEOFDAY)
57 if (gettimeofday(&tp, NULL) != 0) {
62 ((unsigned long int *)sec)[0] = tp.tv_sec;
63 ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
65 #elif defined(HAVE_GETCLOCK)
68 if (getclock(TIMEOFDAY, &tp) != 0) {
73 ((unsigned long int *)sec)[0] = tp.tv_sec;
74 ((unsigned long int *)nsec)[0] = tp.tv_nsec;
76 #elif defined(HAVE_TIME_H)
78 if ((t = time(NULL)) == (time_t) -1) {
83 ((unsigned long int *)sec)[0] = t;
84 ((unsigned long int *)nsec)[0] = 0;
87 #error "getClockTime: don't know how to get at the clock's time"