[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / toClockSec.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: toClockSec.c,v 1.3 1998/12/02 13:28:01 simonm Exp $
5  *
6  * toClockSec Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "timezone.h"
11 #include "stgio.h"
12
13 StgAddr 
14 toClockSec(I_ year, I_ mon, I_ mday, I_ hour, I_ min, I_ sec, I_ isdst, StgByteArray res)
15 {
16     struct tm tm;
17     time_t t;
18
19     tm.tm_year = year - 1900;
20     tm.tm_mon = mon;
21     tm.tm_mday = mday;
22     tm.tm_hour = hour;
23     tm.tm_min = min;
24     tm.tm_sec = sec;
25     tm.tm_isdst = isdst;
26
27 #ifdef HAVE_MKTIME
28     t = mktime(&tm);
29 #else
30 #ifdef HAVE_TIMELOCAL
31     t = timelocal(&tm);
32 #else
33     t = (time_t) -1;
34 #endif
35 #endif
36     if (t == (time_t) -1)
37         return NULL;
38
39     *(time_t *)res = t;
40     return res;
41 }
42
43 StgInt
44 prim_toClockSec(I_ year, I_ mon, I_ mday, I_ hour, I_ min, I_ sec, I_ isdst, StgByteArray res)
45 {
46     struct tm tm;
47     time_t t;
48
49     tm.tm_year = year - 1900;
50     tm.tm_mon = mon;
51     tm.tm_mday = mday;
52     tm.tm_hour = hour;
53     tm.tm_min = min;
54     tm.tm_sec = sec;
55     tm.tm_isdst = isdst;
56
57 #ifdef HAVE_MKTIME
58     t = mktime(&tm);
59 #else
60 #ifdef HAVE_TIMELOCAL
61     t = timelocal(&tm);
62 #else
63     t = (time_t) -1;
64 #endif
65 #endif
66     if (t == (time_t) -1)
67         return 0;
68
69     *(time_t *)res = t;
70     return 1;
71 }