1-- |
2-- Module      :  Foundation.System.Bindings.Time
3-- Maintainer  :  Haskell foundation
4--
5
6module Foundation.System.Bindings.Time where
7
8import Basement.Compat.Base
9import Basement.Compat.C.Types
10import Basement.Types.OffsetSize
11
12#include <time.h>
13#include <sys/time.h>
14#include "foundation_system.h"
15
16type CClockId = CInt
17data CTimeSpec
18data CTimeVal
19data CTimeZone
20
21size_CTimeSpec :: CSize
22size_CTimeSpec = #const sizeof(struct timespec)
23
24ofs_CTimeSpec_Seconds :: Offset Word8
25ofs_CTimeSpec_Seconds = Offset (#offset struct timespec, tv_sec)
26
27ofs_CTimeSpec_NanoSeconds :: Offset Word8
28ofs_CTimeSpec_NanoSeconds = Offset (#offset struct timespec, tv_nsec)
29
30size_CTimeVal :: CSize
31size_CTimeVal = #const sizeof(struct timeval)
32
33size_CTimeZone :: CSize
34size_CTimeZone = #const sizeof(struct timezone)
35
36size_CTimeT :: CSize
37size_CTimeT = #const sizeof(time_t)
38
39------------------------------------------------------------------------
40#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
41
42#define FOUNDATION_CLOCK_REALTIME 0
43#define FOUNDATION_CLOCK_MONOTONIC 1
44#define FOUNDATION_CLOCK_PROCESS_CPUTIME_ID 2
45#define FOUNDATION_CLOCK_THREAD_CPUTIME_ID 3
46
47#endif
48
49
50sysTime_CLOCK_REALTIME :: CClockId
51#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
52sysTime_CLOCK_REALTIME = (#const FOUNDATION_CLOCK_REALTIME)
53#else
54sysTime_CLOCK_REALTIME = (#const CLOCK_REALTIME)
55#endif
56
57sysTime_CLOCK_MONOTONIC :: CClockId
58#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
59sysTime_CLOCK_MONOTONIC = (#const FOUNDATION_CLOCK_MONOTONIC)
60#else
61sysTime_CLOCK_MONOTONIC = (#const CLOCK_MONOTONIC)
62#endif
63
64sysTime_CLOCK_PROCESS_CPUTIME_ID :: CClockId
65#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
66sysTime_CLOCK_PROCESS_CPUTIME_ID = (#const FOUNDATION_CLOCK_PROCESS_CPUTIME_ID)
67#else
68sysTime_CLOCK_PROCESS_CPUTIME_ID = (#const CLOCK_PROCESS_CPUTIME_ID)
69#endif
70
71sysTime_CLOCK_THREAD_CPUTIME_ID :: CClockId
72#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
73sysTime_CLOCK_THREAD_CPUTIME_ID = (#const FOUNDATION_CLOCK_THREAD_CPUTIME_ID)
74#else
75sysTime_CLOCK_THREAD_CPUTIME_ID = (#const CLOCK_THREAD_CPUTIME_ID)
76#endif
77
78#ifdef CLOCK_MONOTONIC_RAW
79sysTime_CLOCK_MONOTONIC_RAW :: CClockId
80sysTime_CLOCK_MONOTONIC_RAW = (#const CLOCK_MONOTONIC_RAW)
81#endif
82
83#ifdef CLOCK_REALTIME_COARSE
84sysTime_CLOCK_REALTIME_COARSE :: CClockId
85sysTime_CLOCK_REALTIME_COARSE = (#const CLOCK_REALTIME_COARSE)
86#endif
87
88#ifdef CLOCK_MONOTIC_COARSE
89sysTime_CLOCK_MONOTONIC_COARSE :: CClockId
90sysTime_CLOCK_MONOTONIC_COARSE = (#const CLOCK_MONOTONIC_COARSE)
91#endif
92
93#ifdef CLOCK_BOOTTIME
94sysTime_CLOCK_BOOTTIME :: CClockId
95sysTime_CLOCK_BOOTTIME = (#const CLOCK_BOOTTIME)
96#endif
97
98#ifdef CLOCK_REALTIME_ALARM
99sysTime_CLOCK_REALTIME_ALARM :: CClockId
100sysTime_CLOCK_REALTIME_ALARM = (#const CLOCK_REALTIME_ALARM)
101#endif
102
103#ifdef CLOCK_BOOTTIME_ALARM
104sysTime_CLOCK_BOOTTIME_ALARM :: CClockId
105sysTime_CLOCK_BOOTTIME_ALARM = (#const CLOCK_BOOTTIME_ALARM)
106#endif
107
108#ifdef CLOCK_TAI
109sysTime_CLOCK_TAI :: CClockId
110sysTime_CLOCK_TAI = (#const CLOCK_TAI)
111#endif
112
113#ifdef FOUNDATION_SYSTEM_API_NO_CLOCK
114foreign import ccall unsafe "foundation_time_clock_getres"
115    sysTimeClockGetRes :: CClockId -> Ptr CTimeSpec -> IO CInt
116foreign import ccall unsafe "foundation_time_clock_gettime"
117    sysTimeClockGetTime :: CClockId -> Ptr CTimeSpec -> IO CInt
118#else
119foreign import ccall unsafe "clock_getres"
120    sysTimeClockGetRes :: CClockId -> Ptr CTimeSpec -> IO CInt
121foreign import ccall unsafe "clock_gettime"
122    sysTimeClockGetTime :: CClockId -> Ptr CTimeSpec -> IO CInt
123#endif
124
125foreign import ccall unsafe "gettimeofday"
126    sysTimeGetTimeOfDay :: Ptr CTimeVal -> Ptr CTimeZone -> IO CInt
127