[project @ 2000-06-19 13:28:35 by simonmar]
[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.6 2000/06/19 13:28:35 simonmar Exp $
5  *
6  * toClockSec Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11 #include "timezone.h"
12
13 StgInt
14 toClockSec(I_ year, I_ mon, I_ mday, I_ hour, I_ min, I_ sec, I_ tz, 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 #elif defined(HAVE_TIMELOCAL)
30     t = timelocal(&tm);
31 #else
32     t = (time_t) -1;
33 #endif
34     if (t == (time_t) -1)
35         return 0;
36     /*
37      * mktime expects its argument to be in the local timezone, but
38      * toUTCTime makes UTC-encoded CalendarTime's ...
39      *
40      * Since there is no any_tz_struct_tm-to-time_t conversion
41      * function, we have to fake one... :-) If not in all, it works in
42      * most cases (before, it was the other way round...)
43      *
44      * Luckily, mktime tells us, what it *thinks* the timezone is, so,
45      * to compensate, we add the timezone difference to mktime's
46      * result.
47      */
48     *(time_t *)res = t + tz - GMTOFF(&tm);
49     return 1;
50 }