1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <msvcrt/string.h>
3 #include <msvcrt/time.h>
4 #include <msvcrt/stdlib.h>
5 #include <msvcrt/wchar.h>
8 #define TM_YEAR_BASE 1900
10 static const char* afmt[] = {
11 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
13 static const char* Afmt[] = {
14 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
17 static const char* bfmt[] = {
18 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
21 static const char* Bfmt[] = {
22 "January", "February", "March", "April", "May", "June", "July",
23 "August", "September", "October", "November", "December",
30 static int _add(const char* str)
32 for (;; ++pt, --gsize)
41 static int _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)
53 static size_t _fmt(const char* format, const struct tm* t)
55 for (; *format; ++format)
58 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 ? t->tm_hour % 12 : 12, 2, '0'))
118 if (!_conv(t->tm_yday + 1, 3, '0'))
122 if (!_conv(t->tm_hour, 2, ' '))
126 if (!_conv(t->tm_hour % 12 ? t->tm_hour % 12 : 12, 2, ' '))
130 if (!_conv(t->tm_min, 2, '0'))
134 if (!_conv(t->tm_mon + 1, 2, '0'))
142 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
146 if (!_fmt("%H:%M", t))
150 if (!_fmt("%I:%M:%S %p", t))
154 if (!_conv(t->tm_sec, 2, '0'))
159 if (!_fmt("%H:%M:%S", t))
167 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, 2, '0'))
171 if (!_conv((t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7, 2, '0'))
175 if (!_conv(t->tm_wday, 1, '0'))
179 if (!_fmt("%m/%d/%y", t))
183 if (!_conv((t->tm_year + TM_YEAR_BASE) % 100, 2, '0'))
187 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
191 if (!t->tm_zone || !_add(t->tm_zone))
196 * X311J/88-090 (4.12.3.5): if conversion char is
197 * undefined, behavior is undefined. Print out the
198 * character itself as printf(3) does.
215 strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
218 if ((gsize = maxsize) < 1)
223 return maxsize - gsize;
231 size_t wcsftime(wchar_t* s, size_t maxsize, const wchar_t* format, const struct tm* t)
239 for (i = 0; i < j; i++)
240 f[i] = (char)*format;
243 if ((gsize = maxsize) < 1)
248 for (i = 0; i < maxsize;i ++)
249 s[i] = (wchar_t)x[i];
252 return maxsize - gsize;
254 for (i = 0; i < maxsize; i++)
255 s[i] = (wchar_t)x[i];