update for HEAD-2003091401
[reactos.git] / lib / crtdll / time / strftime.c
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>
6
7
8 #define TM_YEAR_BASE 1900
9
10 static const char* afmt[] = {
11   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
12 };
13 static const char* Afmt[] = {
14   "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
15   "Saturday",
16 };
17 static const char* bfmt[] = {
18   "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
19   "Oct", "Nov", "Dec",
20 };
21 static const char* Bfmt[] = {
22   "January", "February", "March", "April", "May", "June", "July",
23   "August", "September", "October", "November", "December",
24 };
25
26 static size_t gsize;
27 static char* pt;
28
29
30 static int _add(const char* str)
31 {
32   for (;; ++pt, --gsize)
33   {
34     if (!gsize)
35       return 0;
36     if (!(*pt = *str++))
37       return 1;
38   }
39 }
40
41 static int _conv(int n, int digits, char pad)
42 {
43   static char buf[10];
44   char* p;
45
46   for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
47     *p-- = n % 10 + '0';
48   while (p > buf && digits-- > 0)
49     *p-- = pad;
50   return _add(++p);
51 }
52
53 static size_t _fmt(const char* format, const struct tm* t)
54 {
55   for (; *format; ++format)
56   {
57     if (*format == '%') {
58         if (*(format+1) == '#') {format++;}
59
60     switch(*++format) {
61       case '\0':
62         --format;
63         break;
64       case 'A':
65         if (t->tm_wday < 0 || t->tm_wday > 6)
66           return 0;
67         if (!_add(Afmt[t->tm_wday]))
68           return 0;
69         continue;
70       case 'a':
71         if (t->tm_wday < 0 || t->tm_wday > 6)
72           return 0;
73         if (!_add(afmt[t->tm_wday]))
74           return 0;
75         continue;
76       case 'B':
77         if (t->tm_mon < 0 || t->tm_mon > 11)
78           return 0;
79         if (!_add(Bfmt[t->tm_mon]))
80           return 0;
81         continue;
82       case 'b':
83       case 'h':
84         if (t->tm_mon < 0 || t->tm_mon > 11)
85           return 0;
86         if (!_add(bfmt[t->tm_mon]))
87           return 0;
88         continue;
89       case 'C':
90         if (!_fmt("%a %b %e %H:%M:%S %Y", t))
91           return 0;
92         continue;
93       case 'c':
94         if (!_fmt("%m/%d/%y %H:%M:%S", t))
95           return 0;
96         continue;
97       case 'e':
98         if (!_conv(t->tm_mday, 2, ' '))
99           return 0;
100         continue;
101       case 'D':
102         if (!_fmt("%m/%d/%y", t))
103           return 0;
104         continue;
105       case 'd':
106         if (!_conv(t->tm_mday, 2, '0'))
107           return 0;
108         continue;
109       case 'H':
110         if (!_conv(t->tm_hour, 2, '0'))
111           return 0;
112         continue;
113       case 'I':
114         if (!_conv(t->tm_hour % 12 ? t->tm_hour % 12 : 12, 2, '0'))
115           return 0;
116         continue;
117       case 'j':
118         if (!_conv(t->tm_yday + 1, 3, '0'))
119           return 0;
120         continue;
121       case 'k':
122         if (!_conv(t->tm_hour, 2, ' '))
123           return 0;
124         continue;
125       case 'l':
126         if (!_conv(t->tm_hour % 12 ? t->tm_hour % 12 : 12, 2, ' '))
127           return 0;
128         continue;
129       case 'M':
130         if (!_conv(t->tm_min, 2, '0'))
131           return 0;
132         continue;
133       case 'm':
134         if (!_conv(t->tm_mon + 1, 2, '0'))
135           return 0;
136         continue;
137       case 'n':
138         if (!_add("\n"))
139           return 0;
140         continue;
141       case 'p':
142         if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
143           return 0;
144         continue;
145       case 'R':
146         if (!_fmt("%H:%M", t))
147           return 0;
148         continue;
149       case 'r':
150         if (!_fmt("%I:%M:%S %p", t))
151           return 0;
152         continue;
153       case 'S':
154         if (!_conv(t->tm_sec, 2, '0'))
155           return 0;
156         continue;
157       case 'T':
158       case 'X':
159         if (!_fmt("%H:%M:%S", t))
160           return 0;
161         continue;
162       case 't':
163         if (!_add("\t"))
164           return 0;
165         continue;
166       case 'U':
167         if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7, 2, '0'))
168           return 0;
169         continue;
170       case 'W':
171         if (!_conv((t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7, 2, '0'))
172           return 0;
173         continue;
174       case 'w':
175         if (!_conv(t->tm_wday, 1, '0'))
176           return 0;
177         continue;
178       case 'x':
179         if (!_fmt("%m/%d/%y", t))
180           return 0;
181         continue;
182       case 'y':
183         if (!_conv((t->tm_year + TM_YEAR_BASE) % 100, 2, '0'))
184           return 0;
185         continue;
186       case 'Y':
187         if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
188           return 0;
189         continue;
190       case 'Z':
191         if (!t->tm_zone || !_add(t->tm_zone))
192           return 0;
193         continue;
194       case '%':
195         /*
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.
199          */
200       default:
201         break;
202       }
203     }
204     if (!gsize--)
205       return 0;
206     *pt++ = *format;
207   }
208   return gsize;
209 }
210
211 /*
212  * @implemented
213  */
214 size_t
215 strftime(char *s, size_t maxsize, const char *format, const struct tm *t)
216 {
217   pt = s;
218   if ((gsize = maxsize) < 1)
219     return 0;
220   if (_fmt(format, t))
221   {
222     *pt = '\0';
223     return maxsize - gsize;
224   }
225   return 0;
226 }
227
228 /*
229  * @implemented
230  */
231 size_t wcsftime(wchar_t* s, size_t maxsize, const wchar_t* format, const struct tm* t)
232 {
233   char* x;
234   char* f;
235   int i,j;
236   x = malloc(maxsize);
237   j = wcslen(format);
238   f = malloc(j+1);
239   for (i = 0; i < j; i++)
240         f[i] = (char)*format;
241   f[i] = 0;
242   pt = x;
243   if ((gsize = maxsize) < 1)
244     return 0;
245   if (_fmt(f, t)) {
246     *pt = '\0';
247     free(f);
248     for (i = 0; i < maxsize;i ++)
249         s[i] = (wchar_t)x[i];
250     s[i] = 0;
251     free(x);
252     return maxsize - gsize;
253   }
254   for (i = 0; i < maxsize; i++)
255         s[i] = (wchar_t)x[i];
256   s[i] = 0;
257   free(f);
258   free(x);
259   return 0;
260 }