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