[project @ 1998-12-02 13:17:09 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.3 1998/12/02 13:28:04 simonm Exp $
5  *
6  * toUTCTime Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "timezone.h"
11 #include "stgio.h"
12
13 StgAddr 
14 toUTCTime(I_ size, StgByteArray d, StgByteArray res)
15 {
16     time_t t;
17     struct tm *tm,*tmp=(struct tm *)res;
18
19     switch(size) {
20         default:
21             return NULL;
22         case 0:
23             t = 0;
24             break;
25         case -1:
26             t = - (time_t) ((StgInt *)d)[0];
27             if (t > 0) 
28                 return NULL;
29             break;
30         case 1:
31             t = (time_t) ((StgInt *)d)[0];
32             if (t < 0) 
33                 return NULL;
34             break;
35         }
36     tm = gmtime(&t);
37     
38     if (tm == NULL)
39         return NULL;
40
41     /*
42       gmtime() may return a ptr to statically allocated storage,
43       so to make toUTCTime reentrant, we manually copy
44       the structure into the (struct tm *) passed in.
45     */
46     tmp->tm_sec    = tm->tm_sec;
47     tmp->tm_min    = tm->tm_min;
48     tmp->tm_hour   = tm->tm_hour;
49     tmp->tm_mday   = tm->tm_mday;
50     tmp->tm_mon    = tm->tm_mon;
51     tmp->tm_year   = tm->tm_year;
52     tmp->tm_wday   = tm->tm_wday;
53     tmp->tm_yday   = tm->tm_yday;
54     tmp->tm_isdst  = tm->tm_isdst;
55     /*
56       If you don't have tm_zone in (struct tm), but
57       you get at it via the shared tmzone[], you'll
58       lose. Same goes for the tm_gmtoff field.
59     
60     */
61 #if HAVE_TM_ZONE
62     strcpy(tmp->tm_zone,tm->tm_zone);
63     tmp->tm_gmtoff = tm->tm_gmtoff;
64 #endif
65
66     return (StgAddr)res;
67 }
68
69 StgInt
70 prim_toUTCTime(StgInt64 d, StgByteArray res)
71 {
72     time_t t;
73     struct tm *tm,*tmp=(struct tm *)res;
74
75     t = (time_t) d;
76     if (t < 0) 
77       return 0;
78
79     tm = gmtime(&t);
80     
81     if (tm == NULL)
82         return 0;
83
84     /*
85       gmtime() may return a ptr to statically allocated storage,
86       so to make toUTCTime reentrant, we manually copy
87       the structure into the (struct tm *) passed in.
88     */
89     tmp->tm_sec    = tm->tm_sec;
90     tmp->tm_min    = tm->tm_min;
91     tmp->tm_hour   = tm->tm_hour;
92     tmp->tm_mday   = tm->tm_mday;
93     tmp->tm_mon    = tm->tm_mon;
94     tmp->tm_year   = tm->tm_year;
95     tmp->tm_wday   = tm->tm_wday;
96     tmp->tm_yday   = tm->tm_yday;
97     tmp->tm_isdst  = tm->tm_isdst;
98     /*
99       If you don't have tm_zone in (struct tm), but
100       you get at it via the shared tmzone[], you'll
101       lose. Same goes for the tm_gmtoff field.
102     
103     */
104 #if HAVE_TM_ZONE
105     strcpy(tmp->tm_zone,tm->tm_zone);
106     tmp->tm_gmtoff = tm->tm_gmtoff;
107 #endif
108
109     return 1;
110 }
111