* move to config-header similar to citserver.
[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
14 #define FALSE 0 /**< no. */
15 #define TRUE 1 /**< yes. */
16
17 /**
18  * \brief       Wrapper around strftime() or strftime_l()
19  *              depending upon how our build is configured.
20  *
21  * \param       s       String target buffer
22  * \param       max     Maximum size of string target buffer
23  * \param       format  strftime() format
24  * \param       tm      Input date/time
25  */
26 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm)
27 {
28 #ifdef ENABLE_NLS
29         if (wc_locales[WC->selected_language] == NULL) {
30                 return strftime(s, max, format, tm);
31         }
32         else {
33                 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
34         }
35 #else
36         return strftime(s, max, format, tm);
37 #endif
38 }
39
40
41 /**
42  * \brief Format a date/time stamp for output 
43  * \param buf the output buffer
44  * \param thetime time to convert to string 
45  * \param brief do we want compact view?????
46  */
47 void fmt_date(char *buf, time_t thetime, int brief)
48 {
49         struct tm tm;
50         struct tm today_tm;
51         time_t today_timet;
52         int hour;
53         int time_format;
54         
55         time_format = get_time_format_cached ();
56         today_timet = time(NULL);
57         localtime_r(&today_timet, &today_tm);
58
59         localtime_r(&thetime, &tm);
60         hour = tm.tm_hour;
61         if (hour == 0)
62                 hour = 12;
63         else if (hour > 12)
64                 hour = hour - 12;
65
66         buf[0] = 0;
67
68         if (brief) {
69
70                 /** If date == today, show only the time */
71                 if ((tm.tm_year == today_tm.tm_year)
72                   &&(tm.tm_mon == today_tm.tm_mon)
73                   &&(tm.tm_mday == today_tm.tm_mday)) {
74                         if (time_format == WC_TIMEFORMAT_24) 
75                                 wc_strftime(buf, 32, "%k:%M", &tm);
76                         else
77                                 wc_strftime(buf, 32, "%l:%M%p", &tm);
78                 }
79                 /** Otherwise, for messages up to 6 months old, show the
80                  * month and day, and the time */
81                 else if (today_timet - thetime < 15552000) {
82                         if (time_format == WC_TIMEFORMAT_24) 
83                                 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
84                         else
85                                 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
86                 }
87                 /** older than 6 months, show only the date */
88                 else {
89                         wc_strftime(buf, 32, "%b %d %Y", &tm);
90                 }
91         }
92         else {
93                 wc_strftime(buf, 32, "%c", &tm);
94         }
95 }
96
97
98 /**
99  * \brief learn the users timeformat preference.
100  */
101 int get_time_format_cached (void)
102 {
103         char calhourformat[16];
104         int *time_format_cache;
105         time_format_cache = &(WC->time_format_cache);
106         if (*time_format_cache == WC_TIMEFORMAT_NONE)
107         {
108                 get_preference("calhourformat", calhourformat, sizeof calhourformat);
109                 if (!strcasecmp(calhourformat, "24")) 
110                         *time_format_cache = WC_TIMEFORMAT_24;
111                 else
112                         *time_format_cache = WC_TIMEFORMAT_AMPM;
113         }
114         return *time_format_cache;
115 }
116
117 /**
118  * \brief Format TIME ONLY for output 
119  * \param buf the output buffer
120  * \param thetime time to format into buf
121  */
122 void fmt_time(char *buf, time_t thetime)
123 {
124         struct tm *tm;
125         int hour;
126         int time_format;
127         
128         time_format = get_time_format_cached ();
129         buf[0] = 0;
130         tm = localtime(&thetime);
131         hour = tm->tm_hour;
132         if (hour == 0)
133                 hour = 12;
134         else if (hour > 12)
135                 hour = hour - 12;
136
137         if (time_format == WC_TIMEFORMAT_24) {
138                 sprintf(buf, "%2d:%02d",
139                         tm->tm_hour, tm->tm_min
140                 );
141         }
142         else {
143                 sprintf(buf, "%d:%02d%s",
144                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
145                 );
146         }
147 }
148
149
150
151
152 /**
153  * \brief Break down the timestamp used in HTTP headers
154  * Should read rfc1123 and rfc850 dates OK
155  * \todo FIXME won't read asctime
156  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
157  * \param buf time to parse
158  * \return the time found in buf
159  */
160 time_t httpdate_to_timestamp(char *buf)
161 {
162         time_t t = 0;
163         struct tm tt;
164         char *c;
165         char tz[256];
166
167         /** Skip day of week, to number */
168         for (c = buf; *c != ' '; c++)
169                 ;
170         c++;
171
172         /* Get day of month */
173         tt.tm_mday = atoi(c);
174         for (; *c != ' ' && *c != '-'; c++);
175         c++;
176
177         /** Get month */
178         switch (*c) {
179         case 'A':       /** April, August */
180                 tt.tm_mon = (c[1] == 'p') ? 3 : 7;
181                 break;
182         case 'D':       /** December */
183                 tt.tm_mon = 11;
184                 break;
185         case 'F':       /** February */
186                 tt.tm_mon = 1;
187                 break;
188         case 'M':       /** March, May */
189                 tt.tm_mon = (c[2] == 'r') ? 2 : 4;
190                 break;
191         case 'J':       /** January, June, July */
192                 tt.tm_mon = (c[2] == 'n') ? ((c[1] == 'a') ? 0 : 5) : 6;
193                 break;
194         case 'N':       /** November */
195                 tt.tm_mon = 10;
196                 break;
197         case 'O':       /** October */
198                 tt.tm_mon = 9;
199                 break;
200         case 'S':       /** September */
201                 tt.tm_mon = 8;
202                 break;
203         default:
204                 return 42;
205                 break;  /** NOTREACHED */
206         }
207         c += 4;
208
209         tt.tm_year = 0;
210         /** Get year */
211         tt.tm_year = atoi(c);
212         for (; *c != ' '; c++);
213         c++;
214         if (tt.tm_year >= 1900)
215                 tt.tm_year -= 1900;
216
217         /** Get hour */
218         tt.tm_hour = atoi(c);
219         for (; *c != ':'; c++);
220         c++;
221
222         /** Get minute */
223         tt.tm_min = atoi(c);
224         for (; *c != ':'; c++);
225         c++;
226
227         /** Get second */
228         tt.tm_sec = atoi(c);
229         for (; *c && *c != ' '; c++);
230
231         /** Got everything; let's go */
232         /** First, change to UTC */
233         if (getenv("TZ"))
234                 sprintf(tz, "TZ=%s", getenv("TZ"));
235         else
236                 strcpy(tz, "TZ=");
237         putenv("TZ=UTC");
238         tzset();
239         t = mktime(&tt);
240         putenv(tz);
241         tzset();
242         return t;
243 }
244
245
246
247
248 /*@}*/