1 /* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
2 /* $NetBSD: strftime.c,v 1.8 1999/02/07 17:33:30 augustss Exp $ */
5 * Copyright (c) 1989 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char *sccsid = "@(#)strftime.c 5.11 (Berkeley) 2/24/91";
41 __RCSID("$NetBSD: strftime.c,v 1.8 1999/02/07 17:33:30 augustss Exp $");
43 #endif /* LIBC_SCCS and not lint */
48 static int _add __P((const char *, char **, const char *));
49 static int _conv __P((int, int, int, char **, const char *));
50 static int _secs __P((const struct tm *, char **, const char *));
51 static size_t _fmt __P((const char *, const struct tm *, char **,
55 strftime(s, maxsize, format, t)
68 if (_fmt(format, t, &pt, s + maxsize)) {
75 #define SUN_WEEK(t) (((t)->tm_yday + 7 - \
77 #define MON_WEEK(t) (((t)->tm_yday + 7 - \
78 ((t)->tm_wday ? (t)->tm_wday - 1 : 6)) / 7)
81 _fmt(format, t, pt, ptlim)
85 const char * const ptlim;
87 for (; *format; ++format) {
93 } else if (*format == 'O') {
94 /* Alternate numeric symbols */
102 if (t->tm_wday < 0 || t->tm_wday > 6)
104 if (!_add(_CurrentTimeLocale->day[t->tm_wday],
110 if (t->tm_wday < 0 || t->tm_wday > 6)
112 if (!_add(_CurrentTimeLocale->abday[t->tm_wday],
117 if (t->tm_mon < 0 || t->tm_mon > 11)
119 if (!_add(_CurrentTimeLocale->mon[t->tm_mon],
125 if (t->tm_mon < 0 || t->tm_mon > 11)
127 if (!_add(_CurrentTimeLocale->abmon[t->tm_mon],
132 if (!_conv((t->tm_year + TM_YEAR_BASE) / 100,
137 if (!_fmt(_CurrentTimeLocale->d_t_fmt, t, pt,
142 if (!_fmt("%m/%d/%y", t, pt, ptlim))
146 if (!_conv(t->tm_mday, 2, '0', pt, ptlim))
150 if (!_conv(t->tm_mday, 2, ' ', pt, ptlim))
154 if (!_conv(t->tm_hour, 2, '0', pt, ptlim))
158 if (!_conv(t->tm_hour % 12 ?
159 t->tm_hour % 12 : 12, 2, '0', pt, ptlim))
163 if (!_conv(t->tm_yday + 1, 3, '0', pt, ptlim))
167 if (!_conv(t->tm_hour, 2, ' ', pt, ptlim))
171 if (!_conv(t->tm_hour % 12 ?
172 t->tm_hour % 12: 12, 2, ' ', pt, ptlim))
176 if (!_conv(t->tm_min, 2, '0', pt, ptlim))
180 if (!_conv(t->tm_mon + 1, 2, '0', pt, ptlim))
184 if (!_add("\n", pt, ptlim))
188 if (!_add(_CurrentTimeLocale->am_pm[t->tm_hour
193 if (!_fmt("%H:%M", t, pt, ptlim))
197 if (!_fmt(_CurrentTimeLocale->t_fmt_ampm, t, pt,
202 if (!_conv(t->tm_sec, 2, '0', pt, ptlim))
206 if (!_secs(t, pt, ptlim))
210 if (!_fmt("%H:%M:%S", t, pt, ptlim))
214 if (!_add("\t", pt, ptlim))
218 if (!_conv(SUN_WEEK(t), 2, '0', pt, ptlim))
222 if (!_conv(t->tm_wday ? t->tm_wday : 7, 1, '0',
226 case 'V': /* ISO 8601 week number */
227 case 'G': /* ISO 8601 year (four digits) */
228 case 'g': /* ISO 8601 year (two digits) */
230 ** From Arnold Robbins' strftime version 3.0: "the week number of the
231 ** year (the first Monday as the first day of week 1) as a decimal number
235 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
236 ** "Week 01 of a year is per definition the first week which has the
237 ** Thursday in this year, which is equivalent to the week which contains
238 ** the fourth day of January. In other words, the first week of a new year
239 ** is the week which has the majority of its days in the new year. Week 01
240 ** might also contain days from the previous year and the week before week
241 ** 01 of a year is the last week (52 or 53) of the previous year even if
242 ** it contains days from the new year. A week starts with Monday (day 1)
243 ** and ends with Sunday (day 7). For example, the first week of the year
244 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
253 year = t->tm_year + TM_YEAR_BASE;
265 ** What yday (-3 ... 3) does
266 ** the ISO year begin on?
268 bot = ((yday + 11 - wday) %
271 ** What yday does the NEXT
272 ** ISO year begin on?
285 w = 1 + ((yday - bot) /
290 yday += isleap(year) ?
294 #ifdef XPG4_1994_04_09
296 && t->tm_mon == TM_JANUARY)
298 && t->tm_mon == TM_DECEMBER))
300 #endif /* defined XPG4_1994_04_09 */
301 if (*format == 'V') {
302 if (!_conv(w, 2, '0',
305 } else if (*format == 'g') {
306 if (!_conv(year % 100, 2, '0',
309 } else if (!_conv(year, 4, '0',
315 if (!_conv(MON_WEEK(t), 2, '0', pt, ptlim))
319 if (!_conv(t->tm_wday, 1, '0', pt, ptlim))
323 if (!_fmt(_CurrentTimeLocale->d_fmt, t, pt,
328 if (!_fmt(_CurrentTimeLocale->t_fmt, t, pt,
333 if (!_conv((t->tm_year + TM_YEAR_BASE) % 100,
338 if (!_conv((t->tm_year + TM_YEAR_BASE), 4, '0',
343 if (tzname[t->tm_isdst ? 1 : 0] &&
344 !_add(tzname[t->tm_isdst ? 1 : 0], pt,
350 * X311J/88-090 (4.12.3.5): if conversion char is
351 * undefined, behavior is undefined. Print out the
352 * character itself as printf(3) does.
362 return (ptlim - *pt);
369 const char * const ptlim;
376 buf[sizeof (buf) - 1] = '\0';
377 /* Make a copy, mktime(3) modifies the tm struct. */
380 for (p = buf + sizeof(buf) - 2; s > 0 && p > buf; s /= 10)
381 *p-- = (char)(s % 10 + '0');
382 return (_add(++p, pt, ptlim));
386 _conv(n, digits, pad, pt, ptlim)
390 const char * const ptlim;
395 buf[sizeof (buf) - 1] = '\0';
396 for (p = buf + sizeof(buf) - 2; n > 0 && p > buf; n /= 10, --digits)
398 while (p > buf && digits-- > 0)
400 return (_add(++p, pt, ptlim));
407 const char * const ptlim;
413 if ((**pt = *str++) == '\0')