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