5 * implements the ansi c function strftime()
7 * written 6 september 1989 by jim nutt
8 * released into the public domain by jim nutt
10 * modified 21-Oct-89 by Rob Duff
12 * modified 08-Dec-04 by Tobi Oetiker (added %V)
15 #include <stddef.h> /* for size_t */
16 #include <stdarg.h> /* for va_arg */
17 #include <time.h> /* for struct tm */
20 static char *aday[] = {
21 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
24 static char *day[] = {
25 "Sunday", "Monday", "Tuesday", "Wednesday",
26 "Thursday", "Friday", "Saturday"
29 static char *amonth[] = {
30 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
31 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
34 static char *month[] = {
35 "January", "February", "March", "April", "May", "June",
36 "July", "August", "September", "October", "November", "December"
39 char *tzname_[2] = {"CST", "CDT"}; /* Add your own defaults here */
43 static void strfmt(char *str, const char *fmt, ...);
47 * size_t strftime_(char *str,
52 * this functions acts much like a sprintf for time/date output.
53 * given a pointer to an output buffer, a format string and a
54 * time, it copies the time to the output buffer formatted in
55 * accordance with the format string. the parameters are used
58 * str is a pointer to the output buffer, there should
59 * be at least maxs characters available at the address
62 * maxs is the maximum number of characters to be copied
63 * into the output buffer, included the '\0' terminator
65 * fmt is the format string. a percent sign (%) is used
66 * to indicate that the following character is a special
67 * format character. the following are valid format
70 * %A full weekday name (Monday)
71 * %a abbreviated weekday name (Mon)
72 * %B full month name (January)
73 * %b abbreviated month name (Jan)
74 * %c standard date and time representation
75 * %d day-of-month (01-31)
76 * %H hour (24 hour clock) (00-23)
77 * %I hour (12 hour clock) (01-12)
78 * %j day-of-year (001-366)
81 * %p local equivalent of AM or PM
83 * %U week-of-year, first day sunday (00-53)
84 * %W week-of-year, first day monday (00-53)
85 * %V ISO 8601 Week number
86 * %w weekday (0-6, sunday is 0)
87 * %X standard time representation
88 * %x standard date representation
89 * %Y year with century
90 * %y year without century (00-99)
94 * the standard date string is equivalent to:
98 * the standard time string is equivalent to:
102 * the standard date and time string is equivalent to:
104 * %a %b %d %H:%M:%S %Y
106 * strftime_() returns the number of characters placed in the
107 * buffer, not including the terminating \0, or zero if more
108 * than maxs characters were produced.
112 size_t strftime_(char *s, size_t maxs, const char *f, const struct tm *t)
131 r = aday[t->tm_wday];
139 r = amonth[t->tm_mon];
143 r = month[t->tm_mon];
147 strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
148 aday[t->tm_wday], amonth[t->tm_mon],
149 t->tm_mday,t->tm_hour, t->tm_min,
150 t->tm_sec, t->tm_year+1900);
154 strfmt(r,"%2",t->tm_mday);
158 strfmt(r,"%2",t->tm_hour);
162 strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
166 strfmt(r,"%3",t->tm_yday+1);
170 strfmt(r,"%2",t->tm_mon+1);
174 strfmt(r,"%2",t->tm_min);
178 r = (t->tm_hour>11)?"PM":"AM";
182 strfmt(r,"%2",t->tm_sec);
187 if (t->tm_yday%7 > t->tm_wday)
194 if (t->tm_yday%7 > (t->tm_wday+6)%7)
201 /* ISO 8601 Week Of Year:
202 If the week (Monday - Sunday) containing January 1 has four or more
203 days in the new year, then it is week 1; otherwise it is week 53 of
204 the previous year and the next week is week one. */
206 w = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) / 7;
207 d = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) % 7;
209 if (d >= 4) { w++; } else if (w == 0) { w = 53; }
214 strfmt(r,"%1",t->tm_wday);
218 strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
219 amonth[t->tm_mon], t->tm_mday, t->tm_year+1900);
223 strfmt(r, "%2:%2:%2", t->tm_hour,
224 t->tm_min, t->tm_sec);
228 strfmt(r,"%2",t->tm_year%100);
232 strfmt(r,"%4",t->tm_year+1900);
236 r = (t->tm_isdst && tzname_[1][0]) ?
237 tzname_[1] : tzname_[0];
241 buf[0] = '%'; /* reconstruct the format */
245 f--; /* back up if at end of string */
274 typedef void *va_list;
275 #define va_start(vp,v) (vp=((char*)&v)+sizeof(v))
276 #define va_arg(vp,t) (*((t*)(vp))++)
281 static int pow[5] = { 1, 10, 100, 1000, 10000 };
284 * static void strfmt(char *str, char *fmt);
286 * simple sprintf for strftime
288 * each format descriptor is of the form %n
289 * where n goes from zero to four
296 static void strfmt(char *str, const char *fmt, ...)
308 if (ilen == 0) /* zero means string arg */
310 sval = va_arg(vp, char*);
314 else /* always leading zeros */
316 ival = va_arg(vp, int);
320 *str++ = (char)('0' + ival / pow[ilen]);
324 else *str++ = fmt[-1];
332 #include <stdio.h> /* for printf */
333 #include <time.h> /* for strftime */
337 int main(int argc, char *argv[])
345 fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1];
346 len = strftime_(test,sizeof test, fmt, localtime(&now));
347 printf("%d: %s\n", len, test);