* Doxygen groups. Sorted the files into groups. so now we have a nice structure
[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                 if (!strcasecmp(calhourformat, "24")) {
113                         sprintf(buf, "%s %d %d %2d:%02d",
114                                 ascmonths[tm.tm_mon],
115                                 tm.tm_mday,
116                                 tm.tm_year + 1900,
117                                 tm.tm_hour, tm.tm_min
118                         );
119                 }
120                 else {
121                         sprintf(buf, "%s %d %d %2d:%02d%s",
122                                 ascmonths[tm.tm_mon],
123                                 tm.tm_mday,
124                                 tm.tm_year + 1900,
125                                 hour, tm.tm_min, ((tm.tm_hour >= 12) ? "pm" : "am")
126                         );
127                 }
128         }
129 }
130
131
132
133 /**
134  * \brief Format TIME ONLY for output 
135  * \param buf the output buffer
136  * \param thetime time to format into buf
137  */
138 void fmt_time(char *buf, time_t thetime)
139 {
140         struct tm *tm;
141         int hour;
142         char calhourformat[16];
143
144         get_preference("calhourformat", calhourformat, sizeof calhourformat);
145
146         buf[0] = 0;
147         tm = localtime(&thetime);
148         hour = tm->tm_hour;
149         if (hour == 0)
150                 hour = 12;
151         else if (hour > 12)
152                 hour = hour - 12;
153
154         if (!strcasecmp(calhourformat, "24")) {
155                 sprintf(buf, "%2d:%02d",
156                         tm->tm_hour, tm->tm_min
157                 );
158         }
159         else {
160                 sprintf(buf, "%d:%02d%s",
161                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
162                 );
163         }
164 }
165
166
167
168
169 /**
170  * \brief Break down the timestamp used in HTTP headers
171  * Should read rfc1123 and rfc850 dates OK
172  * \todo FIXME won't read asctime
173  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
174  * \param buf time to parse
175  * \return the time found in buf
176  */
177 time_t httpdate_to_timestamp(const char *buf)
178 {
179         time_t t = 0;
180         struct tm tt;
181         char *c;
182         char tz[256];
183
184         /** Skip day of week, to number */
185         for (c = buf; *c != ' '; c++)
186                 ;
187         c++;
188
189         /* Get day of month */
190         tt.tm_mday = atoi(c);
191         for (; *c != ' ' && *c != '-'; c++);
192         c++;
193
194         /** Get month */
195         switch (*c) {
196         case 'A':       /** April, August */
197                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
198                 break;
199         case 'D':       /** December */
200                 tt.tm_mon = 11;
201                 break;
202         case 'F':       /** February */
203                 tt.tm_mon = 1;
204                 break;
205         case 'M':       /** March, May */
206                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
207                 break;
208         case 'J':       /** January, June, July */
209                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
210                 break;
211         case 'N':       /** November */
212                 tt.tm_mon = 10;
213                 break;
214         case 'O':       /** October */
215                 tt.tm_mon = 9;
216                 break;
217         case 'S':       /** September */
218                 tt.tm_mon = 8;
219                 break;
220         default:
221                 return 42;
222                 break;  /** NOTREACHED */
223         }
224         c += 4;
225
226         tt.tm_year = 0;
227         /** Get year */
228         tt.tm_year = atoi(c);
229         for (; *c != ' '; c++);
230         c++;
231         if (tt.tm_year >= 1900)
232                 tt.tm_year -= 1900;
233
234         /** Get hour */
235         tt.tm_hour = atoi(c);
236         for (; *c != ':'; c++);
237         c++;
238
239         /** Get minute */
240         tt.tm_min = atoi(c);
241         for (; *c != ':'; c++);
242         c++;
243
244         /** Get second */
245         tt.tm_sec = atoi(c);
246         for (; *c && *c != ' '; c++);
247
248         /** Got everything; let's go */
249         /** First, change to UTC */
250         if (getenv("TZ"))
251                 sprintf(tz, "TZ=%s", getenv("TZ"));
252         else
253                 strcpy(tz, "TZ=");
254         putenv("TZ=UTC");
255         tzset();
256         t = mktime(&tt);
257         putenv(tz);
258         tzset();
259         return t;
260 }
261
262
263
264 /**
265  * /brief Initialize the strings used to display months and weekdays.
266  */
267 void initialize_months_and_days(void) {
268         wdays[0] = _("Sunday");
269         wdays[1] = _("Monday");
270         wdays[2] = _("Tuesday");
271         wdays[3] = _("Wednesday");
272         wdays[4] = _("Thursday");
273         wdays[5] = _("Friday");
274         wdays[6] = _("Saturday");
275
276         months[0] = _("January");
277         months[1] = _("February");
278         months[2] = _("March");
279         months[3] = _("April");
280         months[4] = _("May");
281         months[5] = _("June");
282         months[6] = _("July");
283         months[7] = _("August");
284         months[8] = _("September");
285         months[9] = _("October");
286         months[10] = _("November");
287         months[11] = _("December");
288 }
289
290
291
292
293 /*@}*/