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 /* Define your own defaults in config.h if necessary */
21 #if defined(TZNAME_STD) && defined(TZNAME_DST)
22 char *tzname_[2] = { TZNAME_STD, TZNAME_DST };
24 #define tzname_ tzname
27 static char *aday[] = {
28 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
31 static char *day[] = {
32 "Sunday", "Monday", "Tuesday", "Wednesday",
33 "Thursday", "Friday", "Saturday"
36 static char *amonth[] = {
37 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
38 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
41 static char *month[] = {
42 "January", "February", "March", "April", "May", "June",
43 "July", "August", "September", "October", "November", "December"
55 * size_t strftime_(char *str,
60 * this functions acts much like a sprintf for time/date output.
61 * given a pointer to an output buffer, a format string and a
62 * time, it copies the time to the output buffer formatted in
63 * accordance with the format string. the parameters are used
66 * str is a pointer to the output buffer, there should
67 * be at least maxs characters available at the address
70 * maxs is the maximum number of characters to be copied
71 * into the output buffer, included the '\0' terminator
73 * fmt is the format string. a percent sign (%) is used
74 * to indicate that the following character is a special
75 * format character. the following are valid format
78 * %A full weekday name (Monday)
79 * %a abbreviated weekday name (Mon)
80 * %B full month name (January)
81 * %b abbreviated month name (Jan)
82 * %c standard date and time representation
83 * %d day-of-month (01-31)
84 * %H hour (24 hour clock) (00-23)
85 * %I hour (12 hour clock) (01-12)
86 * %j day-of-year (001-366)
89 * %p local equivalent of AM or PM
91 * %U week-of-year, first day sunday (00-53)
92 * %W week-of-year, first day monday (00-53)
93 * %V ISO 8601 Week number
94 * %w weekday (0-6, sunday is 0)
95 * %X standard time representation
96 * %x standard date representation
97 * %Y year with century
98 * %y year without century (00-99)
102 * the standard date string is equivalent to:
106 * the standard time string is equivalent to:
110 * the standard date and time string is equivalent to:
112 * %a %b %d %H:%M:%S %Y
114 * strftime_() returns the number of characters placed in the
115 * buffer, not including the terminating \0, or zero if more
116 * than maxs characters were produced.
131 while ((*f != '\0')) {
140 r = aday[t->tm_wday];
148 r = amonth[t->tm_mon];
152 r = month[t->tm_mon];
156 strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
157 aday[t->tm_wday], amonth[t->tm_mon],
158 t->tm_mday, t->tm_hour, t->tm_min,
159 t->tm_sec, t->tm_year + 1900);
163 strfmt(r, "%2", t->tm_mday);
167 strfmt(r, "%2", t->tm_hour);
171 strfmt(r, "%2", (t->tm_hour % 12) ? t->tm_hour % 12 : 12);
175 strfmt(r, "%3", t->tm_yday + 1);
179 strfmt(r, "%2", t->tm_mon + 1);
183 strfmt(r, "%2", t->tm_min);
187 r = (t->tm_hour > 11) ? "PM" : "AM";
191 strfmt(r, "%2", t->tm_sec);
196 if (t->tm_yday % 7 > t->tm_wday)
203 if (t->tm_yday % 7 > (t->tm_wday + 6) % 7)
210 /* ISO 8601 Week Of Year:
211 If the week (Monday - Sunday) containing January 1 has four or more
212 days in the new year, then it is week 1; otherwise it is week 53 of
213 the previous year and the next week is week one. */
215 w = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) / 7;
216 d = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) % 7;
227 strfmt(r, "%1", t->tm_wday);
231 strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
232 amonth[t->tm_mon], t->tm_mday, t->tm_year + 1900);
236 strfmt(r, "%2:%2:%2", t->tm_hour, t->tm_min, t->tm_sec);
240 strfmt(r, "%2", t->tm_year % 100);
244 strfmt(r, "%4", t->tm_year + 1900);
248 r = (t->tm_isdst && tzname_[1][0]) ? tzname_[1] : tzname_[0];
252 buf[0] = '%'; /* reconstruct the format */
256 f--; /* back up if at end of string */
280 typedef void *va_list;
281 #define va_start(vp,v) (vp=((char*)&v)+sizeof(v))
282 #define va_arg(vp,t) (*((t*)(vp))++)
287 static int powers[5] = { 1, 10, 100, 1000, 10000 };
290 * static void strfmt(char *str, char *fmt);
292 * simple sprintf for strftime
294 * each format descriptor is of the form %n
295 * where n goes from zero to four
315 if (ilen == 0) { /* zero means string arg */
316 sval = va_arg(vp, char *);
320 } else { /* always leading zeros */
322 ival = va_arg(vp, int);
325 ival %= powers[ilen--];
326 *str++ = (char) ('0' + ival / powers[ilen]);
338 #include <stdio.h> /* for printf */
339 #include <time.h> /* for strftime */
353 fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1];
354 len = strftime_(test, sizeof test, fmt, localtime(&now));
355 printf("%d: %s\n", len, test);