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