[project @ 1997-03-19 14:16:50 by sof]
[ghc-hetmet.git] / ghc / lib / cbits / toUTCTime.lc
diff --git a/ghc/lib/cbits/toUTCTime.lc b/ghc/lib/cbits/toUTCTime.lc
new file mode 100644 (file)
index 0000000..cb6bd10
--- /dev/null
@@ -0,0 +1,67 @@
+%
+% (c) The GRASP/AQUA Project, Glasgow University, 1995
+%
+\subsection[toUTCTime.lc]{toUTCTime Runtime Support}
+
+\begin{code}
+
+#include "rtsdefs.h"
+#include "timezone.h"
+#include "stgio.h"
+
+StgAddr 
+toUTCTime(I_ size, StgByteArray d, StgByteArray res)
+{
+    time_t t;
+    struct tm *tm,*tmp=(struct tm *)res;
+
+    switch(size) {
+       default:
+           return NULL;
+       case 0:
+           t = 0;
+           break;
+       case -1:
+           t = - (time_t) ((StgInt *)d)[0];
+           if (t > 0) 
+               return NULL;
+           break;
+       case 1:
+           t = (time_t) ((StgInt *)d)[0];
+           if (t < 0) 
+               return NULL;
+           break;
+       }
+    tm = gmtime(&t);
+    
+    if (tm == NULL)
+       return NULL;
+
+    /*
+      gmtime() may return a ptr to statically allocated storage,
+      so to make toUTCTime reentrant, we manually copy
+      the structure into the (struct tm *) passed in.
+    */
+    tmp->tm_sec    = tm->tm_sec;
+    tmp->tm_min    = tm->tm_min;
+    tmp->tm_hour   = tm->tm_hour;
+    tmp->tm_mday   = tm->tm_mday;
+    tmp->tm_mon    = tm->tm_mon;
+    tmp->tm_year   = tm->tm_year;
+    tmp->tm_wday   = tm->tm_wday;
+    tmp->tm_yday   = tm->tm_yday;
+    tmp->tm_isdst  = tm->tm_isdst;
+    /*
+      If you don't have tm_zone in (struct tm), but
+      you get at it via the shared tmzone[], you'll
+      lose. Same goes for the tm_gmtoff field.
+    
+    */
+#if HAVE_TM_ZONE
+    strcpy(tmp->tm_zone,tm->tm_zone);
+    tmp->tm_gmtoff = tm->tm_gmtoff;
+#endif
+
+    return (StgAddr)res;
+}
+\end{code}