strftime.c revision 1.9.2.2 1 1.9.2.2 erh /* $NetBSD: strftime.c,v 1.9.2.2 1999/11/05 19:21:13 erh Exp $ */
2 1.9.2.2 erh
3 1.9.2.2 erh /*
4 1.9.2.2 erh * Copyright (c) 1989 The Regents of the University of California.
5 1.9.2.2 erh * All rights reserved.
6 1.9.2.2 erh *
7 1.9.2.2 erh * Redistribution and use in source and binary forms, with or without
8 1.9.2.2 erh * modification, are permitted provided that the following conditions
9 1.9.2.2 erh * are met:
10 1.9.2.2 erh * 1. Redistributions of source code must retain the above copyright
11 1.9.2.2 erh * notice, this list of conditions and the following disclaimer.
12 1.9.2.2 erh * 2. Redistributions in binary form must reproduce the above copyright
13 1.9.2.2 erh * notice, this list of conditions and the following disclaimer in the
14 1.9.2.2 erh * documentation and/or other materials provided with the distribution.
15 1.9.2.2 erh * 3. All advertising materials mentioning features or use of this software
16 1.9.2.2 erh * must display the following acknowledgement:
17 1.9.2.2 erh * This product includes software developed by the University of
18 1.9.2.2 erh * California, Berkeley and its contributors.
19 1.9.2.2 erh * 4. Neither the name of the University nor the names of its contributors
20 1.9.2.2 erh * may be used to endorse or promote products derived from this software
21 1.9.2.2 erh * without specific prior written permission.
22 1.9.2.2 erh *
23 1.9.2.2 erh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.9.2.2 erh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.9.2.2 erh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.9.2.2 erh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.9.2.2 erh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.9.2.2 erh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.9.2.2 erh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.9.2.2 erh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.9.2.2 erh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.9.2.2 erh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.9.2.2 erh * SUCH DAMAGE.
34 1.9.2.2 erh */
35 1.9.2.2 erh
36 1.9.2.2 erh #include <sys/cdefs.h>
37 1.9.2.2 erh #if defined(LIBC_SCCS) && !defined(lint)
38 1.9.2.2 erh #if 0
39 1.9.2.2 erh static char *sccsid = "@(#)strftime.c 5.11 (Berkeley) 2/24/91";
40 1.9.2.2 erh #else
41 1.9.2.2 erh __RCSID("$NetBSD: strftime.c,v 1.9.2.2 1999/11/05 19:21:13 erh Exp $");
42 1.9.2.2 erh #endif
43 1.9.2.2 erh #endif /* LIBC_SCCS and not lint */
44 1.9.2.2 erh
45 1.9.2.2 erh #include "namespace.h"
46 1.9.2.2 erh #include <sys/localedef.h>
47 1.9.2.2 erh #include <locale.h>
48 1.9.2.2 erh #include <string.h>
49 1.9.2.2 erh #include <tzfile.h>
50 1.9.2.2 erh #include <time.h>
51 1.9.2.2 erh
52 1.9.2.2 erh static int _add __P((const char *, char **, const char *));
53 1.9.2.2 erh static int _conv __P((int, int, int, char **, const char *));
54 1.9.2.2 erh static int _secs __P((const struct tm *, char **, const char *));
55 1.9.2.2 erh static size_t _fmt __P((const char *, const struct tm *, char **,
56 1.9.2.2 erh const char *));
57 1.9.2.2 erh
58 1.9.2.2 erh size_t
59 1.9.2.2 erh strftime(s, maxsize, format, t)
60 1.9.2.2 erh char *s;
61 1.9.2.2 erh size_t maxsize;
62 1.9.2.2 erh const char *format;
63 1.9.2.2 erh const struct tm *t;
64 1.9.2.2 erh {
65 1.9.2.2 erh char *pt;
66 1.9.2.2 erh
67 1.9.2.2 erh tzset();
68 1.9.2.2 erh if (maxsize < 1)
69 1.9.2.2 erh return (0);
70 1.9.2.2 erh
71 1.9.2.2 erh pt = s;
72 1.9.2.2 erh if (_fmt(format, t, &pt, s + maxsize)) {
73 1.9.2.2 erh *pt = '\0';
74 1.9.2.2 erh return (pt - s);
75 1.9.2.2 erh } else
76 1.9.2.2 erh return (0);
77 1.9.2.2 erh }
78 1.9.2.2 erh
79 1.9.2.2 erh #define SUN_WEEK(t) (((t)->tm_yday + 7 - \
80 1.9.2.2 erh ((t)->tm_wday)) / 7)
81 1.9.2.2 erh #define MON_WEEK(t) (((t)->tm_yday + 7 - \
82 1.9.2.2 erh ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
83 1.9.2.2 erh
84 1.9.2.2 erh static size_t
85 1.9.2.2 erh _fmt(format, t, pt, ptlim)
86 1.9.2.2 erh const char *format;
87 1.9.2.2 erh const struct tm *t;
88 1.9.2.2 erh char **pt;
89 1.9.2.2 erh const char * const ptlim;
90 1.9.2.2 erh {
91 1.9.2.2 erh for (; *format; ++format) {
92 1.9.2.2 erh if (*format == '%') {
93 1.9.2.2 erh ++format;
94 1.9.2.2 erh if (*format == 'E') {
95 1.9.2.2 erh /* Alternate Era */
96 1.9.2.2 erh ++format;
97 1.9.2.2 erh } else if (*format == 'O') {
98 1.9.2.2 erh /* Alternate numeric symbols */
99 1.9.2.2 erh ++format;
100 1.9.2.2 erh }
101 1.9.2.2 erh switch (*format) {
102 1.9.2.2 erh case '\0':
103 1.9.2.2 erh --format;
104 1.9.2.2 erh break;
105 1.9.2.2 erh case 'A':
106 1.9.2.2 erh if (t->tm_wday < 0 || t->tm_wday > 6)
107 1.9.2.2 erh return (0);
108 1.9.2.2 erh if (!_add(_CurrentTimeLocale->day[t->tm_wday],
109 1.9.2.2 erh pt, ptlim))
110 1.9.2.2 erh return (0);
111 1.9.2.2 erh continue;
112 1.9.2.2 erh
113 1.9.2.2 erh case 'a':
114 1.9.2.2 erh if (t->tm_wday < 0 || t->tm_wday > 6)
115 1.9.2.2 erh return (0);
116 1.9.2.2 erh if (!_add(_CurrentTimeLocale->abday[t->tm_wday],
117 1.9.2.2 erh pt, ptlim))
118 1.9.2.2 erh return (0);
119 1.9.2.2 erh continue;
120 1.9.2.2 erh case 'B':
121 1.9.2.2 erh if (t->tm_mon < 0 || t->tm_mon > 11)
122 1.9.2.2 erh return (0);
123 1.9.2.2 erh if (!_add(_CurrentTimeLocale->mon[t->tm_mon],
124 1.9.2.2 erh pt, ptlim))
125 1.9.2.2 erh return (0);
126 1.9.2.2 erh continue;
127 1.9.2.2 erh case 'b':
128 1.9.2.2 erh case 'h':
129 1.9.2.2 erh if (t->tm_mon < 0 || t->tm_mon > 11)
130 1.9.2.2 erh return (0);
131 1.9.2.2 erh if (!_add(_CurrentTimeLocale->abmon[t->tm_mon],
132 1.9.2.2 erh pt, ptlim))
133 1.9.2.2 erh return (0);
134 1.9.2.2 erh continue;
135 1.9.2.2 erh case 'C':
136 1.9.2.2 erh if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
137 1.9.2.2 erh 2, '0', pt, ptlim))
138 1.9.2.2 erh return (0);
139 1.9.2.2 erh continue;
140 1.9.2.2 erh case 'c':
141 1.9.2.2 erh if (!_fmt(_CurrentTimeLocale->d_t_fmt, t, pt,
142 1.9.2.2 erh ptlim))
143 1.9.2.2 erh return (0);
144 1.9.2.2 erh continue;
145 1.9.2.2 erh case 'D':
146 1.9.2.2 erh if (!_fmt("%m/%d/%y", t, pt, ptlim))
147 1.9.2.2 erh return (0);
148 1.9.2.2 erh continue;
149 1.9.2.2 erh case 'd':
150 1.9.2.2 erh if (!_conv(t->tm_mday, 2, '0', pt, ptlim))
151 1.9.2.2 erh return (0);
152 1.9.2.2 erh continue;
153 1.9.2.2 erh case 'e':
154 1.9.2.2 erh if (!_conv(t->tm_mday, 2, ' ', pt, ptlim))
155 1.9.2.2 erh return (0);
156 1.9.2.2 erh continue;
157 1.9.2.2 erh case 'H':
158 1.9.2.2 erh if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
159 1.9.2.2 erh return (0);
160 1.9.2.2 erh continue;
161 1.9.2.2 erh case 'I':
162 1.9.2.2 erh if (!_conv(t->tm_hour % 12 ?
163 1.9.2.2 erh t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
164 1.9.2.2 erh return (0);
165 1.9.2.2 erh continue;
166 1.9.2.2 erh case 'j':
167 1.9.2.2 erh if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
168 1.9.2.2 erh return (0);
169 1.9.2.2 erh continue;
170 1.9.2.2 erh case 'k':
171 1.9.2.2 erh if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
172 1.9.2.2 erh return (0);
173 1.9.2.2 erh continue;
174 1.9.2.2 erh case 'l':
175 1.9.2.2 erh if (!_conv(t->tm_hour % 12 ?
176 1.9.2.2 erh t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
177 1.9.2.2 erh return (0);
178 1.9.2.2 erh continue;
179 1.9.2.2 erh case 'M':
180 1.9.2.2 erh if (!_conv(t->tm_min, 2, '0', pt, ptlim))
181 1.9.2.2 erh return (0);
182 1.9.2.2 erh continue;
183 1.9.2.2 erh case 'm':
184 1.9.2.2 erh if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
185 1.9.2.2 erh return (0);
186 1.9.2.2 erh continue;
187 1.9.2.2 erh case 'n':
188 1.9.2.2 erh if (!_add("\n", pt, ptlim))
189 1.9.2.2 erh return (0);
190 1.9.2.2 erh continue;
191 1.9.2.2 erh case 'p':
192 1.9.2.2 erh if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
193 1.9.2.2 erh >= 12], pt, ptlim))
194 1.9.2.2 erh return (0);
195 1.9.2.2 erh continue;
196 1.9.2.2 erh case 'R':
197 1.9.2.2 erh if (!_fmt("%H:%M", t, pt, ptlim))
198 1.9.2.2 erh return (0);
199 1.9.2.2 erh continue;
200 1.9.2.2 erh case 'r':
201 1.9.2.2 erh if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
202 1.9.2.2 erh ptlim))
203 1.9.2.2 erh return (0);
204 1.9.2.2 erh continue;
205 1.9.2.2 erh case 'S':
206 1.9.2.2 erh if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
207 1.9.2.2 erh return (0);
208 1.9.2.2 erh continue;
209 1.9.2.2 erh case 's':
210 1.9.2.2 erh if (!_secs(t, pt, ptlim))
211 1.9.2.2 erh return (0);
212 1.9.2.2 erh continue;
213 1.9.2.2 erh case 'T':
214 1.9.2.2 erh if (!_fmt("%H:%M:%S", t, pt, ptlim))
215 1.9.2.2 erh return (0);
216 1.9.2.2 erh continue;
217 1.9.2.2 erh case 't':
218 1.9.2.2 erh if (!_add("\t", pt, ptlim))
219 1.9.2.2 erh return (0);
220 1.9.2.2 erh continue;
221 1.9.2.2 erh case 'U':
222 1.9.2.2 erh if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
223 1.9.2.2 erh return (0);
224 1.9.2.2 erh continue;
225 1.9.2.2 erh case 'u':
226 1.9.2.2 erh if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
227 1.9.2.2 erh pt, ptlim))
228 1.9.2.2 erh return (0);
229 1.9.2.2 erh continue;
230 1.9.2.2 erh case 'V': /* ISO 8601 week number */
231 1.9.2.2 erh case 'G': /* ISO 8601 year (four digits) */
232 1.9.2.2 erh case 'g': /* ISO 8601 year (two digits) */
233 1.9.2.2 erh /*
234 1.9.2.2 erh ** From Arnold Robbins' strftime version 3.0: "the week number of the
235 1.9.2.2 erh ** year (the first Monday as the first day of week 1) as a decimal number
236 1.9.2.2 erh ** (01-53)."
237 1.9.2.2 erh ** (ado, 1993-05-24)
238 1.9.2.2 erh **
239 1.9.2.2 erh ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
240 1.9.2.2 erh ** "Week 01 of a year is per definition the first week which has the
241 1.9.2.2 erh ** Thursday in this year, which is equivalent to the week which contains
242 1.9.2.2 erh ** the fourth day of January. In other words, the first week of a new year
243 1.9.2.2 erh ** is the week which has the majority of its days in the new year. Week 01
244 1.9.2.2 erh ** might also contain days from the previous year and the week before week
245 1.9.2.2 erh ** 01 of a year is the last week (52 or 53) of the previous year even if
246 1.9.2.2 erh ** it contains days from the new year. A week starts with Monday (day 1)
247 1.9.2.2 erh ** and ends with Sunday (day 7). For example, the first week of the year
248 1.9.2.2 erh ** 1997 lasts from 1996-12-30 to 1997-01-05..."
249 1.9.2.2 erh ** (ado, 1996-01-02)
250 1.9.2.2 erh */
251 1.9.2.2 erh {
252 1.9.2.2 erh int year;
253 1.9.2.2 erh int yday;
254 1.9.2.2 erh int wday;
255 1.9.2.2 erh int w;
256 1.9.2.2 erh
257 1.9.2.2 erh year = t->tm_year + TM_YEAR_BASE;
258 1.9.2.2 erh yday = t->tm_yday;
259 1.9.2.2 erh wday = t->tm_wday;
260 1.9.2.2 erh for ( ; ; ) {
261 1.9.2.2 erh int len;
262 1.9.2.2 erh int bot;
263 1.9.2.2 erh int top;
264 1.9.2.2 erh
265 1.9.2.2 erh len = isleap(year) ?
266 1.9.2.2 erh DAYSPERLYEAR :
267 1.9.2.2 erh DAYSPERNYEAR;
268 1.9.2.2 erh /*
269 1.9.2.2 erh ** What yday (-3 ... 3) does
270 1.9.2.2 erh ** the ISO year begin on?
271 1.9.2.2 erh */
272 1.9.2.2 erh bot = ((yday + 11 - wday) %
273 1.9.2.2 erh DAYSPERWEEK) - 3;
274 1.9.2.2 erh /*
275 1.9.2.2 erh ** What yday does the NEXT
276 1.9.2.2 erh ** ISO year begin on?
277 1.9.2.2 erh */
278 1.9.2.2 erh top = bot -
279 1.9.2.2 erh (len % DAYSPERWEEK);
280 1.9.2.2 erh if (top < -3)
281 1.9.2.2 erh top += DAYSPERWEEK;
282 1.9.2.2 erh top += len;
283 1.9.2.2 erh if (yday >= top) {
284 1.9.2.2 erh ++year;
285 1.9.2.2 erh w = 1;
286 1.9.2.2 erh break;
287 1.9.2.2 erh }
288 1.9.2.2 erh if (yday >= bot) {
289 1.9.2.2 erh w = 1 + ((yday - bot) /
290 1.9.2.2 erh DAYSPERWEEK);
291 1.9.2.2 erh break;
292 1.9.2.2 erh }
293 1.9.2.2 erh --year;
294 1.9.2.2 erh yday += isleap(year) ?
295 1.9.2.2 erh DAYSPERLYEAR :
296 1.9.2.2 erh DAYSPERNYEAR;
297 1.9.2.2 erh }
298 1.9.2.2 erh #ifdef XPG4_1994_04_09
299 1.9.2.2 erh if ((w == 52
300 1.9.2.2 erh && t->tm_mon == TM_JANUARY)
301 1.9.2.2 erh || (w == 1
302 1.9.2.2 erh && t->tm_mon == TM_DECEMBER))
303 1.9.2.2 erh w = 53;
304 1.9.2.2 erh #endif /* defined XPG4_1994_04_09 */
305 1.9.2.2 erh if (*format == 'V') {
306 1.9.2.2 erh if (!_conv(w, 2, '0',
307 1.9.2.2 erh pt, ptlim))
308 1.9.2.2 erh return (0);
309 1.9.2.2 erh } else if (*format == 'g') {
310 1.9.2.2 erh if (!_conv(year % 100, 2, '0',
311 1.9.2.2 erh pt, ptlim))
312 1.9.2.2 erh return (0);
313 1.9.2.2 erh } else if (!_conv(year, 4, '0',
314 1.9.2.2 erh pt, ptlim))
315 1.9.2.2 erh return (0);
316 1.9.2.2 erh }
317 1.9.2.2 erh continue;
318 1.9.2.2 erh case 'W':
319 1.9.2.2 erh if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
320 1.9.2.2 erh return (0);
321 1.9.2.2 erh continue;
322 1.9.2.2 erh case 'w':
323 1.9.2.2 erh if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
324 1.9.2.2 erh return (0);
325 1.9.2.2 erh continue;
326 1.9.2.2 erh case 'x':
327 1.9.2.2 erh if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
328 1.9.2.2 erh ptlim))
329 1.9.2.2 erh return (0);
330 1.9.2.2 erh continue;
331 1.9.2.2 erh case 'X':
332 1.9.2.2 erh if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
333 1.9.2.2 erh ptlim))
334 1.9.2.2 erh return (0);
335 1.9.2.2 erh continue;
336 1.9.2.2 erh case 'y':
337 1.9.2.2 erh if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
338 1.9.2.2 erh 2, '0', pt, ptlim))
339 1.9.2.2 erh return (0);
340 1.9.2.2 erh continue;
341 1.9.2.2 erh case 'Y':
342 1.9.2.2 erh if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
343 1.9.2.2 erh pt, ptlim))
344 1.9.2.2 erh return (0);
345 1.9.2.2 erh continue;
346 1.9.2.2 erh case 'Z':
347 1.9.2.2 erh if (tzname[t->tm_isdst ? 1 : 0] &&
348 1.9.2.2 erh !_add(tzname[t->tm_isdst ? 1 : 0], pt,
349 1.9.2.2 erh ptlim))
350 1.9.2.2 erh return (0);
351 1.9.2.2 erh continue;
352 1.9.2.2 erh case '%':
353 1.9.2.2 erh /*
354 1.9.2.2 erh * X311J/88-090 (4.12.3.5): if conversion char is
355 1.9.2.2 erh * undefined, behavior is undefined. Print out the
356 1.9.2.2 erh * character itself as printf(3) does.
357 1.9.2.2 erh */
358 1.9.2.2 erh default:
359 1.9.2.2 erh break;
360 1.9.2.2 erh }
361 1.9.2.2 erh }
362 1.9.2.2 erh if (*pt == ptlim)
363 1.9.2.2 erh return (0);
364 1.9.2.2 erh *(*pt)++ = *format;
365 1.9.2.2 erh }
366 1.9.2.2 erh return (ptlim - *pt);
367 1.9.2.2 erh }
368 1.9.2.2 erh
369 1.9.2.2 erh static int
370 1.9.2.2 erh _secs(t, pt, ptlim)
371 1.9.2.2 erh const struct tm *t;
372 1.9.2.2 erh char **pt;
373 1.9.2.2 erh const char * const ptlim;
374 1.9.2.2 erh {
375 1.9.2.2 erh char buf[15];
376 1.9.2.2 erh time_t s;
377 1.9.2.2 erh char *p;
378 1.9.2.2 erh struct tm tmp;
379 1.9.2.2 erh
380 1.9.2.2 erh buf[sizeof (buf) - 1] = '\0';
381 1.9.2.2 erh /* Make a copy, mktime(3) modifies the tm struct. */
382 1.9.2.2 erh tmp = *t;
383 1.9.2.2 erh s = mktime(&tmp);
384 1.9.2.2 erh for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
385 1.9.2.2 erh *p-- = (char)(s % 10 + '0');
386 1.9.2.2 erh return (_add(++p, pt, ptlim));
387 1.9.2.2 erh }
388 1.9.2.2 erh
389 1.9.2.2 erh static int
390 1.9.2.2 erh _conv(n, digits, pad, pt, ptlim)
391 1.9.2.2 erh int n, digits;
392 1.9.2.2 erh int pad;
393 1.9.2.2 erh char **pt;
394 1.9.2.2 erh const char * const ptlim;
395 1.9.2.2 erh {
396 1.9.2.2 erh char buf[10];
397 1.9.2.2 erh char *p;
398 1.9.2.2 erh
399 1.9.2.2 erh buf[sizeof (buf) - 1] = '\0';
400 1.9.2.2 erh p = buf + sizeof(buf) - 2;
401 1.9.2.2 erh do {
402 1.9.2.2 erh *p-- = n % 10 + '0';
403 1.9.2.2 erh n /= 10;
404 1.9.2.2 erh --digits;
405 1.9.2.2 erh } while (n > 0 && p > buf);
406 1.9.2.2 erh while (p > buf && digits-- > 0)
407 1.9.2.2 erh *p-- = pad;
408 1.9.2.2 erh return (_add(++p, pt, ptlim));
409 1.9.2.2 erh }
410 1.9.2.2 erh
411 1.9.2.2 erh static int
412 1.9.2.2 erh _add(str, pt, ptlim)
413 1.9.2.2 erh const char *str;
414 1.9.2.2 erh char **pt;
415 1.9.2.2 erh const char * const ptlim;
416 1.9.2.2 erh {
417 1.9.2.2 erh
418 1.9.2.2 erh for (;; ++(*pt)) {
419 1.9.2.2 erh if (*pt == ptlim)
420 1.9.2.2 erh return (0);
421 1.9.2.2 erh if ((**pt = *str++) == '\0')
422 1.9.2.2 erh return (1);
423 1.9.2.2 erh }
424 1.9.2.2 erh }
425