9 extern locale_t wc_locales
[];
12 typedef unsigned char byte
;
14 #define FALSE 0 /**< no. */
15 #define TRUE 1 /**< yes. */
18 * Wrapper around strftime() or strftime_l()
19 * depending upon how our build is configured.
21 * s String target buffer
22 * max Maximum size of string target buffer
23 * format strftime() format
26 size_t wc_strftime(char *s
, size_t max
, const char *format
, const struct tm
*tm
)
31 if (wc_locales
[WC
->selected_language
] == NULL
) {
32 return strftime(s
, max
, format
, tm
);
35 return strftime_l(s
, max
, format
, tm
, wc_locales
[WC
->selected_language
]);
38 return strftime(s
, max
, format
, tm
);
41 return strftime(s
, max
, format
, tm
);
48 * Format a date/time stamp for output
50 void webcit_fmt_date(char *buf
, time_t thetime
, int brief
)
57 time_format
= get_time_format_cached ();
58 today_timet
= time(NULL
);
59 localtime_r(&today_timet
, &today_tm
);
61 localtime_r(&thetime
, &tm
);
65 /* If date == today, show only the time */
66 if ((tm
.tm_year
== today_tm
.tm_year
)
67 &&(tm
.tm_mon
== today_tm
.tm_mon
)
68 &&(tm
.tm_mday
== today_tm
.tm_mday
)) {
69 if (time_format
== WC_TIMEFORMAT_24
)
70 wc_strftime(buf
, 32, "%k:%M", &tm
);
72 wc_strftime(buf
, 32, "%l:%M%p", &tm
);
74 /* Otherwise, for messages up to 6 months old, show the month and day, and the time */
75 else if (today_timet
- thetime
< 15552000) {
76 if (time_format
== WC_TIMEFORMAT_24
)
77 wc_strftime(buf
, 32, "%b %d %k:%M", &tm
);
79 wc_strftime(buf
, 32, "%b %d %l:%M%p", &tm
);
81 /* older than 6 months, show only the date */
83 wc_strftime(buf
, 32, "%b %d %Y", &tm
);
87 if (time_format
== WC_TIMEFORMAT_24
)
88 wc_strftime(buf
, 32, "%a %b %d %Y %T %Z", &tm
);
90 wc_strftime(buf
, 32, "%a %b %d %Y %r %Z", &tm
);
96 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
98 long guess_calhourformat(void) {
101 memset(&tm
, 0, sizeof tm
);
102 wc_strftime(buf
, 32, "%X", &tm
);
103 if (buf
[strlen(buf
)-1] == 'M') {
111 * learn the users timeformat preference.
113 int get_time_format_cached (void)
116 int *time_format_cache
;
117 time_format_cache
= &(WC
->time_format_cache
);
118 if (*time_format_cache
== WC_TIMEFORMAT_NONE
)
120 get_pref_long("calhourformat", &calhourformat
, 99);
122 /* If we don't know the user's time format preference yet,
123 * make a guess based on the locale.
125 if (calhourformat
== 99) {
126 calhourformat
= guess_calhourformat();
129 /* Now set the preference */
130 if (calhourformat
== 24)
131 *time_format_cache
= WC_TIMEFORMAT_24
;
133 *time_format_cache
= WC_TIMEFORMAT_AMPM
;
135 return *time_format_cache
;
139 * Format TIME ONLY for output
140 * buf the output buffer
141 * thetime time to format into buf
143 void fmt_time(char *buf
, time_t thetime
)
149 time_format
= get_time_format_cached ();
151 tm
= localtime(&thetime
);
158 if (time_format
== WC_TIMEFORMAT_24
) {
159 sprintf(buf
, "%d:%02d",
160 tm
->tm_hour
, tm
->tm_min
164 sprintf(buf
, "%d:%02d%s",
165 hour
, tm
->tm_min
, ((tm
->tm_hour
> 12) ? "pm" : "am")
174 * Break down the timestamp used in HTTP headers
175 * Should read rfc1123 and rfc850 dates OK
176 * FIXME won't read asctime
177 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
179 time_t httpdate_to_timestamp(StrBuf
*buf
)
185 /** Skip day of week, to number */
186 for (c
= ChrPtr(buf
); *c
!= ' '; c
++)
190 memset(&tt
, 0, sizeof(tt
));
192 /* Get day of month */
193 tt
.tm_mday
= atoi(c
);
194 for (; *c
!= ' ' && *c
!= '-'; c
++);
199 case 'A': /* April, August */
200 tt
.tm_mon
= (c
[1] == 'p') ? 3 : 7;
202 case 'D': /* December */
205 case 'F': /* February */
208 case 'M': /* March, May */
209 tt
.tm_mon
= (c
[2] == 'r') ? 2 : 4;
211 case 'J': /* January, June, July */
212 tt
.tm_mon
= (c
[2] == 'n') ? ((c
[1] == 'a') ? 0 : 5) : 6;
214 case 'N': /* November */
217 case 'O': /* October */
220 case 'S': /* September */
225 break; /* NOTREACHED */
231 tt
.tm_year
= atoi(c
);
232 for (; *c
!= ' '; c
++);
234 if (tt
.tm_year
>= 1900)
238 tt
.tm_hour
= atoi(c
);
239 for (; *c
!= ':'; c
++);
244 for (; *c
!= ':'; c
++);
249 for (; *c
&& *c
!= ' '; c
++);
251 /* Got everything; let's go. The global 'timezone' variable contains the
252 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
253 * This produces an illegal value for tm_sec, but mktime() will normalize
254 * it for us. This eliminates the need to temporarily switch the environment
255 * variable TZ to UTC, which is good because it fails to switch back on
259 tt
.tm_sec
= tt
.tm_sec
- (int)timezone
;
265 void LoadTimeformatSettingsCache(StrBuf
*Preference
, long lvalue
)
267 int *time_format_cache
;
269 time_format_cache
= &(WC
->time_format_cache
);
271 *time_format_cache
= WC_TIMEFORMAT_24
;
273 *time_format_cache
= WC_TIMEFORMAT_AMPM
;
282 RegisterPreference("calhourformat", _("Time format"), PRF_INT
, LoadTimeformatSettingsCache
);