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