Aha! Finally found the culprit. Someone (possibly me) at
[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 #ifdef ENABLE_NLS
25         if (wc_locales[WC->selected_language] == NULL) {
26                 return strftime(s, max, format, tm);
27         }
28         else { // TODO: this gives empty strings on debian.
29                 return strftime_l(s, max, format, tm, wc_locales[WC->selected_language]);
30         }
31 #else
32         return strftime(s, max, format, tm);
33 #endif
34 }
35
36
37 /*
38  * Format a date/time stamp for output 
39  */
40 void webcit_fmt_date(char *buf, time_t thetime, int brief)
41 {
42         struct tm tm;
43         struct tm today_tm;
44         time_t today_timet;
45         int time_format;
46
47         time_format = get_time_format_cached ();
48         today_timet = time(NULL);
49         localtime_r(&today_timet, &today_tm);
50
51         localtime_r(&thetime, &tm);
52
53         if (brief) {
54
55                 /* If date == today, show only the time */
56                 if ((tm.tm_year == today_tm.tm_year)
57                   &&(tm.tm_mon == today_tm.tm_mon)
58                   &&(tm.tm_mday == today_tm.tm_mday)) {
59                         if (time_format == WC_TIMEFORMAT_24) 
60                                 wc_strftime(buf, 32, "%k:%M", &tm);
61                         else
62                                 wc_strftime(buf, 32, "%l:%M%p", &tm);
63                 }
64                 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
65                 else if (today_timet - thetime < 15552000) {
66                         if (time_format == WC_TIMEFORMAT_24) 
67                                 wc_strftime(buf, 32, "%b %d %k:%M", &tm);
68                         else
69                                 wc_strftime(buf, 32, "%b %d %l:%M%p", &tm);
70                 }
71                 /* older than 6 months, show only the date */
72                 else {
73                         wc_strftime(buf, 32, "%b %d %Y", &tm);
74                 }
75         }
76         else {
77                 if (time_format == WC_TIMEFORMAT_24)
78                         wc_strftime(buf, 32, "%a %b %d %Y %T %Z", &tm);
79                 else
80                         wc_strftime(buf, 32, "%a %b %d %Y %r %Z", &tm);
81         }
82 }
83
84
85 /*
86  * learn the users timeformat preference.
87  */
88 int get_time_format_cached (void)
89 {
90         char calhourformat[16];
91         int *time_format_cache;
92         time_format_cache = &(WC->time_format_cache);
93         if (*time_format_cache == WC_TIMEFORMAT_NONE)
94         {
95                 get_preference("calhourformat", calhourformat, sizeof calhourformat);
96                 if (!strcasecmp(calhourformat, "24")) 
97                         *time_format_cache = WC_TIMEFORMAT_24;
98                 else
99                         *time_format_cache = WC_TIMEFORMAT_AMPM;
100         }
101         return *time_format_cache;
102 }
103
104 /*
105  * Format TIME ONLY for output 
106  * buf          the output buffer
107  * thetime      time to format into buf
108  */
109 void fmt_time(char *buf, time_t thetime)
110 {
111         struct tm *tm;
112         int hour;
113         int time_format;
114         
115         time_format = get_time_format_cached ();
116         buf[0] = 0;
117         tm = localtime(&thetime);
118         hour = tm->tm_hour;
119         if (hour == 0)
120                 hour = 12;
121         else if (hour > 12)
122                 hour = hour - 12;
123
124         if (time_format == WC_TIMEFORMAT_24) {
125                 sprintf(buf, "%2d:%02d",
126                         tm->tm_hour, tm->tm_min
127                 );
128         }
129         else {
130                 sprintf(buf, "%d:%02d%s",
131                         hour, tm->tm_min, ((tm->tm_hour > 12) ? "pm" : "am")
132                 );
133         }
134 }
135
136
137
138
139 /*
140  * Break down the timestamp used in HTTP headers
141  * Should read rfc1123 and rfc850 dates OK
142  * FIXME won't read asctime
143  * Doesn't understand timezone, but we only should be using GMT/UTC anyway
144  */
145 time_t httpdate_to_timestamp(char *buf)
146 {
147         time_t t = 0;
148         struct tm tt;
149         char *c;
150         char tz[256];
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 */
217         /* First, change to UTC */
218         if (getenv("TZ"))
219                 snprintf(tz, 256, "TZ=%s", getenv("TZ"));
220         else
221                 strcpy(tz, "TZ=");
222         putenv("TZ=UTC");
223         tzset();
224         t = mktime(&tt);
225         putenv(tz);
226         tzset();
227         return t;
228 }
229
230