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