1#ifndef lint
2#ifndef NOID
3static char	elsieid[] = "@(#)strftime.c	8.1";
4/*
5** Based on the UCB version with the ID appearing below.
6** This is ANSIish only when "multibyte character == plain character".
7*/
8#endif /* !defined NOID */
9#endif /* !defined lint */
10
11#include "private.h"
12
13/*
14** Copyright (c) 1989 The Regents of the University of California.
15** All rights reserved.
16**
17** Redistribution and use in source and binary forms are permitted
18** provided that the above copyright notice and this paragraph are
19** duplicated in all such forms and that any documentation,
20** advertising materials, and other materials related to such
21** distribution and use acknowledge that the software was developed
22** by the University of California, Berkeley. The name of the
23** University may not be used to endorse or promote products derived
24** from this software without specific prior written permission.
25** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28*/
29
30#ifndef LIBC_SCCS
31#ifndef lint
32static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
33#endif /* !defined lint */
34#endif /* !defined LIBC_SCCS */
35
36#include "tzfile.h"
37#include "fcntl.h"
38#include "locale.h"
39#include "timelocal.h"
40
41#define Locale __get_current_time_locale()
42
43static char *	_add P((const char *, char *, const char *));
44static char *	_conv P((int, const char *, char *, const char *));
45static char *	_fmt P((const char *, const struct tm *, char *, const char *,
46			int *));
47static char *	_yconv P((int, int, int, int, char *, const char *));
48
49extern char *	tzname[];
50
51#ifndef YEAR_2000_NAME
52#define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
53#endif /* !defined YEAR_2000_NAME */
54
55#define IN_NONE	0
56#define IN_SOME	1
57#define IN_THIS	2
58#define IN_ALL	3
59
60#define NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
61
62size_t
63strftime(s, maxsize, format, t)
64char * const		s;
65const size_t		maxsize;
66const char * const	format;
67const struct tm * const	t;
68{
69	char *	p;
70	int	warn;
71
72	tzset();
73	warn = IN_NONE;
74	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
75#ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
76	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
77		(void) fprintf(stderr, "\n");
78		if (format == NULL)
79			(void) fprintf(stderr, "NULL strftime format ");
80		else	(void) fprintf(stderr, "strftime format \"%s\" ",
81				format);
82		(void) fprintf(stderr, "yields only two digits of years in ");
83		if (warn == IN_SOME)
84			(void) fprintf(stderr, "some locales");
85		else if (warn == IN_THIS)
86			(void) fprintf(stderr, "the current locale");
87		else	(void) fprintf(stderr, "all locales");
88		(void) fprintf(stderr, "\n");
89	}
90#endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
91	if (p == s + maxsize)
92		return 0;
93	*p = '\0';
94	return p - s;
95}
96
97static char *
98_fmt(format, t, pt, ptlim, warnp)
99const char *		format;
100const struct tm * const	t;
101char *			pt;
102const char * const	ptlim;
103int *			warnp;
104{
105	for ( ; *format; ++format) {
106		if (*format == '%') {
107label:
108			switch (*++format) {
109			case '\0':
110				--format;
111				break;
112			case '0':
113				// ignore '0'-modifier supported by glibc for compatibility
114				goto label;
115			case 'A':
116				pt = _add((t->tm_wday < 0 ||
117					t->tm_wday >= DAYSPERWEEK) ?
118					"?" : Locale->weekday[t->tm_wday],
119					pt, ptlim);
120				continue;
121			case 'a':
122				pt = _add((t->tm_wday < 0 ||
123					t->tm_wday >= DAYSPERWEEK) ?
124					"?" : Locale->wday[t->tm_wday],
125					pt, ptlim);
126				continue;
127			case 'B':
128				pt = _add((t->tm_mon < 0 ||
129					t->tm_mon >= MONSPERYEAR) ?
130					"?" : Locale->month[t->tm_mon],
131					pt, ptlim);
132				continue;
133			case 'b':
134			case 'h':
135				pt = _add((t->tm_mon < 0 ||
136					t->tm_mon >= MONSPERYEAR) ?
137					"?" : Locale->mon[t->tm_mon],
138					pt, ptlim);
139				continue;
140			case 'C':
141				/*
142				** %C used to do a...
143				**	_fmt("%a %b %e %X %Y", t);
144				** ...whereas now POSIX 1003.2 calls for
145				** something completely different.
146				** (ado, 1993-05-24)
147				*/
148				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
149					pt, ptlim);
150				continue;
151			case 'c':
152				{
153				int warn2 = IN_SOME;
154
155				pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
156				if (warn2 > *warnp)
157					*warnp = warn2;
158				}
159				continue;
160			case 'D':
161				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
162				continue;
163			case 'd':
164				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
165				continue;
166			case 'E':
167			case 'O':
168				/*
169				** C99 locale modifiers.
170				** The sequences
171				**	%Ec %EC %Ex %EX %Ey %EY
172				**	%Od %oe %OH %OI %Om %OM
173				**	%OS %Ou %OU %OV %Ow %OW %Oy
174				** are supposed to provide alternate
175				** representations.
176				*/
177				goto label;
178			case 'e':
179				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
180				continue;
181			case 'F':
182				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
183				continue;
184			case 'H':
185				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
186				continue;
187			case 'I':
188				pt = _conv((t->tm_hour % 12) ?
189					(t->tm_hour % 12) : 12,
190					"%02d", pt, ptlim);
191				continue;
192			case 'j':
193				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
194				continue;
195			case 'k':
196				/*
197				** This used to be...
198				**	_conv(t->tm_hour % 12 ?
199				**		t->tm_hour % 12 : 12, 2, ' ');
200				** ...and has been changed to the below to
201				** match SunOS 4.1.1 and Arnold Robbins'
202				** strftime version 3.0. That is, "%k" and
203				** "%l" have been swapped.
204				** (ado, 1993-05-24)
205				*/
206				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
207				continue;
208#ifdef KITCHEN_SINK
209			case 'K':
210				/*
211				** After all this time, still unclaimed!
212				*/
213				pt = _add("kitchen sink", pt, ptlim);
214				continue;
215#endif /* defined KITCHEN_SINK */
216			case 'l':
217				/*
218				** This used to be...
219				**	_conv(t->tm_hour, 2, ' ');
220				** ...and has been changed to the below to
221				** match SunOS 4.1.1 and Arnold Robbin's
222				** strftime version 3.0. That is, "%k" and
223				** "%l" have been swapped.
224				** (ado, 1993-05-24)
225				*/
226				pt = _conv((t->tm_hour % 12) ?
227					(t->tm_hour % 12) : 12,
228					"%2d", pt, ptlim);
229				continue;
230			case 'M':
231				pt = _conv(t->tm_min, "%02d", pt, ptlim);
232				continue;
233			case 'm':
234				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
235				continue;
236			case 'n':
237				pt = _add("\n", pt, ptlim);
238				continue;
239			case 'p':
240				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
241					Locale->pm :
242					Locale->am,
243					pt, ptlim);
244				continue;
245			case 'R':
246				pt = _fmt("%H:%M", t, pt, ptlim, warnp);
247				continue;
248			case 'r':
249				pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
250				continue;
251			case 'S':
252				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
253				continue;
254			case 's':
255				{
256					struct tm	tm;
257					char		buf[INT_STRLEN_MAXIMUM(
258								time_t) + 1];
259					time_t		mkt;
260
261					tm = *t;
262					mkt = mktime(&tm);
263					if (TYPE_SIGNED(time_t))
264						(void) sprintf(buf, "%ld",
265							(long) mkt);
266					else	(void) sprintf(buf, "%lu",
267							(unsigned long) mkt);
268					pt = _add(buf, pt, ptlim);
269				}
270				continue;
271			case 'T':
272				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
273				continue;
274			case 't':
275				pt = _add("\t", pt, ptlim);
276				continue;
277			case 'U':
278				pt = _conv((t->tm_yday + DAYSPERWEEK -
279					t->tm_wday) / DAYSPERWEEK,
280					"%02d", pt, ptlim);
281				continue;
282			case 'u':
283				/*
284				** From Arnold Robbins' strftime version 3.0:
285				** "ISO 8601: Weekday as a decimal number
286				** [1 (Monday) - 7]"
287				** (ado, 1993-05-24)
288				*/
289				pt = _conv((t->tm_wday == 0) ?
290					DAYSPERWEEK : t->tm_wday,
291					"%d", pt, ptlim);
292				continue;
293			case 'V':	/* ISO 8601 week number */
294			case 'G':	/* ISO 8601 year (four digits) */
295			case 'g':	/* ISO 8601 year (two digits) */
296/*
297** From Arnold Robbins' strftime version 3.0: "the week number of the
298** year (the first Monday as the first day of week 1) as a decimal number
299** (01-53)."
300** (ado, 1993-05-24)
301**
302** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
303** "Week 01 of a year is per definition the first week which has the
304** Thursday in this year, which is equivalent to the week which contains
305** the fourth day of January. In other words, the first week of a new year
306** is the week which has the majority of its days in the new year. Week 01
307** might also contain days from the previous year and the week before week
308** 01 of a year is the last week (52 or 53) of the previous year even if
309** it contains days from the new year. A week starts with Monday (day 1)
310** and ends with Sunday (day 7). For example, the first week of the year
311** 1997 lasts from 1996-12-30 to 1997-01-05..."
312** (ado, 1996-01-02)
313*/
314				{
315					int	year;
316					int	base;
317					int	yday;
318					int	wday;
319					int	w;
320
321					year = t->tm_year;
322					base = TM_YEAR_BASE;
323					yday = t->tm_yday;
324					wday = t->tm_wday;
325					for ( ; ; ) {
326						int	len;
327						int	bot;
328						int	top;
329
330						len = isleap_sum(year, base) ?
331							DAYSPERLYEAR :
332							DAYSPERNYEAR;
333						/*
334						** What yday (-3 ... 3) does
335						** the ISO year begin on?
336						*/
337						bot = ((yday + 11 - wday) %
338							DAYSPERWEEK) - 3;
339						/*
340						** What yday does the NEXT
341						** ISO year begin on?
342						*/
343						top = bot -
344							(len % DAYSPERWEEK);
345						if (top < -3)
346							top += DAYSPERWEEK;
347						top += len;
348						if (yday >= top) {
349							++base;
350							w = 1;
351							break;
352						}
353						if (yday >= bot) {
354							w = 1 + ((yday - bot) /
355								DAYSPERWEEK);
356							break;
357						}
358						--base;
359						yday += isleap_sum(year, base) ?
360							DAYSPERLYEAR :
361							DAYSPERNYEAR;
362					}
363#ifdef XPG4_1994_04_09
364					if ((w == 52 &&
365						t->tm_mon == TM_JANUARY) ||
366						(w == 1 &&
367						t->tm_mon == TM_DECEMBER))
368							w = 53;
369#endif /* defined XPG4_1994_04_09 */
370					if (*format == 'V')
371						pt = _conv(w, "%02d",
372							pt, ptlim);
373					else if (*format == 'g') {
374						*warnp = IN_ALL;
375						pt = _yconv(year, base, 0, 1,
376							pt, ptlim);
377					} else	pt = _yconv(year, base, 1, 1,
378							pt, ptlim);
379				}
380				continue;
381			case 'v':
382				/*
383				** From Arnold Robbins' strftime version 3.0:
384				** "date as dd-bbb-YYYY"
385				** (ado, 1993-05-24)
386				*/
387				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
388				continue;
389			case 'W':
390				pt = _conv((t->tm_yday + DAYSPERWEEK -
391					(t->tm_wday ?
392					(t->tm_wday - 1) :
393					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
394					"%02d", pt, ptlim);
395				continue;
396			case 'w':
397				pt = _conv(t->tm_wday, "%d", pt, ptlim);
398				continue;
399			case 'X':
400				pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
401				continue;
402			case 'x':
403				{
404				int	warn2 = IN_SOME;
405
406				pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
407				if (warn2 == IN_ALL)
408					warn2 = IN_THIS;
409				if (warn2 > *warnp)
410					*warnp = warn2;
411				}
412				continue;
413			case 'y':
414				*warnp = IN_ALL;
415				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
416					pt, ptlim);
417				continue;
418			case 'Y':
419				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
420					pt, ptlim);
421				continue;
422			case 'Z':
423#ifdef TM_ZONE
424				if (t->TM_ZONE != NULL)
425					pt = _add(t->TM_ZONE, pt, ptlim);
426				else
427#endif /* defined TM_ZONE */
428				if (t->tm_isdst >= 0)
429					pt = _add(tzname[t->tm_isdst != 0],
430						pt, ptlim);
431				/*
432				** C99 says that %Z must be replaced by the
433				** empty string if the time zone is not
434				** determinable.
435				*/
436				continue;
437			case 'z':
438				{
439				int		diff;
440				char const *	sign;
441
442				if (t->tm_isdst < 0)
443					continue;
444#ifdef TM_GMTOFF
445				diff = t->TM_GMTOFF;
446#else /* !defined TM_GMTOFF */
447				/*
448				** C99 says that the UTC offset must
449				** be computed by looking only at
450				** tm_isdst. This requirement is
451				** incorrect, since it means the code
452				** must rely on magic (in this case
453				** altzone and timezone), and the
454				** magic might not have the correct
455				** offset. Doing things correctly is
456				** tricky and requires disobeying C99;
457				** see GNU C strftime for details.
458				** For now, punt and conform to the
459				** standard, even though it's incorrect.
460				**
461				** C99 says that %z must be replaced by the
462				** empty string if the time zone is not
463				** determinable, so output nothing if the
464				** appropriate variables are not available.
465				*/
466				if (t->tm_isdst == 0)
467#ifdef USG_COMPAT
468					diff = -timezone;
469#else /* !defined USG_COMPAT */
470					continue;
471#endif /* !defined USG_COMPAT */
472				else
473#ifdef ALTZONE
474					diff = -altzone;
475#else /* !defined ALTZONE */
476					continue;
477#endif /* !defined ALTZONE */
478#endif /* !defined TM_GMTOFF */
479				if (diff < 0) {
480					sign = "-";
481					diff = -diff;
482				} else	sign = "+";
483				pt = _add(sign, pt, ptlim);
484				diff /= SECSPERMIN;
485				diff = (diff / MINSPERHOUR) * 100 +
486					(diff % MINSPERHOUR);
487				pt = _conv(diff, "%04d", pt, ptlim);
488				}
489				continue;
490			case '+':
491				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
492					warnp);
493				continue;
494			case '%':
495			/*
496			** X311J/88-090 (4.12.3.5): if conversion char is
497			** undefined, behavior is undefined. Print out the
498			** character itself as printf(3) also does.
499			*/
500			default:
501				break;
502			}
503		}
504		if (pt == ptlim)
505			break;
506		*pt++ = *format;
507	}
508	return pt;
509}
510
511static char *
512_conv(n, format, pt, ptlim)
513const int		n;
514const char * const	format;
515char * const		pt;
516const char * const	ptlim;
517{
518	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
519
520	(void) sprintf(buf, format, n);
521	return _add(buf, pt, ptlim);
522}
523
524static char *
525_add(str, pt, ptlim)
526const char *		str;
527char *			pt;
528const char * const	ptlim;
529{
530	while (pt < ptlim && (*pt = *str++) != '\0')
531		++pt;
532	return pt;
533}
534
535/*
536** POSIX and the C Standard are unclear or inconsistent about
537** what %C and %y do if the year is negative or exceeds 9999.
538** Use the convention that %C concatenated with %y yields the
539** same output as %Y, and that %Y contains at least 4 bytes,
540** with more only if necessary.
541*/
542
543static char *
544_yconv(a, b, convert_top, convert_yy, pt, ptlim)
545const int		a;
546const int		b;
547const int		convert_top;
548const int		convert_yy;
549char *			pt;
550const char * const	ptlim;
551{
552	register int	lead;
553	register int	trail;
554
555#define DIVISOR	100
556	trail = a % DIVISOR + b % DIVISOR;
557	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
558	trail %= DIVISOR;
559	if (trail < 0 && lead > 0) {
560		trail += DIVISOR;
561		--lead;
562	} else if (lead < 0 && trail > 0) {
563		trail -= DIVISOR;
564		++lead;
565	}
566	if (convert_top) {
567		if (lead == 0 && trail < 0)
568			pt = _add("-0", pt, ptlim);
569		else	pt = _conv(lead, "%02d", pt, ptlim);
570	}
571	if (convert_yy)
572		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
573	return pt;
574}
575