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