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