[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / toUTCTime.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[toUTCTime.lc]{toUTCTime Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "timezone.h"
10 #include "stgio.h"
11
12 #ifdef cygwin32_TARGET_OS
13 extern char *_tzname;
14 char *tzname;
15 #endif
16
17 StgAddr 
18 toUTCTime(I_ size, StgByteArray d, StgByteArray res)
19 {
20     time_t t;
21     struct tm *tm,*tmp=(struct tm *)res;
22
23     switch(size) {
24         default:
25             return NULL;
26         case 0:
27             t = 0;
28             break;
29         case -1:
30             t = - (time_t) ((StgInt *)d)[0];
31             if (t > 0) 
32                 return NULL;
33             break;
34         case 1:
35             t = (time_t) ((StgInt *)d)[0];
36             if (t < 0) 
37                 return NULL;
38             break;
39         }
40     tm = gmtime(&t);
41     
42     if (tm == NULL)
43         return NULL;
44
45     /*
46       gmtime() may return a ptr to statically allocated storage,
47       so to make toUTCTime reentrant, we manually copy
48       the structure into the (struct tm *) passed in.
49     */
50     tmp->tm_sec    = tm->tm_sec;
51     tmp->tm_min    = tm->tm_min;
52     tmp->tm_hour   = tm->tm_hour;
53     tmp->tm_mday   = tm->tm_mday;
54     tmp->tm_mon    = tm->tm_mon;
55     tmp->tm_year   = tm->tm_year;
56     tmp->tm_wday   = tm->tm_wday;
57     tmp->tm_yday   = tm->tm_yday;
58     tmp->tm_isdst  = tm->tm_isdst;
59     /*
60       If you don't have tm_zone in (struct tm), but
61       you get at it via the shared tmzone[], you'll
62       lose. Same goes for the tm_gmtoff field.
63     
64     */
65 #if HAVE_TM_ZONE
66     strcpy(tmp->tm_zone,tm->tm_zone);
67     tmp->tm_gmtoff = tm->tm_gmtoff;
68 #endif
69
70     return (StgAddr)res;
71 }
72 \end{code}