779d458f4ee184034299bbddb21cace1bb578ccf
[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 { // TODO: this gives empty strings on debian.
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  * learn the users timeformat preference.
97  */
98 int get_time_format_cached (void)
99 {
100         long calhourformat;
101         int *time_format_cache;
102         time_format_cache = &(WC->time_format_cache);
103         if (*time_format_cache == WC_TIMEFORMAT_NONE)
104         {
105                 get_pref_long("calhourformat", &calhourformat, 24);
106                 if (calhourformat == 24) 
107                         *time_format_cache = WC_TIMEFORMAT_24;
108                 else
109                         *time_format_cache = WC_TIMEFORMAT_AMPM;
110         }
111         return *time_format_cache;
112 }
113
114 /*
115  * Format TIME ONLY for output 
116  * buf          the output buffer
117  * thetime      time to format into buf
118  */
119 void fmt_time(char *buf, time_t thetime)
120 {
121         struct tm *tm;
122         int hour;
123         int time_format;
124         
125         time_format = get_time_format_cached ();
126         buf[0] = 0;
127         tm = localtime(&thetime);
128         hour = tm->tm_hour;
129         if (hour == 0)
130                 hour = 12;
131         else if (hour > 12)
132                 hour = hour - 12;
133
134         if (time_format == WC_TIMEFORMAT_24) {
135                 sprintf(buf, "%2d:%02d",
136                         tm->tm_hour, tm->tm_min
137                 );
138         }
139         else {
140                 sprintf(buf, "%d:%02d%s",
141                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
142                 );
143         }
144 }
145
146
147
148
149 /*
150  * Break down the timestamp used in HTTP headers
151  * Should read rfc1123 and rfc850 dates OK
152  * FIXME won't read asctime
153  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
154  */
155 time_t httpdate_to_timestamp(StrBuf *buf)
156 {
157         time_t t = 0;
158         struct tm tt;
159         const char *c;
160
161         /** Skip day of week, to number */
162         for (c = ChrPtr(buf); *c != ' '; c++)
163                 ;
164         c++;
165         
166         memset(&tt, 0, sizeof(tt));
167
168         /* Get day of month */
169         tt.tm_mday = atoi(c);
170         for (; *c != ' ' && *c != '-'; c++);
171         c++;
172
173         /* Get month */
174         switch (*c) {
175         case 'A':       /* April, August */
176                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
177                 break;
178         case 'D':       /* December */
179                 tt.tm_mon = 11;
180                 break;
181         case 'F':       /* February */
182                 tt.tm_mon = 1;
183                 break;
184         case 'M':       /* March, May */
185                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
186                 break;
187         case 'J':       /* January, June, July */
188                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
189                 break;
190         case 'N':       /* November */
191                 tt.tm_mon = 10;
192                 break;
193         case 'O':       /* October */
194                 tt.tm_mon = 9;
195                 break;
196         case 'S':       /* September */
197                 tt.tm_mon = 8;
198                 break;
199         default:
200                 return 42;
201                 break;  /* NOTREACHED */
202         }
203         c += 4;
204
205         tt.tm_year = 0;
206         /* Get year */
207         tt.tm_year = atoi(c);
208         for (; *c != ' '; c++);
209         c++;
210         if (tt.tm_year >= 1900)
211                 tt.tm_year -= 1900;
212
213         /* Get hour */
214         tt.tm_hour = atoi(c);
215         for (; *c != ':'; c++);
216         c++;
217
218         /* Get minute */
219         tt.tm_min = atoi(c);
220         for (; *c != ':'; c++);
221         c++;
222
223         /* Get second */
224         tt.tm_sec = atoi(c);
225         for (; *c && *c != ' '; c++);
226
227         /* Got everything; let's go.  The global 'timezone' variable contains the
228          * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
229          * This produces an illegal value for tm_sec, but mktime() will normalize
230          * it for us.  This eliminates the need to temporarily switch the environment
231          * variable TZ to UTC, which is good because it fails to switch back on
232          * some systems.
233          */
234         tzset();
235         tt.tm_sec = tt.tm_sec - (int)timezone;
236         t = mktime(&tt);
237         return t;
238 }