* add Michael Meskes patch: make time in task due dates optional
[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 Format)
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         /*
64          * DATEFMT_FULL:      full display 
65          * DATEFMT_BRIEF:     if date == today, show only the time
66          *                    otherwise, for messages up to 6 months old, 
67          *                 show the month and day, and the time
68          *                    older than 6 months, show only the date
69          * DATEFMT_RAWDATE:   show full date, regardless of age 
70          */
71
72         switch (Format) {
73                 case DATEFMT_BRIEF:
74                         if ((tm.tm_year == today_tm.tm_year)
75                           &&(tm.tm_mon == today_tm.tm_mon)
76                           &&(tm.tm_mday == today_tm.tm_mday)) {
77                                 if (time_format == WC_TIMEFORMAT_24) 
78                                         wc_strftime(buf, 32, "%k:%M", &tm);
79                                 else
80                                         wc_strftime(buf, 32, "%l:%M%p", &tm);
81                         }
82                         else if (today_timet - thetime < 15552000) {
83                                 if (time_format == WC_TIMEFORMAT_24) 
84                                         wc_strftime(buf, 32, "%b %d %k:%M", &tm);
85                                 else
86                                         wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
87                         }
88                         else {
89                                 wc_strftime(buf, 32, "%b %d %Y", &tm);
90                         }
91                         break;
92                 case DATEFMT_FULL:
93                         if (time_format == WC_TIMEFORMAT_24)
94                                 wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
95                         else
96                                 wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
97                         break;
98                 case DATEFMT_RAWDATE:
99                         wc_strftime(buf, 32, "%a %b %d %Y", &tm);
100                         break;
101         }
102 }
103
104
105 /* 
106  * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
107  */
108 long guess_calhourformat(void) {
109         char buf[32];
110         struct tm tm;
111         memset(&tm, 0, sizeof tm);
112         wc_strftime(buf, 32, "%X", &tm);
113         if (buf[strlen(buf)-1] == 'M') {
114                 return 12;
115         }
116         return 24;
117 }
118
119
120 /*
121  * learn the users timeformat preference.
122  */
123 int get_time_format_cached (void)
124 {
125         long calhourformat;
126         int *time_format_cache;
127         time_format_cache = &(WC->time_format_cache);
128         if (*time_format_cache == WC_TIMEFORMAT_NONE)
129         {
130                 get_pref_long("calhourformat", &calhourformat, 99);
131
132                 /* If we don't know the user's time format preference yet,
133                  * make a guess based on the locale.
134                  */
135                 if (calhourformat == 99) {
136                         calhourformat = guess_calhourformat();
137                 }
138
139                 /* Now set the preference */
140                 if (calhourformat == 24) 
141                         *time_format_cache = WC_TIMEFORMAT_24;
142                 else
143                         *time_format_cache = WC_TIMEFORMAT_AMPM;
144         }
145         return *time_format_cache;
146 }
147
148 /*
149  * Format TIME ONLY for output 
150  * buf          the output buffer
151  * thetime      time to format into buf
152  */
153 void fmt_time(char *buf, time_t thetime)
154 {
155         struct tm *tm;
156         int hour;
157         int time_format;
158         
159         time_format = get_time_format_cached ();
160         buf[0] = 0;
161         tm = localtime(&thetime);
162         hour = tm->tm_hour;
163         if (hour == 0)
164                 hour = 12;
165         else if (hour > 12)
166                 hour = hour - 12;
167
168         if (time_format == WC_TIMEFORMAT_24) {
169                 sprintf(buf, "%d:%02d",
170                         tm->tm_hour, tm->tm_min
171                 );
172         }
173         else {
174                 sprintf(buf, "%d:%02d%s",
175                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
176                 );
177         }
178 }
179
180
181
182
183 /*
184  * Break down the timestamp used in HTTP headers
185  * Should read rfc1123 and rfc850 dates OK
186  * FIXME won't read asctime
187  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
188  */
189 time_t httpdate_to_timestamp(StrBuf *buf)
190 {
191         time_t t = 0;
192         struct tm tt;
193         const char *c;
194
195         /** Skip day of week, to number */
196         for (c = ChrPtr(buf); *c != ' '; c++)
197                 ;
198         c++;
199         
200         memset(&tt, 0, sizeof(tt));
201
202         /* Get day of month */
203         tt.tm_mday = atoi(c);
204         for (; *c != ' ' && *c != '-'; c++);
205         c++;
206
207         /* Get month */
208         switch (*c) {
209         case 'A':       /* April, August */
210                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
211                 break;
212         case 'D':       /* December */
213                 tt.tm_mon = 11;
214                 break;
215         case 'F':       /* February */
216                 tt.tm_mon = 1;
217                 break;
218         case 'M':       /* March, May */
219                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
220                 break;
221         case 'J':       /* January, June, July */
222                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
223                 break;
224         case 'N':       /* November */
225                 tt.tm_mon = 10;
226                 break;
227         case 'O':       /* October */
228                 tt.tm_mon = 9;
229                 break;
230         case 'S':       /* September */
231                 tt.tm_mon = 8;
232                 break;
233         default:
234                 return 42;
235                 break;  /* NOTREACHED */
236         }
237         c += 4;
238
239         tt.tm_year = 0;
240         /* Get year */
241         tt.tm_year = atoi(c);
242         for (; *c != ' '; c++);
243         c++;
244         if (tt.tm_year >= 1900)
245                 tt.tm_year -= 1900;
246
247         /* Get hour */
248         tt.tm_hour = atoi(c);
249         for (; *c != ':'; c++);
250         c++;
251
252         /* Get minute */
253         tt.tm_min = atoi(c);
254         for (; *c != ':'; c++);
255         c++;
256
257         /* Get second */
258         tt.tm_sec = atoi(c);
259         for (; *c && *c != ' '; c++);
260
261         /* Got everything; let's go.  The global 'timezone' variable contains the
262          * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
263          * This produces an illegal value for tm_sec, but mktime() will normalize
264          * it for us.  This eliminates the need to temporarily switch the environment
265          * variable TZ to UTC, which is good because it fails to switch back on
266          * some systems.
267          */
268         tzset();
269         tt.tm_sec = tt.tm_sec - (int)timezone;
270         t = mktime(&tt);
271         return t;
272 }
273
274
275 void LoadTimeformatSettingsCache(StrBuf *Preference, long lvalue)
276 {
277         int *time_format_cache;
278         
279          time_format_cache = &(WC->time_format_cache);
280          if (lvalue == 24) 
281                  *time_format_cache = WC_TIMEFORMAT_24;
282          else
283                  *time_format_cache = WC_TIMEFORMAT_AMPM;
284 }
285
286
287
288 void 
289 InitModule_DATETIME
290 (void)
291 {
292         RegisterPreference("calhourformat", _("Time format"), PRF_INT, LoadTimeformatSettingsCache);
293
294
295 }