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