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