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 Format
)
57 time_format
= get_time_format_cached ();
58 today_timet
= time(NULL
);
59 localtime_r(&today_timet
, &today_tm
);
61 localtime_r(&thetime
, &tm
);
64 * DATEFMT_FULL: full display
65 * DATEFMT_BRIEF: if date == today, show only the time
66 * otherwise, for messages up to 6 months old,
67 * show the month and day, and the time
68 * older than 6 months, show only the date
69 * DATEFMT_RAWDATE: show full date, regardless of age
70 * DATEFMT_LOCALEDATE: show full date as prefered for the locale
75 if ((tm
.tm_year
== today_tm
.tm_year
)
76 &&(tm
.tm_mon
== today_tm
.tm_mon
)
77 &&(tm
.tm_mday
== today_tm
.tm_mday
)) {
78 if (time_format
== WC_TIMEFORMAT_24
)
79 wc_strftime(buf
, 32, "%k:%M", &tm
);
81 wc_strftime(buf
, 32, "%l:%M%p", &tm
);
83 else if (today_timet
- thetime
< 15552000) {
84 if (time_format
== WC_TIMEFORMAT_24
)
85 wc_strftime(buf
, 32, "%b %d %k:%M", &tm
);
87 wc_strftime(buf
, 32, "%b %d %l:%M%p", &tm
);
90 wc_strftime(buf
, 32, "%b %d %Y", &tm
);
94 if (time_format
== WC_TIMEFORMAT_24
)
95 wc_strftime(buf
, 32, "%a %b %d %Y %T %Z", &tm
);
97 wc_strftime(buf
, 32, "%a %b %d %Y %r %Z", &tm
);
100 wc_strftime(buf
, 32, "%a %b %d %Y", &tm
);
102 case DATEFMT_LOCALEDATE
:
103 wc_strftime(buf
, 32, "%x", &tm
);
110 * Try to guess whether the user will prefer 12 hour or 24 hour time based on the locale.
112 long guess_calhourformat(void) {
115 memset(&tm
, 0, sizeof tm
);
116 wc_strftime(buf
, 32, "%X", &tm
);
117 if (buf
[strlen(buf
)-1] == 'M') {
125 * learn the users timeformat preference.
127 int get_time_format_cached (void)
130 int *time_format_cache
;
131 time_format_cache
= &(WC
->time_format_cache
);
132 if (*time_format_cache
== WC_TIMEFORMAT_NONE
)
134 get_pref_long("calhourformat", &calhourformat
, 99);
136 /* If we don't know the user's time format preference yet,
137 * make a guess based on the locale.
139 if (calhourformat
== 99) {
140 calhourformat
= guess_calhourformat();
143 /* Now set the preference */
144 if (calhourformat
== 24)
145 *time_format_cache
= WC_TIMEFORMAT_24
;
147 *time_format_cache
= WC_TIMEFORMAT_AMPM
;
149 return *time_format_cache
;
153 * Format TIME ONLY for output
154 * buf the output buffer
155 * thetime time to format into buf
157 void fmt_time(char *buf
, time_t thetime
)
163 time_format
= get_time_format_cached ();
165 tm
= localtime(&thetime
);
172 if (time_format
== WC_TIMEFORMAT_24
) {
173 sprintf(buf
, "%d:%02d",
174 tm
->tm_hour
, tm
->tm_min
178 sprintf(buf
, "%d:%02d%s",
179 hour
, tm
->tm_min
, ((tm
->tm_hour
> 12) ? "pm" : "am")
188 * Break down the timestamp used in HTTP headers
189 * Should read rfc1123 and rfc850 dates OK
190 * FIXME won't read asctime
191 * Doesn't understand timezone, but we only should be using GMT/UTC anyway
193 time_t httpdate_to_timestamp(StrBuf
*buf
)
199 /** Skip day of week, to number */
200 for (c
= ChrPtr(buf
); *c
!= ' '; c
++)
204 memset(&tt
, 0, sizeof(tt
));
206 /* Get day of month */
207 tt
.tm_mday
= atoi(c
);
208 for (; *c
!= ' ' && *c
!= '-'; c
++);
213 case 'A': /* April, August */
214 tt
.tm_mon
= (c
[1] == 'p') ? 3 : 7;
216 case 'D': /* December */
219 case 'F': /* February */
222 case 'M': /* March, May */
223 tt
.tm_mon
= (c
[2] == 'r') ? 2 : 4;
225 case 'J': /* January, June, July */
226 tt
.tm_mon
= (c
[2] == 'n') ? ((c
[1] == 'a') ? 0 : 5) : 6;
228 case 'N': /* November */
231 case 'O': /* October */
234 case 'S': /* September */
239 break; /* NOTREACHED */
245 tt
.tm_year
= atoi(c
);
246 for (; *c
!= ' '; c
++);
248 if (tt
.tm_year
>= 1900)
252 tt
.tm_hour
= atoi(c
);
253 for (; *c
!= ':'; c
++);
258 for (; *c
!= ':'; c
++);
263 for (; *c
&& *c
!= ' '; c
++);
265 /* Got everything; let's go. The global 'timezone' variable contains the
266 * local timezone's offset from UTC, in seconds, so we apply that to tm_sec.
267 * This produces an illegal value for tm_sec, but mktime() will normalize
268 * it for us. This eliminates the need to temporarily switch the environment
269 * variable TZ to UTC, which is good because it fails to switch back on
273 tt
.tm_sec
= tt
.tm_sec
- (int)timezone
;
279 void LoadTimeformatSettingsCache(StrBuf
*Preference
, long lvalue
)
281 int *time_format_cache
;
283 time_format_cache
= &(WC
->time_format_cache
);
285 *time_format_cache
= WC_TIMEFORMAT_24
;
287 *time_format_cache
= WC_TIMEFORMAT_AMPM
;
296 RegisterPreference("calhourformat", _("Time format"), PRF_INT
, LoadTimeformatSettingsCache
);