2 * Copyright (c) 1989 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)strftime.c 5.8 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
24 #include <sys/types.h>
31 static char *afmt[] = {
32 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
34 static char *Afmt[] = {
35 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
38 static char *bfmt[] = {
39 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
42 static char *Bfmt[] = {
43 "January", "February", "March", "April", "May", "June", "July",
44 "August", "September", "October", "November", "December",
51 strftime(s, maxsize, format, t)
60 if ((gsize = maxsize) < 1)
62 if (_fmt(format, t)) {
64 return(maxsize - gsize);
71 register char *format;
74 for (; *format; ++format) {
81 if (t->tm_wday < 0 || t->tm_wday > 6)
83 if (!_add(Afmt[t->tm_wday]))
87 if (t->tm_wday < 0 || t->tm_wday > 6)
89 if (!_add(afmt[t->tm_wday]))
93 if (t->tm_mon < 0 || t->tm_mon > 11)
95 if (!_add(Bfmt[t->tm_mon]))
100 if (t->tm_mon < 0 || t->tm_mon > 11)
102 if (!_add(bfmt[t->tm_mon]))
106 if (!_fmt("%a %b %e %H:%M:%S %Y", t))
110 if (!_fmt("%m/%d/%y %H:%M:%S", t))
114 if (!_conv(t->tm_mday, 2, ' '))
118 if (!_fmt("%m/%d/%y", t))
122 if (!_conv(t->tm_mday, 2, '0'))
126 if (!_conv(t->tm_hour, 2, '0'))
130 if (!_conv(t->tm_hour % 12 ?
131 t->tm_hour % 12 : 12, 2, '0'))
135 if (!_conv(t->tm_yday + 1, 3, '0'))
139 if (!_conv(t->tm_hour, 2, ' '))
143 if (!_conv(t->tm_hour % 12 ?
144 t->tm_hour % 12 : 12, 2, ' '))
148 if (!_conv(t->tm_min, 2, '0'))
152 if (!_conv(t->tm_mon + 1, 2, '0'))
160 if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
164 if (!_fmt("%H:%M", t))
168 if (!_fmt("%I:%M:%S %p", t))
172 if (!_conv(t->tm_sec, 2, '0'))
177 if (!_fmt("%H:%M:%S", t))
185 if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
190 if (!_conv((t->tm_yday + 7 -
191 (t->tm_wday ? (t->tm_wday - 1) : 6))
196 if (!_conv(t->tm_wday, 1, '0'))
200 if (!_fmt("%m/%d/%y", t))
204 if (!_conv((t->tm_year + TM_YEAR_BASE)
209 if (!_conv(t->tm_year + TM_YEAR_BASE, 4, '0'))
213 if (!t->tm_zone || !_add(t->tm_zone))
218 * X311J/88-090 (4.12.3.5): if conversion char is
219 * undefined, behavior is undefined. Print out the
220 * character itself as printf(3) does.
233 _conv(n, digits, pad)
240 for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
242 while (p > buf && digits-- > 0)
251 for (;; ++pt, --gsize) {