1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
5 #define TM_YEAR_BASE 1900
7 static const char *afmt
[] = {
8 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
10 static const char *Afmt
[] = {
11 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
14 static const char *bfmt
[] = {
15 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
18 static const char *Bfmt
[] = {
19 "January", "February", "March", "April", "May", "June", "July",
20 "August", "September", "October", "November", "December",
29 for (;; ++pt
, --gsize
)
39 _conv(int n
, int digits
, char pad
)
44 for (p
= buf
+ sizeof(buf
) - 2; n
> 0 && p
> buf
; n
/= 10, --digits
)
46 while (p
> buf
&& digits
-- > 0)
52 _fmt(const char *format
, const struct tm
*t
)
54 for (; *format
; ++format
)
57 if (*(format
+1) == '#' ) {format
++;}
65 if (t
->tm_wday
< 0 || t
->tm_wday
> 6)
67 if (!_add(Afmt
[t
->tm_wday
]))
71 if (t
->tm_wday
< 0 || t
->tm_wday
> 6)
73 if (!_add(afmt
[t
->tm_wday
]))
77 if (t
->tm_mon
< 0 || t
->tm_mon
> 11)
79 if (!_add(Bfmt
[t
->tm_mon
]))
84 if (t
->tm_mon
< 0 || t
->tm_mon
> 11)
86 if (!_add(bfmt
[t
->tm_mon
]))
90 if (!_fmt("%a %b %e %H:%M:%S %Y", t
))
94 if (!_fmt("%m/%d/%y %H:%M:%S", t
))
98 if (!_conv(t
->tm_mday
, 2, ' '))
102 if (!_fmt("%m/%d/%y", t
))
106 if (!_conv(t
->tm_mday
, 2, '0'))
110 if (!_conv(t
->tm_hour
, 2, '0'))
114 if (!_conv(t
->tm_hour
% 12 ?
115 t
->tm_hour
% 12 : 12, 2, '0'))
119 if (!_conv(t
->tm_yday
+ 1, 3, '0'))
123 if (!_conv(t
->tm_hour
, 2, ' '))
127 if (!_conv(t
->tm_hour
% 12 ?
128 t
->tm_hour
% 12 : 12, 2, ' '))
132 if (!_conv(t
->tm_min
, 2, '0'))
136 if (!_conv(t
->tm_mon
+ 1, 2, '0'))
144 if (!_add(t
->tm_hour
>= 12 ? "PM" : "AM"))
148 if (!_fmt("%H:%M", t
))
152 if (!_fmt("%I:%M:%S %p", t
))
156 if (!_conv(t
->tm_sec
, 2, '0'))
161 if (!_fmt("%H:%M:%S", t
))
169 if (!_conv((t
->tm_yday
+ 7 - t
->tm_wday
) / 7,
174 if (!_conv((t
->tm_yday
+ 7 -
175 (t
->tm_wday
? (t
->tm_wday
- 1) : 6))
180 if (!_conv(t
->tm_wday
, 1, '0'))
184 if (!_fmt("%m/%d/%y", t
))
188 if (!_conv((t
->tm_year
+ TM_YEAR_BASE
)
193 if (!_conv(t
->tm_year
+ TM_YEAR_BASE
, 4, '0'))
198 // FIXME: t->tm_zone does not appear to be in MS headers
199 if (!t
->tm_zone
|| !_add(t
->tm_zone
))
205 * X311J/88-090 (4.12.3.5): if conversion char is
206 * undefined, behavior is undefined. Print out the
207 * character itself as printf(3) does.
221 strftime(char *s
, size_t maxsize
, const char *format
, const struct tm
*t
)
224 if ((gsize
= maxsize
) < 1)
229 return maxsize
- gsize
;
235 wcsftime(wchar_t *s
, size_t maxsize
, const wchar_t *format
, const struct tm
*t
)
244 f
[i
] = (char)*format
;
247 if ((gsize
= maxsize
) < 1)
253 for(i
=0;i
<maxsize
;i
++)
254 s
[i
] = (wchar_t)x
[i
];
257 return maxsize
- gsize
;
259 for(i
=0;i
<maxsize
;i
++)
260 s
[i
] = (wchar_t)x
[i
];