xref: /csrg-svn/lib/libc/string/strftime.c (revision 37177)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 03/14/89";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <tzfile.h>
25 
26 static char *afmt[] = {
27 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
28 };
29 static char *Afmt[] = {
30 	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
31 	"Saturday",
32 };
33 static char *bfmt[] = {
34 	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
35 	"Oct", "Nov", "Dec",
36 };
37 static char *Bfmt[] = {
38 	"January", "February", "March", "April", "May", "June", "July",
39 	"August", "September", "October", "November", "December",
40 };
41 
42 static size_t gsize;
43 static char *pt;
44 
45 size_t
46 strftime(s, maxsize, format, t)
47 	char *s;
48 	char *format;
49 	size_t maxsize;
50 	struct tm *t;
51 {
52 	size_t _fmt();
53 
54 	pt = s;
55 	if ((gsize = maxsize) < 1)
56 		return(0);
57 	if (_fmt(format, t)) {
58 		*pt = '\0';
59 		return(maxsize - gsize);
60 	}
61 	return(0);
62 }
63 
64 static size_t
65 _fmt(format, t)
66 	register char *format;
67 	struct tm *t;
68 {
69 	for (; *format; ++format) {
70 		if (*format == '%')
71 			switch(*++format) {
72 			case '\0':
73 				--format;
74 				break;
75 			case 'A':
76 				if (t->tm_wday < 0 || t->tm_wday > 6)
77 					return(0);
78 				if (!_add(Afmt[t->tm_wday]))
79 					return(0);
80 				continue;
81 			case 'a':
82 				if (t->tm_wday < 0 || t->tm_wday > 6)
83 					return(0);
84 				if (!_add(afmt[t->tm_wday]))
85 					return(0);
86 				continue;
87 			case 'B':
88 				if (t->tm_mon < 0 || t->tm_mon > 11)
89 					return(0);
90 				if (!_add(Bfmt[t->tm_mon]))
91 					return(0);
92 				continue;
93 			case 'b':
94 			case 'h':
95 				if (t->tm_mon < 0 || t->tm_mon > 11)
96 					return(0);
97 				if (!_add(bfmt[t->tm_mon]))
98 					return(0);
99 				continue;
100 			case 'c':
101 				if (!_fmt("%a %b %d %X %Z %Y", t))
102 					return(0);
103 				continue;
104 			case 'D':
105 				if (!_fmt("%m/%d/%y", t))
106 					return(0);
107 				continue;
108 			case 'd':
109 				if (!_conv(t->tm_mday, 2))
110 					return(0);
111 				continue;
112 			case 'H':
113 				if (!_conv(t->tm_hour, 2))
114 					return(0);
115 				continue;
116 			case 'I':
117 				if (!_conv(t->tm_hour % 12 ?
118 				    t->tm_hour % 12 : 12, 2))
119 					return(0);
120 				continue;
121 			case 'j':
122 				if (!_conv(t->tm_yday + 1, 3))
123 					return(0);
124 				continue;
125 			case 'M':
126 				if (!_conv(t->tm_min, 2))
127 					return(0);
128 				continue;
129 			case 'm':
130 				if (!_conv(t->tm_mon + 1, 2))
131 					return(0);
132 				continue;
133 			case 'n':
134 				if (!_add("\n"))
135 					return(0);
136 				continue;
137 			case 'p':
138 				if (!_add(t->tm_hour >= 12 ? "PM" : "AM"))
139 					return(0);
140 				continue;
141 			case 'R':
142 				if (!_fmt("%H:%M", t))
143 					return(0);
144 				continue;
145 			case 'r':
146 				if (!_fmt("%I:%M:%S %p", t))
147 					return(0);
148 				continue;
149 			case 'S':
150 				if (!_conv(t->tm_sec, 2))
151 					return(0);
152 				continue;
153 			case 'T':
154 			case 'X':
155 				if (!_fmt("%H:%M:%S", t))
156 					return(0);
157 				continue;
158 			case 't':
159 				if (!_add("\t"))
160 					return(0);
161 				continue;
162 			case 'U':
163 				if (!_conv((t->tm_yday + 7 - t->tm_wday) / 7,
164 				    2))
165 					return(0);
166 				continue;
167 			case 'W':
168 				if (!_conv((t->tm_yday + 7 -
169 				    (t->tm_wday ? (t->tm_wday - 1) : 6))
170 				    / 7, 2))
171 					return(0);
172 				continue;
173 			case 'w':
174 				if (!_conv(t->tm_wday, 1))
175 					return(0);
176 				continue;
177 			case 'x':
178 				if (!_fmt("%a %b %d %Y", t))
179 					return(0);
180 				continue;
181 			case 'y':
182 				if (!_conv((t->tm_year + TM_YEAR_BASE)
183 				    % 100, 2))
184 					return(0);
185 				continue;
186 			case 'Y':
187 				if (!_conv(t->tm_year + TM_YEAR_BASE, 4))
188 					return(0);
189 				continue;
190 			case 'Z':
191 				if (!t->tm_zone || !_add(t->tm_zone))
192 					return(0);
193 				continue;
194 			case '%':
195 			/*
196 			 * X311J/88-090 (4.12.3.5): if conversion char is
197 			 * undefined, behavior is undefined.  Print out the
198 			 * character itself as printf(3) also does.
199 			 */
200 			default:
201 				break;
202 		}
203 		if (!gsize--)
204 			return(0);
205 		*pt++ = *format;
206 	}
207 	return(gsize);
208 }
209 
210 static
211 _conv(n, digits)
212 	int n, digits;
213 {
214 	static char buf[10];
215 	register char *p;
216 
217 	for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
218 		*p-- = n % 10 + '0';
219 	while (p > buf && digits-- > 0)
220 		*p-- = '0';
221 	return(_add(++p));
222 }
223 
224 static
225 _add(str)
226 	register char *str;
227 {
228 	for (;; ++pt, --gsize) {
229 		if (!gsize)
230 			return(0);
231 		if (!(*pt = *str++))
232 			return(1);
233 	}
234 }
235