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