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