[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / showTime.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[showTime.lc]{ClockTime.showsPrec Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #if TIME_WITH_SYS_TIME
12 # include <sys/time.h>
13 # include <time.h>
14 #else
15 # if HAVE_SYS_TIME_H
16 #  include <sys/time.h>
17 # else
18 #  include <time.h>
19 # endif
20 #endif
21
22 StgAddr
23 showTime(I_ size, StgByteArray d, StgByteArray buf)
24 {
25     time_t t;
26     struct tm *tm;
27
28     switch(size) {
29         default:
30             return (StgAddr)strcpy(buf, "ClockTime.show{LibTime}: out of range");
31         case 0:
32             t = 0;
33             break;
34         case -1:
35             t = - (time_t) ((StgInt *)d)[0];
36             if (t > 0) 
37                 return
38  (StgAddr)strcpy(buf, "ClockTime.show{LibTime}: out of range");
39             break;
40         case 1:
41             t = (time_t) ((StgInt *)d)[0];
42             if (t < 0) 
43                return (StgAddr) strcpy(buf, "ClockTime.show{LibTime}: out of range");
44             break;
45         }
46     tm = localtime(&t);
47     if (tm != NULL && strftime(buf, 32 /*Magic number*/, "%a %b %d %T %Z %Y", tm) > 0)
48        return (StgAddr)buf;
49     return (StgAddr)strcpy(buf, "ClockTime.show{LibTime}: internal error");
50 }
51 \end{code}