1 /*****************************************************************************/
5 /* Convert broken down time to a string in a user specified format */
9 /* (C) 2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
42 /* Use static local variables for speed */
43 #pragma staticlocals (on);
47 /*****************************************************************************/
49 /*****************************************************************************/
53 size_t __fastcall__ strftime (char* buf, size_t bufsize, const char* format,
56 static const char* days[7] = {
57 "Sunday", "Monday", "Tuesday", "Wednesday",
58 "Thursday", "Friday", "Saturday"
60 static const char* months[12] = {
61 "January", "February", "March", "April", "May", "June",
62 "July", "August", "September", "October", "November", "December"
72 /* Copy until we reach the end of the format string or a format specifier */
75 if (count >= bufsize) {
76 /* Not enough buffer space available */
79 if ((c = *format++) == '\0') {
80 /* End of format string reached */
85 /* Format specifier */
95 argptr = days[tm->tm_wday];
99 argptr = months[tm->tm_mon];
103 sprintf (arg, "%02d/%02d/%02d", tm->tm_mon + 1,
104 tm->tm_mday, tm->tm_year % 100);
109 sprintf (arg, "%04d-%02d-%02d", tm->tm_year + 1900,
110 tm->tm_mon + 1, tm->tm_mday);
114 sprintf (arg, "%02d", tm->tm_hour);
118 sprintf (arg, "%02d", tm->tm_hour % 12);
122 sprintf (arg, "%02d", tm->tm_min);
127 argptr = (tm->tm_hour >= 12)? "pm" : "am";
131 sprintf (arg, "%02d", tm->tm_sec);
135 week = tm->tm_yday / 7;
136 if (tm->tm_mday % 7 > tm->tm_wday) {
139 sprintf (arg, "%02u", week);
143 /* ### This one is buggy */
144 week = tm->tm_yday / 7;
145 if (tm->tm_mday % 7 > tm->tm_wday) {
148 sprintf (arg, "%2u", week);
152 sprintf (arg, "%02d:%02d:%02d", tm->tm_hour,
153 tm->tm_min, tm->tm_sec);
157 sprintf (arg, "%4d", tm->tm_year + 1900);
161 argptr = tm->tm_isdst? _tz.dstname : _tz.tzname;
165 sprintf (arg, "%.3s", days[tm->tm_wday]);
169 sprintf (arg, "%.3s", months[tm->tm_mon]);
173 sprintf (arg, "%.3s %.3s%3d %02d:%02d:%02d %d",
174 days[tm->tm_wday], months[tm->tm_mon],
175 tm->tm_mday, tm->tm_hour, tm->tm_min,
176 tm->tm_sec, tm->tm_year + 1900);
180 sprintf (arg, "%02d", tm->tm_mday);
184 sprintf (arg, "%03d", tm->tm_yday + 1);
188 sprintf (arg, "%02d", tm->tm_mon + 1);
192 argptr = (tm->tm_hour >= 12)? "PM" : "AM";
196 sprintf (arg, "%d", tm->tm_wday);
200 sprintf (arg, "%04d-%02d-%02d", tm->tm_year + 1900,
201 tm->tm_mon + 1, tm->tm_mday);
205 sprintf (arg, "%02d", tm->tm_year % 100);
209 /* Unknown format specifier, convert to empty string */
214 /* Check if we have enough space to copy the argument string */
215 len = strlen (argptr);
217 if (count < bufsize) {
218 memcpy (buf, argptr, len);
224 /* No format character, just copy */