Began moving date outputs to strftime_l()
[citadel.git] / webcit / fmt_date.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup FormatDates Miscellaneous routines formating dates
6  * \ingroup Calendaring
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "webserver.h"
11
12 typedef unsigned char byte; /**< a byte. */
13 char *wdays[7];
14 char *months[12];
15
16 #define FALSE 0 /**< no. */
17 #define TRUE 1 /**< yes. */
18
19 /**
20  * \brief Format a date/time stamp for output 
21  * \param buf the output buffer
22  * \param thetime time to convert to string 
23  * \param brief do we want compact view?????
24  */
25 void fmt_date(char *buf, time_t thetime, int brief)
26 {
27         struct tm tm;
28         struct tm today_tm;
29         time_t today_timet;
30         int hour;
31         char calhourformat[16];
32         static char *ascmonths[12] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL } ;
33
34         if (ascmonths[0] == NULL) {
35                 ascmonths[0] = _("Jan");
36                 ascmonths[1] = _("Feb");
37                 ascmonths[2] = _("Mar");
38                 ascmonths[3] = _("Apr");
39                 ascmonths[4] = _("May");
40                 ascmonths[5] = _("Jun");
41                 ascmonths[6] = _("Jul");
42                 ascmonths[7] = _("Aug");
43                 ascmonths[8] = _("Sep");
44                 ascmonths[9] = _("Oct");
45                 ascmonths[10] = _("Nov");
46                 ascmonths[11] = _("Dec");
47         };
48
49         get_preference("calhourformat", calhourformat, sizeof calhourformat);
50
51         today_timet = time(NULL);
52         localtime_r(&today_timet, &today_tm);
53
54         localtime_r(&thetime, &tm);
55         hour = tm.tm_hour;
56         if (hour == 0)
57                 hour = 12;
58         else if (hour > 12)
59                 hour = hour - 12;
60
61         buf[0] = 0;
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 (!strcasecmp(calhourformat, "24")) {
70                                 sprintf(buf, "%2d:%02d",
71                                         tm.tm_hour, tm.tm_min
72                                 );
73                         }
74                         else {
75                                 sprintf(buf, "%2d:%02d%s",
76                                         hour, tm.tm_min,
77                                         ((tm.tm_hour >= 12) ? "pm" : "am")
78                                 );
79                         }
80                 }
81
82                 /** Otherwise, for messages up to 6 months old, show the
83                  * month and day, and the time */
84                 else if (today_timet - thetime < 15552000) {
85                         if (!strcasecmp(calhourformat, "24")) {
86                                 sprintf(buf, "%s %d %2d:%02d",
87                                         ascmonths[tm.tm_mon],
88                                         tm.tm_mday,
89                                         tm.tm_hour, tm.tm_min
90                                 );
91                         }
92                         else {
93                                 sprintf(buf, "%s %d %2d:%02d%s",
94                                         ascmonths[tm.tm_mon],
95                                         tm.tm_mday,
96                                         hour, tm.tm_min,
97                                         ((tm.tm_hour >= 12) ? "pm" : "am")
98                                 );
99                         }
100                 }
101
102                 /** older than 6 months, show only the date */
103                 else {
104                         sprintf(buf, "%s %d %d",
105                                 ascmonths[tm.tm_mon],
106                                 tm.tm_mday,
107                                 tm.tm_year + 1900
108                         );
109                 }
110         }
111         else {
112                 strftime_l(buf, 32, "%c", &tm, wc_locales[WC->selected_language]);
113         }
114 }
115
116
117
118 /**
119  * \brief Format TIME ONLY for output 
120  * \param buf the output buffer
121  * \param thetime time to format into buf
122  */
123 void fmt_time(char *buf, time_t thetime)
124 {
125         struct tm *tm;
126         int hour;
127         char calhourformat[16];
128
129         get_preference("calhourformat", calhourformat, sizeof calhourformat);
130
131         buf[0] = 0;
132         tm = localtime(&thetime);
133         hour = tm->tm_hour;
134         if (hour == 0)
135                 hour = 12;
136         else if (hour > 12)
137                 hour = hour - 12;
138
139         if (!strcasecmp(calhourformat, "24")) {
140                 sprintf(buf, "%2d:%02d",
141                         tm->tm_hour, tm->tm_min
142                 );
143         }
144         else {
145                 sprintf(buf, "%d:%02d%s",
146                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
147                 );
148         }
149 }
150
151
152
153
154 /**
155  * \brief Break down the timestamp used in HTTP headers
156  * Should read rfc1123 and rfc850 dates OK
157  * \todo FIXME won't read asctime
158  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
159  * \param buf time to parse
160  * \return the time found in buf
161  */
162 time_t httpdate_to_timestamp(const char *buf)
163 {
164         time_t t = 0;
165         struct tm tt;
166         char *c;
167         char tz[256];
168
169         /** Skip day of week, to number */
170         for (c = buf; *c != ' '; c++)
171                 ;
172         c++;
173
174         /* Get day of month */
175         tt.tm_mday = atoi(c);
176         for (; *c != ' ' && *c != '-'; c++);
177         c++;
178
179         /** Get month */
180         switch (*c) {
181         case 'A':       /** April, August */
182                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
183                 break;
184         case 'D':       /** December */
185                 tt.tm_mon = 11;
186                 break;
187         case 'F':       /** February */
188                 tt.tm_mon = 1;
189                 break;
190         case 'M':       /** March, May */
191                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
192                 break;
193         case 'J':       /** January, June, July */
194                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
195                 break;
196         case 'N':       /** November */
197                 tt.tm_mon = 10;
198                 break;
199         case 'O':       /** October */
200                 tt.tm_mon = 9;
201                 break;
202         case 'S':       /** September */
203                 tt.tm_mon = 8;
204                 break;
205         default:
206                 return 42;
207                 break;  /** NOTREACHED */
208         }
209         c += 4;
210
211         tt.tm_year = 0;
212         /** Get year */
213         tt.tm_year = atoi(c);
214         for (; *c != ' '; c++);
215         c++;
216         if (tt.tm_year >= 1900)
217                 tt.tm_year -= 1900;
218
219         /** Get hour */
220         tt.tm_hour = atoi(c);
221         for (; *c != ':'; c++);
222         c++;
223
224         /** Get minute */
225         tt.tm_min = atoi(c);
226         for (; *c != ':'; c++);
227         c++;
228
229         /** Get second */
230         tt.tm_sec = atoi(c);
231         for (; *c && *c != ' '; c++);
232
233         /** Got everything; let's go */
234         /** First, change to UTC */
235         if (getenv("TZ"))
236                 sprintf(tz, "TZ=%s", getenv("TZ"));
237         else
238                 strcpy(tz, "TZ=");
239         putenv("TZ=UTC");
240         tzset();
241         t = mktime(&tt);
242         putenv(tz);
243         tzset();
244         return t;
245 }
246
247
248
249 /**
250  * /brief Initialize the strings used to display months and weekdays.
251  */
252 void initialize_months_and_days(void) {
253         wdays[0] = _("Sunday");
254         wdays[1] = _("Monday");
255         wdays[2] = _("Tuesday");
256         wdays[3] = _("Wednesday");
257         wdays[4] = _("Thursday");
258         wdays[5] = _("Friday");
259         wdays[6] = _("Saturday");
260
261         months[0] = _("January");
262         months[1] = _("February");
263         months[2] = _("March");
264         months[3] = _("April");
265         months[4] = _("May");
266         months[5] = _("June");
267         months[6] = _("July");
268         months[7] = _("August");
269         months[8] = _("September");
270         months[9] = _("October");
271         months[10] = _("November");
272         months[11] = _("December");
273 }
274
275
276
277
278 /*@}*/