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