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"
48 static void strfmt(char *str, const char *fmt, ...);
52 * size_t strftime_(char *str,
57 * this functions acts much like a sprintf for time/date output.
58 * given a pointer to an output buffer, a format string and a
59 * time, it copies the time to the output buffer formatted in
60 * accordance with the format string. the parameters are used
63 * str is a pointer to the output buffer, there should
64 * be at least maxs characters available at the address
67 * maxs is the maximum number of characters to be copied
68 * into the output buffer, included the '\0' terminator
70 * fmt is the format string. a percent sign (%) is used
71 * to indicate that the following character is a special
72 * format character. the following are valid format
75 * %A full weekday name (Monday)
76 * %a abbreviated weekday name (Mon)
77 * %B full month name (January)
78 * %b abbreviated month name (Jan)
79 * %c standard date and time representation
80 * %d day-of-month (01-31)
81 * %H hour (24 hour clock) (00-23)
82 * %I hour (12 hour clock) (01-12)
83 * %j day-of-year (001-366)
86 * %p local equivalent of AM or PM
88 * %U week-of-year, first day sunday (00-53)
89 * %W week-of-year, first day monday (00-53)
90 * %V ISO 8601 Week number
91 * %w weekday (0-6, sunday is 0)
92 * %X standard time representation
93 * %x standard date representation
94 * %Y year with century
95 * %y year without century (00-99)
99 * the standard date string is equivalent to:
103 * the standard time string is equivalent to:
107 * the standard date and time string is equivalent to:
109 * %a %b %d %H:%M:%S %Y
111 * strftime_() returns the number of characters placed in the
112 * buffer, not including the terminating \0, or zero if more
113 * than maxs characters were produced.
117 size_t strftime_(char *s, size_t maxs, const char *f, const struct tm *t)
136 r = aday[t->tm_wday];
144 r = amonth[t->tm_mon];
148 r = month[t->tm_mon];
152 strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
153 aday[t->tm_wday], amonth[t->tm_mon],
154 t->tm_mday,t->tm_hour, t->tm_min,
155 t->tm_sec, t->tm_year+1900);
159 strfmt(r,"%2",t->tm_mday);
163 strfmt(r,"%2",t->tm_hour);
167 strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
171 strfmt(r,"%3",t->tm_yday+1);
175 strfmt(r,"%2",t->tm_mon+1);
179 strfmt(r,"%2",t->tm_min);
183 r = (t->tm_hour>11)?"PM":"AM";
187 strfmt(r,"%2",t->tm_sec);
192 if (t->tm_yday%7 > t->tm_wday)
199 if (t->tm_yday%7 > (t->tm_wday+6)%7)
206 /* ISO 8601 Week Of Year:
207 If the week (Monday - Sunday) containing January 1 has four or more
208 days in the new year, then it is week 1; otherwise it is week 53 of
209 the previous year and the next week is week one. */
211 w = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) / 7;
212 d = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) % 7;
214 if (d >= 4) { w++; } else if (w == 0) { w = 53; }
219 strfmt(r,"%1",t->tm_wday);
223 strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
224 amonth[t->tm_mon], t->tm_mday, t->tm_year+1900);
228 strfmt(r, "%2:%2:%2", t->tm_hour,
229 t->tm_min, t->tm_sec);
233 strfmt(r,"%2",t->tm_year%100);
237 strfmt(r,"%4",t->tm_year+1900);
241 r = (t->tm_isdst && tzname_[1][0]) ?
242 tzname_[1] : tzname_[0];
246 buf[0] = '%'; /* reconstruct the format */
250 f--; /* back up if at end of string */
279 typedef void *va_list;
280 #define va_start(vp,v) (vp=((char*)&v)+sizeof(v))
281 #define va_arg(vp,t) (*((t*)(vp))++)
286 static int powers[5] = { 1, 10, 100, 1000, 10000 };
289 * static void strfmt(char *str, char *fmt);
291 * simple sprintf for strftime
293 * each format descriptor is of the form %n
294 * where n goes from zero to four
301 static void strfmt(char *str, const char *fmt, ...)
313 if (ilen == 0) /* zero means string arg */
315 sval = va_arg(vp, char*);
319 else /* always leading zeros */
321 ival = va_arg(vp, int);
324 ival %= powers[ilen--];
325 *str++ = (char)('0' + ival / powers[ilen]);
329 else *str++ = fmt[-1];
337 #include <stdio.h> /* for printf */
338 #include <time.h> /* for strftime */
342 int main(int argc, char *argv[])
350 fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1];
351 len = strftime_(test,sizeof test, fmt, localtime(&now));
352 printf("%d: %s\n", len, test);