43fd6c85e2260f3767dad7eedf7dc6faf5ab8c3a
[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.8 2000/04/06 17:54:01 rrt 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 StgInt
24 showTime(I_ size, StgByteArray d, I_ maxsize, StgByteArray buf)
25 {
26     time_t t;
27     struct tm *tm;
28
29     /*
30      * I allege that with the current (9/99) contents of Time.lhs,
31      * size will always be >= 0.   -- sof
32      */
33     switch(size) {
34         case 0:
35             t = 0;
36             break;
37         case 1:
38             t = (time_t) ((StgInt *)d)[0];
39             break;
40         default:
41             return (-1);
42         }
43     tm = localtime(&t);
44 #ifdef cygwin32_TARGET_OS
45     /* Same as in timezone.c: tzset() isn't called automatically */
46     tzset();
47 #endif
48     if (tm != NULL && strftime(buf, maxsize, "%a %b %d %H:%M:%S %Z %Y", tm) > 0) {
49        return 1;
50     } else {
51        return (-1);
52     }
53 }