1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <crtdll/string.h>
3 #include <crtdll/time.h>
4 #include <crtdll/stdlib.h>
5 #include <crtdll/wchar.h>
7 #define TM_YEAR_BASE 1900
9 static const char *afmt[] = {
10 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
12 static const char *Afmt[] = {
13 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
16 static const char *bfmt[] = {
17 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
20 static const char *Bfmt[] = {
21 "January", "February", "March", "April", "May", "June", "July",
22 "August", "September", "October", "November", "December",
31 for (;; ++pt, --gsize)
41 _conv(int n, int digits, char pad)
46 for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
48 while (p > buf && digits-- > 0)
54 _fmt(const char *format, const struct tm *t)
56 for (; *format; ++format)
59 if (*(format+1) == '#' ) {format++;}
67 if (t->tm_wday < 0 || t->tm_wday > 6)
69 if (!_add(Afmt[t->tm_wday]))
73 if (t->tm_wday < 0 || t->tm_wday > 6)
75 if (!_add(afmt[t->tm_wday]))
79 if (t->tm_mon < 0 || t->tm_mon > 11)
81 if (!_add(Bfmt[t->tm_mon]))
86 if (t->tm_mon < 0 || t->tm_mon > 11)
88 if (!_add(bfmt[t->tm_mon]))
92 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
96 if (!_fmt("%m/%d/%y %H:%M:%S", t))
100 if (!_conv(t->tm_mday, 2, ' '))
104 if (!_fmt("%m/%d/%y", t))
108 if (!_conv(t->tm_mday, 2, '0'))
112 if (!_conv(t->tm_hour, 2, '0'))
116 if (!_conv(t->tm_hour % 12 ?
117 t->tm_hour % 12 : 12, 2, '0'))
121 if (!_conv(t->tm_yday + 1, 3, '0'))
125 if (!_conv(t->tm_hour, 2, ' '))
129 if (!_conv(t->tm_hour % 12 ?
130 t->tm_hour % 12 : 12, 2, ' '))
134 if (!_conv(t->tm_min, 2, '0'))
138 if (!_conv(t->tm_mon + 1, 2, '0'))
146 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
150 if (!_fmt("%H:%M", t))
154 if (!_fmt("%I:%M:%S %p", t))
158 if (!_conv(t->tm_sec, 2, '0'))
163 if (!_fmt("%H:%M:%S", t))
171 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
176 if (!_conv((t->tm_yday + 7 -
177 (t->tm_wday ? (t->tm_wday - 1) : 6))
182 if (!_conv(t->tm_wday, 1, '0'))
186 if (!_fmt("%m/%d/%y", t))
190 if (!_conv((t->tm_year + TM_YEAR_BASE)
195 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
199 if (!t->tm_zone || !_add(t->tm_zone))
204 * X311J/88-090 (4.12.3.5): if conversion char is
205 * undefined, behavior is undefined. Print out the
206 * character itself as printf(3) does.
220 strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
223 if ((gsize = maxsize) < 1)
228 return maxsize - gsize;
234 wcsftime(wchar_t *s, size_t maxsize, const wchar_t *format, const struct tm *t)
243 f[i] = (char)*format;
246 if ((gsize = maxsize) < 1)
252 for(i=0;i<maxsize;i++)
253 s[i] = (wchar_t)x[i];
256 return maxsize - gsize;
258 for(i=0;i<maxsize;i++)
259 s[i] = (wchar_t)x[i];