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