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 static-locals (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"
71 /* Copy until we reach the end of the format string or a format specifier */
74 if (count >= bufsize) {
75 /* Not enough buffer space available */
78 if ((c = *format++) == '\0') {
79 /* End of format string reached */
84 /* Format specifier */
94 argptr = days[tm->tm_wday];
98 argptr = months[tm->tm_mon];
102 sprintf (arg, "%02d/%02d/%02d", tm->tm_mon + 1,
103 tm->tm_mday, tm->tm_year % 100);
108 sprintf (arg, "%04d-%02d-%02d", tm->tm_year + 1900,
109 tm->tm_mon + 1, tm->tm_mday);
113 sprintf (arg, "%02d", tm->tm_hour);
117 sprintf (arg, "%02d", tm->tm_hour % 12);
121 sprintf (arg, "%02d", tm->tm_min);
126 argptr = (tm->tm_hour >= 12)? "pm" : "am";
130 sprintf (arg, "%02d", tm->tm_sec);
134 sprintf (arg, "%02d", (tm->tm_yday + 7 - tm->tm_wday) / 7);
138 sprintf (arg, "%02d",
139 (tm->tm_yday + 7 - (tm->tm_wday? tm->tm_wday - 1 : 6)) / 7);
143 sprintf (arg, "%02d:%02d:%02d", tm->tm_hour,
144 tm->tm_min, tm->tm_sec);
148 sprintf (arg, "%4d", tm->tm_year + 1900);
152 argptr = tm->tm_isdst? _tz.dstname : _tz.tzname;
156 sprintf (arg, "%.3s", days[tm->tm_wday]);
160 sprintf (arg, "%.3s", months[tm->tm_mon]);
164 sprintf (arg, "%.3s %.3s%3d %02d:%02d:%02d %d",
165 days[tm->tm_wday], months[tm->tm_mon],
166 tm->tm_mday, tm->tm_hour, tm->tm_min,
167 tm->tm_sec, tm->tm_year + 1900);
171 sprintf (arg, "%02d", tm->tm_mday);
175 sprintf (arg, "%03d", tm->tm_yday + 1);
179 sprintf (arg, "%02d", tm->tm_mon + 1);
183 argptr = (tm->tm_hour >= 12)? "PM" : "AM";
187 sprintf (arg, "%d", tm->tm_wday);
191 sprintf (arg, "%04d-%02d-%02d", tm->tm_year + 1900,
192 tm->tm_mon + 1, tm->tm_mday);
196 sprintf (arg, "%02d", tm->tm_year % 100);
200 /* Unknown format specifier, convert to empty string */
205 /* Check if we have enough space to copy the argument string */
206 len = strlen (argptr);
208 if (count < bufsize) {
209 memcpy (buf, argptr, len);
215 /* No format character, just copy */