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