summaryrefslogtreecommitdiff
path: root/src/lib/libc/string/strftime.c
diff options
context:
space:
mode:
authormillert <>1998-01-19 00:07:34 +0000
committermillert <>1998-01-19 00:07:34 +0000
commitc5794ce7414757731fc3cf3dfeb1b09c43e500b6 (patch)
treeee181e7cc91263e5add5ebb310254ec5fe12a680 /src/lib/libc/string/strftime.c
parent5a4b430a51a73c805d5b876a4a19e85a3b0f6879 (diff)
downloadopenbsd-c5794ce7414757731fc3cf3dfeb1b09c43e500b6.tar.gz
openbsd-c5794ce7414757731fc3cf3dfeb1b09c43e500b6.tar.bz2
openbsd-c5794ce7414757731fc3cf3dfeb1b09c43e500b6.zip
use tzcode strftime(3) as it is kept up to date.
Diffstat (limited to 'src/lib/libc/string/strftime.c')
-rw-r--r--src/lib/libc/string/strftime.c327
1 files changed, 0 insertions, 327 deletions
diff --git a/src/lib/libc/string/strftime.c b/src/lib/libc/string/strftime.c
deleted file mode 100644
index a72efb8cde..0000000000
--- a/src/lib/libc/string/strftime.c
+++ /dev/null
@@ -1,327 +0,0 @@
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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static char *rcsid = "$OpenBSD: strftime.c,v 1.8 1997/07/25 20:30:14 mickey Exp $";
36#endif /* LIBC_SCCS and not lint */
37
38#include <sys/localedef.h>
39#include <locale.h>
40#include <string.h>
41#include <tzfile.h>
42#include <time.h>
43
44static size_t gsize;
45static char *pt;
46static int _add __P((const char *));
47static int _secs __P((const struct tm *));
48static int _conv __P((int, int, char));
49static size_t _fmt __P((const char *, const struct tm *));
50
51size_t
52strftime(s, maxsize, format, t)
53 char *s;
54 size_t maxsize;
55 const char *format;
56 const struct tm *t;
57{
58 tzset();
59
60 pt = s;
61 if ((gsize = maxsize) < 1)
62 return(0);
63 if (_fmt(format, t)) {
64 *pt = '\0';
65 return(maxsize - gsize);
66 }
67 return(0);
68}
69
70#define SUN_WEEK(t) (((t)->tm_yday + 7 - \
71 ((t)->tm_wday)) / 7)
72#define MON_WEEK(t) (((t)->tm_yday + 7 - \
73 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
74static size_t
75_fmt(format, t)
76 register const char *format;
77 const struct tm *t;
78{
79 for (; *format; ++format) {
80 if (*format == '%') {
81 ++format;
82 if (*format == 'E') {
83 /* Alternate Era */
84 ++format;
85 } else if (*format == 'O') {
86 /* Alternate numeric symbols */
87 ++format;
88 }
89 switch(*format) {
90 case '\0':
91 --format;
92 break;
93 case 'A':
94 if (t->tm_wday < 0 || t->tm_wday > 6)
95 return(0);
96 if (!_add(_CurrentTimeLocale->day[t->tm_wday]))
97 return(0);
98 continue;
99 case 'a':
100 if (t->tm_wday < 0 || t->tm_wday > 6)
101 return(0);
102 if (!_add(_CurrentTimeLocale->abday[t->tm_wday]))
103 return(0);
104 continue;
105 case 'B':
106 if (t->tm_mon < 0 || t->tm_mon > 11)
107 return(0);
108 if (!_add(_CurrentTimeLocale->mon[t->tm_mon]))
109 return(0);
110 continue;
111 case 'b':
112 case 'h':
113 if (t->tm_mon < 0 || t->tm_mon > 11)
114 return(0);
115 if (!_add(_CurrentTimeLocale->abmon[t->tm_mon]))
116 return(0);
117 continue;
118 case 'C':
119 if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
120 2, '0'))
121 return(0);
122 continue;
123 case 'c':
124 if (!_fmt(_CurrentTimeLocale->d_t_fmt, t))
125 return(0);
126 continue;
127 case 'D':
128 if (!_fmt("%m/%d/%y", t))
129 return(0);
130 continue;
131 case 'd':
132 if (!_conv(t->tm_mday, 2, '0'))
133 return(0);
134 continue;
135 case 'e':
136 if (!_conv(t->tm_mday, 2, ' '))
137 return(0);
138 continue;
139 case 'H':
140 if (!_conv(t->tm_hour, 2, '0'))
141 return(0);
142 continue;
143 case 'I':
144 if (!_conv(t->tm_hour % 12 ?
145 t->tm_hour % 12 : 12, 2, '0'))
146 return(0);
147 continue;
148 case 'j':
149 if (!_conv(t->tm_yday + 1, 3, '0'))
150 return(0);
151 continue;
152 case 'k':
153 if (!_conv(t->tm_hour, 2, ' '))
154 return(0);
155 continue;
156 case 'l':
157 if (!_conv(t->tm_hour % 12 ?
158 t->tm_hour % 12: 12, 2, ' '))
159 return(0);
160 continue;
161 case 'M':
162 if (!_conv(t->tm_min, 2, '0'))
163 return(0);
164 continue;
165 case 'm':
166 if (!_conv(t->tm_mon + 1, 2, '0'))
167 return(0);
168 continue;
169 case 'n':
170 if (!_add("\n"))
171 return(0);
172 continue;
173 case 'p':
174 if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour >= 12]))
175 return(0);
176 continue;
177 case 'R':
178 if (!_fmt("%H:%M", t))
179 return(0);
180 continue;
181 case 'r':
182 if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t))
183 return(0);
184 continue;
185 case 'S':
186 if (!_conv(t->tm_sec, 2, '0'))
187 return(0);
188 continue;
189 case 's':
190 if (!_secs(t))
191 return(0);
192 continue;
193 case 'T':
194 if (!_fmt("%H:%M:%S", t))
195 return(0);
196 continue;
197 case 't':
198 if (!_add("\t"))
199 return(0);
200 continue;
201 case 'U':
202 if (!_conv(SUN_WEEK(t), 2, '0'))
203 return(0);
204 continue;
205 case 'u':
206 if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0'))
207 return(0);
208 continue;
209 case 'V':
210 {
211 /* ISO 8601 Week Of Year:
212 If the week (Monday - Sunday) containing
213 January 1 has four or more days in the new
214 year, then it is week 1; otherwise it is
215 week 53 of the previous year and the next
216 week is week one. */
217
218 int week = MON_WEEK(t);
219
220 int days = (((t)->tm_yday + 7 - \
221 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) % 7);
222
223
224 if (days >= 4) {
225 week++;
226 } else if (week == 0) {
227 week = 53;
228 }
229
230 if (!_conv(week, 2, '0'))
231 return(0);
232 continue;
233 }
234 case 'W':
235 if (!_conv(MON_WEEK(t), 2, '0'))
236 return(0);
237 continue;
238 case 'w':
239 if (!_conv(t->tm_wday, 1, '0'))
240 return(0);
241 continue;
242 case 'x':
243 if (!_fmt(_CurrentTimeLocale->d_fmt, t))
244 return(0);
245 continue;
246 case 'X':
247 if (!_fmt(_CurrentTimeLocale->t_fmt, t))
248 return(0);
249 continue;
250 case 'y':
251 if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
252 2, '0'))
253 return(0);
254 continue;
255 case 'Y':
256 if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0'))
257 return(0);
258 continue;
259 case 'Z':
260 if (tzname[t->tm_isdst ? 1 : 0] &&
261 !_add(tzname[t->tm_isdst ? 1 : 0]))
262 return(0);
263 continue;
264 case '%':
265 /*
266 * X311J/88-090 (4.12.3.5): if conversion char is
267 * undefined, behavior is undefined. Print out the
268 * character itself as printf(3) does.
269 */
270 default:
271 break;
272 }
273 }
274 if (!gsize--)
275 return(0);
276 *pt++ = *format;
277 }
278 return(gsize);
279}
280
281static int
282_secs(t)
283 const struct tm *t;
284{
285 static char buf[15];
286 register time_t s;
287 register char *p;
288 struct tm tmp;
289
290 /* Make a copy, mktime(3) modifies the tm struct. */
291 tmp = *t;
292 s = mktime(&tmp);
293 for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
294 *p-- = s % 10 + '0';
295 return(_add(++p));
296}
297
298static int
299#ifdef __STDC__
300_conv(int n, int digits, char pad)
301#else
302_conv(n, digits, pad)
303 int n, digits;
304 char pad;
305#endif
306{
307 static char buf[10];
308 register char *p;
309
310 for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
311 *p-- = n % 10 + '0';
312 while (p > buf && digits-- > 0)
313 *p-- = pad;
314 return(_add(++p));
315}
316
317static int
318_add(str)
319 register const char *str;
320{
321 for (;; ++pt, --gsize) {
322 if (!gsize)
323 return(0);
324 if (!(*pt = *str++))
325 return(1);
326 }
327}