fix all the <time.h> vs. <sys/time.h> issues, hopefully
[citadel.git] / citadel / genstamp.c
1 /*
2  * $Id$
3  *
4  * Function to generate RFC822-compliant textual time/date stamp
5  *
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <string.h>
12
13 #if TIME_WITH_SYS_TIME
14 # include <sys/time.h>
15 # include <time.h>
16 #else
17 # if HAVE_SYS_TIME_H
18 #  include <sys/time.h>
19 # else
20 #  include <time.h>
21 # endif
22 #endif
23
24 #include "genstamp.h"
25
26
27 static char *months[] = {
28         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
29         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
30 };
31
32 static char *weekdays[] = {
33         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
34 };
35
36
37 /*
38  * Supplied with a unix timestamp, generate an RFC822-compliant textual
39  * time and date stamp.
40  */
41 void datestring(char *buf, time_t xtime, int which_format) {
42         struct tm *t;
43
44         long offset;
45         char offsign;
46
47         t = localtime(&xtime);
48
49         /* Convert "seconds west of GMT" to "hours/minutes offset" */
50         offset = timezone;
51         if (offset > 0) {
52                 offsign = '-';
53         }
54         else {
55                 offset = 0L - offset;
56                 offsign = '+';
57         }
58         offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
59
60         switch(which_format) {
61
62                 case DATESTRING_RFC822:
63                         sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
64                                 weekdays[t->tm_wday],
65                                 t->tm_mday,
66                                 months[t->tm_mon],
67                                 t->tm_year + 1900,
68                                 t->tm_hour,
69                                 t->tm_min,
70                                 t->tm_sec,
71                                 offsign, offset
72                                 );
73                 break;
74
75                 case DATESTRING_IMAP:
76                         sprintf(buf, "%02d-%s-%04d %02d:%02d:%02d %c%04ld",
77                                 t->tm_mday,
78                                 months[t->tm_mon],
79                                 t->tm_year + 1900,
80                                 t->tm_hour,
81                                 t->tm_min,
82                                 t->tm_sec,
83                                 offsign, offset
84                                 );
85                 break;
86
87         }
88 }
89