]> code.citadel.org Git - citadel.git/blob - citadel/server/genstamp.c
minor tuning to previous commit
[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
4
5 // Function to generate RFC822-compliant textual time/date stamp
6
7 #include "sysdep.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <string.h>
12 #include <time.h>
13 #include "genstamp.h"
14
15 static char *months[] = {
16         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
17         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
18 };
19
20 static char *weekdays[] = {
21         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
22 };
23
24 // Supplied with a unix timestamp, generate an RFC822-compliant textual
25 // time and date stamp.
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 }