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