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