1ca2752cd6f2d4527dee237807137cfed3cb9a2c
[citadel.git] / citadel / genstamp.c
1 /*
2  * $Id$
3  *
4  * Function to generate RFC822-compliant textual time/date stamp
5  *
6  */
7
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
16 static char *months[] = {
17         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
18         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
19 };
20
21 static char *weekdays[] = {
22         "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
23 };
24
25
26 /*
27  * Supplied with a unix timestamp, generate an RFC822-compliant textual
28  * time and date stamp.
29  */
30 void datestring(char *buf, time_t xtime, int which_format) {
31         struct tm *t;
32
33         long offset;
34         char offsign;
35
36         t = localtime(&xtime);
37
38         /* Convert "seconds west of GMT" to "hours/minutes offset" */
39         offset = timezone;
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                         sprintf(buf, "%s, %02d %s %04d %02d:%02d:%02d %c%04ld",
53                                 weekdays[t->tm_wday],
54                                 t->tm_mday,
55                                 months[t->tm_mon],
56                                 t->tm_year + 1900,
57                                 t->tm_hour,
58                                 t->tm_min,
59                                 t->tm_sec,
60                                 offsign, offset
61                                 );
62                 break;
63
64                 case DATESTRING_IMAP:
65                         sprintf(buf, "%02d-%s-%04d %02d:%02d:%02d %c%04ld",
66                                 t->tm_mday,
67                                 months[t->tm_mon],
68                                 t->tm_year + 1900,
69                                 t->tm_hour,
70                                 t->tm_min,
71                                 t->tm_sec,
72                                 offsign, offset
73                                 );
74                 break;
75
76         }
77 }
78