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