[project @ 1996-01-11 14:06:51 by partain]
[ghc-hetmet.git] / ghc / runtime / io / 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 "stgio.h"
10 #include "timezone.h"
11
12 StgAddr 
13 toUTCTime(size, d, res)
14 StgInt size;
15 StgByteArray d;
16 StgByteArray res;
17 {
18     time_t t;
19     struct tm *tm,*tmp=(struct tm *)res;
20
21     switch(size) {
22         default:
23             return NULL;
24         case 0:
25             t = 0;
26             break;
27         case -1:
28             t = - (time_t) ((StgInt *)d)[0];
29             if (t > 0) 
30                 return NULL;
31             break;
32         case 1:
33             t = (time_t) ((StgInt *)d)[0];
34             if (t < 0) 
35                 return NULL;
36             break;
37         }
38     tm = gmtime(&t);
39     
40     if (tm == NULL)
41         return NULL;
42
43     /*
44       gmtime() may return a ptr to statically allocated storage,
45       so to make toUTCTime reentrant, we manually copy
46       the structure into the (struct tm *) passed in.
47     */
48     tmp->tm_sec    = tm->tm_sec;
49     tmp->tm_min    = tm->tm_min;
50     tmp->tm_hour   = tm->tm_hour;
51     tmp->tm_mday   = tm->tm_mday;
52     tmp->tm_mon    = tm->tm_mon;
53     tmp->tm_year   = tm->tm_year;
54     tmp->tm_wday   = tm->tm_wday;
55     tmp->tm_yday   = tm->tm_yday;
56     tmp->tm_isdst  = tm->tm_isdst;
57     /*
58       If you don't have tm_zone in (struct tm), but
59       you get at it via the shared tmzone[], you'll
60       lose. Same goes for the tm_gmtoff field.
61     
62     */
63 #if HAVE_TM_ZONE
64     strcpy(tmp->tm_zone,tm->tm_zone);
65     tmp->tm_gmtoff = tm->tm_gmtoff;
66 #endif
67
68     return (StgAddr)res;
69 }
70
71 \end{code}