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