[project @ 1998-04-10 11:04:49 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / getClockTime.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: getClockTime.c,v 1.1 1998/04/10 10:54:35 simonm Exp $
5  *
6  * getClockTime Runtime Support
7  */
8
9 #ifndef _AIX
10 #define NON_POSIX_SOURCE    /* gettimeofday */
11 #endif
12
13 #include "Rts.h"
14 #include "stgio.h"
15
16 #ifdef HAVE_GETCLOCK
17
18 # ifdef HAVE_SYS_TIMERS_H
19 #  define POSIX_4D9 1
20 #  include <sys/timers.h>
21 # endif
22
23 #else
24 # ifdef HAVE_GETTIMEOFDAY
25
26 #  ifdef HAVE_SYS_TIME_H
27 #   include <sys/time.h>
28 #  endif
29
30 # else
31
32 #  ifdef HAVE_TIME_H
33 #   include <time.h>
34 #  endif
35
36 # endif
37 #endif
38
39 StgInt
40 getClockTime(StgByteArray sec, StgByteArray nsec)
41 {
42 #ifdef HAVE_GETCLOCK
43     struct timespec tp;
44
45     if (getclock(TIMEOFDAY, &tp) != 0) {
46         cvtErrno();
47         stdErrno();
48         return -1;
49     }
50     ((unsigned long int *)sec)[0] = tp.tv_sec;
51     ((unsigned long int *)nsec)[0] = tp.tv_nsec;
52     return 0;
53 #else
54 #ifdef HAVE_GETTIMEOFDAY
55     struct timeval tp;
56  
57     if (gettimeofday(&tp, NULL) != 0) {
58         cvtErrno();
59         stdErrno();
60         return -1;
61     }
62     ((unsigned long int *)sec)[0] = tp.tv_sec;
63     ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
64     return 0;
65 #else
66     time_t t;
67     if ((t = time(NULL)) == (time_t) -1) {
68         cvtErrno();
69         stdErrno();
70         return -1;
71     }
72     ((unsigned long int *)sec)[0] = t;
73     ((unsigned long int *)nsec)[0] = 0;
74     return 0;
75 #endif
76 #endif
77 }