Oops, applied that timezone offset in the wrong direction. Fixed.
[citadel.git] / webcit / fmt_date.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8 typedef unsigned char byte;
9
10 #define FALSE 0 /**< no. */
11 #define TRUE 1 /**< yes. */
12
13 /*
14  * Wrapper around strftime() or strftime_l()
15  * depending upon how our build is configured.
16  *
17  * s            String target buffer
18  * max          Maximum size of string target buffer
19  * format       strftime() format
20  * tm           Input date/time
21  */
22 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
23 {
24 #ifdef ENABLE_NLS
25         if (wc_locales[WC->selected_language] == NULL) {
26                 return strftime(s, max, format, tm);
27         }
28         else { // TODO: this gives empty strings on debian.
29                 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
30         }
31 #else
32         return strftime(s, max, format, tm);
33 #endif
34 }
35
36
37 /*
38  * Format a date/time stamp for output 
39  */
40 void webcit_fmt_date(char *buf, time_t thetime, int brief)
41 {
42         struct tm tm;
43         struct tm today_tm;
44         time_t today_timet;
45         int time_format;
46
47         time_format = get_time_format_cached ();
48         today_timet = time(NULL);
49         localtime_r(&today_timet, &today_tm);
50
51         localtime_r(&thetime, &tm);
52
53         if (brief) {
54
55                 /* If date == today, show only the time */
56                 if ((tm.tm_year == today_tm.tm_year)
57                   &&(tm.tm_mon == today_tm.tm_mon)
58                   &&(tm.tm_mday == today_tm.tm_mday)) {
59                         if (time_format == WC_TIMEFORMAT_24) 
60                                 wc_strftime(buf, 32, "%k:%M", &tm);
61                         else
62                                 wc_strftime(buf, 32, "%l:%M%p", &tm);
63                 }
64                 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
65                 else if (today_timet - thetime < 15552000) {
66                         if (time_format == WC_TIMEFORMAT_24) 
67                                 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
68                         else
69                                 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
70                 }
71                 /* older than 6 months, show only the date */
72                 else {
73                         wc_strftime(buf, 32, "%b %d %Y", &tm);
74                 }
75         }
76         else {
77                 if (time_format == WC_TIMEFORMAT_24)
78                         wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
79                 else
80                         wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
81         }
82 }
83
84
85 /*
86  * learn the users timeformat preference.
87  */
88 int get_time_format_cached (void)
89 {
90         char calhourformat[16];
91         int *time_format_cache;
92         time_format_cache = &(WC->time_format_cache);
93         if (*time_format_cache == WC_TIMEFORMAT_NONE)
94         {
95                 get_preference("calhourformat", calhourformat, sizeof calhourformat);
96                 if (!strcasecmp(calhourformat, "24")) 
97                         *time_format_cache = WC_TIMEFORMAT_24;
98                 else
99                         *time_format_cache = WC_TIMEFORMAT_AMPM;
100         }
101         return *time_format_cache;
102 }
103
104 /*
105  * Format TIME ONLY for output 
106  * buf          the output buffer
107  * thetime      time to format into buf
108  */
109 void fmt_time(char *buf, time_t thetime)
110 {
111         struct tm *tm;
112         int hour;
113         int time_format;
114         
115         time_format = get_time_format_cached ();
116         buf[0] = 0;
117         tm = localtime(&thetime);
118         hour = tm->tm_hour;
119         if (hour == 0)
120                 hour = 12;
121         else if (hour > 12)
122                 hour = hour - 12;
123
124         if (time_format == WC_TIMEFORMAT_24) {
125                 sprintf(buf, "%2d:%02d",
126                         tm->tm_hour, tm->tm_min
127                 );
128         }
129         else {
130                 sprintf(buf, "%d:%02d%s",
131                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
132                 );
133         }
134 }
135
136
137
138
139 /*
140  * Break down the timestamp used in HTTP headers
141  * Should read rfc1123 and rfc850 dates OK
142  * FIXME won't read asctime
143  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
144  */
145 time_t httpdate_to_timestamp(char *buf)
146 {
147         time_t t = 0;
148         struct tm tt;
149         char *c;
150
151         /** Skip day of week, to number */
152         for (c = buf; *c != ' '; c++)
153                 ;
154         c++;
155
156         /* Get day of month */
157         tt.tm_mday = atoi(c);
158         for (; *c != ' ' && *c != '-'; c++);
159         c++;
160
161         /* Get month */
162         switch (*c) {
163         case 'A':       /* April, August */
164                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
165                 break;
166         case 'D':       /* December */
167                 tt.tm_mon = 11;
168                 break;
169         case 'F':       /* February */
170                 tt.tm_mon = 1;
171                 break;
172         case 'M':       /* March, May */
173                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
174                 break;
175         case 'J':       /* January, June, July */
176                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
177                 break;
178         case 'N':       /* November */
179                 tt.tm_mon = 10;
180                 break;
181         case 'O':       /* October */
182                 tt.tm_mon = 9;
183                 break;
184         case 'S':       /* September */
185                 tt.tm_mon = 8;
186                 break;
187         default:
188                 return 42;
189                 break;  /* NOTREACHED */
190         }
191         c += 4;
192
193         tt.tm_year = 0;
194         /* Get year */
195         tt.tm_year = atoi(c);
196         for (; *c != ' '; c++);
197         c++;
198         if (tt.tm_year >= 1900)
199                 tt.tm_year -= 1900;
200
201         /* Get hour */
202         tt.tm_hour = atoi(c);
203         for (; *c != ':'; c++);
204         c++;
205
206         /* Get minute */
207         tt.tm_min = atoi(c);
208         for (; *c != ':'; c++);
209         c++;
210
211         /* Get second */
212         tt.tm_sec = atoi(c);
213         for (; *c && *c != ' '; c++);
214
215         /* Got everything; let's go.  The global 'timezone' variable contains the
216          * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
217          * This produces an illegal value for tm_sec, but mktime() will normalize
218          * it for us.  This eliminates the need to temporarily switch the environment
219          * variable TZ to UTC, which is good because it fails to switch back on
220          * some systems.
221          */
222         tzset();
223         tt.tm_sec -= timezone;
224         t = mktime(&tt);
225         return t;
226 }