]> code.citadel.org Git - citadel.git/blob - citadel/server/genstamp.c
1a3dc1e950159be66ef7226db266b5661a1b98ee
[citadel.git] / citadel / server / genstamp.c
1 // This program is open source software.  Use, duplication, or disclosure
2 // is subject to the terms of the GNU General Public License, version 3.
3 // The program is distributed without any warranty, expressed or implied.
4
5
6 /*
7  * Function to generate RFC822-compliant textual time/date stamp
8  */
9
10 #include "sysdep.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <time.h>
16 #include "genstamp.h"
17
18 static char *months[] = {
19         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
20         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
21 };
22
23 static char *weekdays[] = {
24         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
25 };
26
27 /*
28  * Supplied with a unix timestamp, generate an RFC822-compliant textual
29  * time and date stamp.
30  */
31 long datestring(char *buf, size_t n, time_t xtime, int which_format) {
32         struct tm t;
33
34         long offset;
35         char offsign;
36
37         localtime_r(&xtime, &t);
38
39         /* Convert "seconds west of GMT" to "hours/minutes offset" */
40 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
41         offset = t.tm_gmtoff;
42 #else
43         offset = timezone;
44 #endif
45         if (offset > 0) {
46                 offsign = '+';
47         }
48         else {
49                 offset = 0L - offset;
50                 offsign = '-';
51         }
52         offset = ( (offset / 3600) * 100 ) + ( offset % 60 );
53
54         switch(which_format) {
55
56                 case DATESTRING_RFC822:
57                         return snprintf(
58                                 buf, n,
59                                 "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
60                                 weekdays[t.tm_wday],
61                                 t.tm_mday,
62                                 months[t.tm_mon],
63                                 t.tm_year + 1900,
64                                 t.tm_hour,
65                                 t.tm_min,
66                                 t.tm_sec,
67                                 offsign, offset
68                                 );
69                 break;
70
71                 case DATESTRING_IMAP:
72                         return snprintf(
73                                 buf, n,
74                                 "%02d-%s-%04d %02d:%02d:%02d %c%04ld",
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         }
86         return 0;
87 }