xref: /netbsd-src/lib/libc/time/strftime.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: strftime.c,v 1.33 2014/10/07 21:51:03 christos Exp $	*/
2 
3 #include <sys/cdefs.h>
4 #if defined(LIBC_SCCS) && !defined(lint)
5 #if 0
6 static char	elsieid[] = "@(#)strftime.c	7.64";
7 static char	elsieid[] = "@(#)strftime.c	8.3";
8 #else
9 __RCSID("$NetBSD: strftime.c,v 1.33 2014/10/07 21:51:03 christos Exp $");
10 #endif
11 #endif /* LIBC_SCCS and not lint */
12 
13 #include "namespace.h"
14 
15 #include <stddef.h>
16 #include <assert.h>
17 #include <locale.h>
18 #include "setlocale_local.h"
19 
20 /*
21 ** Based on the UCB version with the copyright notice and sccsid
22 ** appearing below.
23 **
24 ** This is ANSIish only when "multibyte character == plain character".
25 */
26 
27 #include "private.h"
28 
29 /*
30 ** We don't use these extensions in strftime operation even when
31 ** supported by the local tzcode configuration.  A strictly
32 ** conforming C application may leave them in undefined state.
33 */
34 
35 #ifdef _LIBC
36 #undef TM_ZONE
37 #undef TM_GMTOFF
38 #endif
39 
40 /*
41 ** Copyright (c) 1989, 1993
42 **	The Regents of the University of California.  All rights reserved.
43 **
44 ** Redistribution and use in source and binary forms, with or without
45 ** modification, are permitted provided that the following conditions
46 ** are met:
47 ** 1. Redistributions of source code must retain the above copyright
48 **    notice, this list of conditions and the following disclaimer.
49 ** 2. Redistributions in binary form must reproduce the above copyright
50 **    notice, this list of conditions and the following disclaimer in the
51 **    documentation and/or other materials provided with the distribution.
52 ** 3. All advertising materials mentioning features or use of this software
53 **    must display the following acknowledgement:
54 **	This product includes software developed by the University of
55 **	California, Berkeley and its contributors.
56 ** 4. Neither the name of the University nor the names of its contributors
57 **    may be used to endorse or promote products derived from this software
58 **    without specific prior written permission.
59 **
60 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
61 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 ** SUCH DAMAGE.
71 */
72 
73 #include "tzfile.h"
74 #include "fcntl.h"
75 #include "locale.h"
76 
77 #ifdef __weak_alias
78 __weak_alias(strftime_l, _strftime_l)
79 __weak_alias(strftime_lz, _strftime_lz)
80 __weak_alias(strftime_z, _strftime_z)
81 #endif
82 
83 #include "sys/localedef.h"
84 #define _TIME_LOCALE(loc) \
85     ((_TimeLocale *)((loc)->part_impl[(size_t)LC_TIME]))
86 #define c_fmt   d_t_fmt
87 
88 static char *	_add(const char *, char *, const char *);
89 static char *	_conv(int, const char *, char *, const char *);
90 static char *	_fmt(const timezone_t, const char *, const struct tm *, char *,
91 			const char *, int *, locale_t);
92 static char *	_yconv(int, int, bool, bool, char *, const char *);
93 
94 extern char *	tzname[];
95 
96 #ifndef YEAR_2000_NAME
97 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
98 #endif /* !defined YEAR_2000_NAME */
99 
100 #define IN_NONE	0
101 #define IN_SOME	1
102 #define IN_THIS	2
103 #define IN_ALL	3
104 
105 size_t
106 strftime_z(const timezone_t sp, char * __restrict s, size_t maxsize,
107     const char * __restrict format, const struct tm * __restrict t)
108 {
109 	return strftime_lz(sp, s, maxsize, format, t, _current_locale());
110 }
111 
112 #if HAVE_STRFTIME_L
113 size_t
114 strftime_l(char *s, size_t maxsize, char const *format, struct tm const *t,
115 	   locale_t locale)
116 {
117   /* Just call strftime, as only the C locale is supported.  */
118   return strftime(s, maxsize, format, t);
119 }
120 #endif
121 
122 size_t
123 strftime_lz(const timezone_t sp, char *const s, const size_t maxsize,
124     const char *const format, const struct tm *const t, locale_t loc)
125 {
126 	char *	p;
127 	int	warn;
128 
129 	warn = IN_NONE;
130 	p = _fmt(sp, ((format == NULL) ? "%c" : format), t, s, s + maxsize,
131 	    &warn, loc);
132 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
133 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
134 		(void) fprintf(stderr, "\n");
135 		if (format == NULL)
136 			(void) fprintf(stderr, "NULL strftime format ");
137 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
138 				format);
139 		(void) fprintf(stderr, "yields only two digits of years in ");
140 		if (warn == IN_SOME)
141 			(void) fprintf(stderr, "some locales");
142 		else if (warn == IN_THIS)
143 			(void) fprintf(stderr, "the current locale");
144 		else	(void) fprintf(stderr, "all locales");
145 		(void) fprintf(stderr, "\n");
146 	}
147 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
148 	if (p == s + maxsize)
149 		return 0;
150 	*p = '\0';
151 	return p - s;
152 }
153 
154 static char *
155 _fmt(const timezone_t sp, const char *format, const struct tm *const t,
156 	char *pt, const char *const ptlim, int *warnp, locale_t loc)
157 {
158 	for ( ; *format; ++format) {
159 		if (*format == '%') {
160 label:
161 			switch (*++format) {
162 			case '\0':
163 				--format;
164 				break;
165 			case 'A':
166 				pt = _add((t->tm_wday < 0 ||
167 					t->tm_wday >= DAYSPERWEEK) ?
168 					"?" : _TIME_LOCALE(loc)->day[t->tm_wday],
169 					pt, ptlim);
170 				continue;
171 			case 'a':
172 				pt = _add((t->tm_wday < 0 ||
173 					t->tm_wday >= DAYSPERWEEK) ?
174 					"?" : _TIME_LOCALE(loc)->abday[t->tm_wday],
175 					pt, ptlim);
176 				continue;
177 			case 'B':
178 				pt = _add((t->tm_mon < 0 ||
179 					t->tm_mon >= MONSPERYEAR) ?
180 					"?" : _TIME_LOCALE(loc)->mon[t->tm_mon],
181 					pt, ptlim);
182 				continue;
183 			case 'b':
184 			case 'h':
185 				pt = _add((t->tm_mon < 0 ||
186 					t->tm_mon >= MONSPERYEAR) ?
187 					"?" : _TIME_LOCALE(loc)->abmon[t->tm_mon],
188 					pt, ptlim);
189 				continue;
190 			case 'C':
191 				/*
192 				** %C used to do a...
193 				**	_fmt("%a %b %e %X %Y", t);
194 				** ...whereas now POSIX 1003.2 calls for
195 				** something completely different.
196 				** (ado, 1993-05-24)
197 				*/
198 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
199 					    true, false, pt, ptlim);
200 				continue;
201 			case 'c':
202 				{
203 				int warn2 = IN_SOME;
204 
205 				pt = _fmt(sp, _TIME_LOCALE(loc)->c_fmt, t, pt,
206 				    ptlim, &warn2, loc);
207 				if (warn2 == IN_ALL)
208 					warn2 = IN_THIS;
209 				if (warn2 > *warnp)
210 					*warnp = warn2;
211 				}
212 				continue;
213 			case 'D':
214 				pt = _fmt(sp, "%m/%d/%y", t, pt, ptlim, warnp,
215 				    loc);
216 				continue;
217 			case 'd':
218 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
219 				continue;
220 			case 'E':
221 			case 'O':
222 				/*
223 				** C99 locale modifiers.
224 				** The sequences
225 				**	%Ec %EC %Ex %EX %Ey %EY
226 				**	%Od %oe %OH %OI %Om %OM
227 				**	%OS %Ou %OU %OV %Ow %OW %Oy
228 				** are supposed to provide alternate
229 				** representations.
230 				*/
231 				goto label;
232 			case 'e':
233 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
234 				continue;
235 			case 'F':
236 				pt = _fmt(sp, "%Y-%m-%d", t, pt, ptlim, warnp,
237 				    loc);
238 				continue;
239 			case 'H':
240 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
241 				continue;
242 			case 'I':
243 				pt = _conv((t->tm_hour % 12) ?
244 					(t->tm_hour % 12) : 12,
245 					"%02d", pt, ptlim);
246 				continue;
247 			case 'j':
248 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
249 				continue;
250 			case 'k':
251 				/*
252 				** This used to be...
253 				**	_conv(t->tm_hour % 12 ?
254 				**		t->tm_hour % 12 : 12, 2, ' ');
255 				** ...and has been changed to the below to
256 				** match SunOS 4.1.1 and Arnold Robbins'
257 				** strftime version 3.0. That is, "%k" and
258 				** "%l" have been swapped.
259 				** (ado, 1993-05-24)
260 				*/
261 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
262 				continue;
263 #ifdef KITCHEN_SINK
264 			case 'K':
265 				/*
266 				** After all this time, still unclaimed!
267 				*/
268 				pt = _add("kitchen sink", pt, ptlim);
269 				continue;
270 #endif /* defined KITCHEN_SINK */
271 			case 'l':
272 				/*
273 				** This used to be...
274 				**	_conv(t->tm_hour, 2, ' ');
275 				** ...and has been changed to the below to
276 				** match SunOS 4.1.1 and Arnold Robbin's
277 				** strftime version 3.0. That is, "%k" and
278 				** "%l" have been swapped.
279 				** (ado, 1993-05-24)
280 				*/
281 				pt = _conv((t->tm_hour % 12) ?
282 					(t->tm_hour % 12) : 12,
283 					"%2d", pt, ptlim);
284 				continue;
285 			case 'M':
286 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
287 				continue;
288 			case 'm':
289 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
290 				continue;
291 			case 'n':
292 				pt = _add("\n", pt, ptlim);
293 				continue;
294 			case 'p':
295 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
296 					_TIME_LOCALE(loc)->am_pm[1] :
297 					_TIME_LOCALE(loc)->am_pm[0],
298 					pt, ptlim);
299 				continue;
300 			case 'R':
301 				pt = _fmt(sp, "%H:%M", t, pt, ptlim, warnp,
302 				    loc);
303 				continue;
304 			case 'r':
305 				pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt_ampm, t,
306 				    pt, ptlim, warnp, loc);
307 				continue;
308 			case 'S':
309 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
310 				continue;
311 			case 's':
312 				{
313 					struct tm	tm;
314 					char		buf[INT_STRLEN_MAXIMUM(
315 								time_t) + 1];
316 					time_t		mkt;
317 
318 					tm = *t;
319 					mkt = mktime(&tm);
320 					/* CONSTCOND */
321 					if (TYPE_SIGNED(time_t))
322 						(void)snprintf(buf, sizeof(buf),
323 						    "%jd", (intmax_t) mkt);
324 					else	(void)snprintf(buf, sizeof(buf),
325 						    "%ju", (uintmax_t) mkt);
326 					pt = _add(buf, pt, ptlim);
327 				}
328 				continue;
329 			case 'T':
330 				pt = _fmt(sp, "%H:%M:%S", t, pt, ptlim, warnp,
331 				    loc);
332 				continue;
333 			case 't':
334 				pt = _add("\t", pt, ptlim);
335 				continue;
336 			case 'U':
337 				pt = _conv((t->tm_yday + DAYSPERWEEK -
338 					t->tm_wday) / DAYSPERWEEK,
339 					"%02d", pt, ptlim);
340 				continue;
341 			case 'u':
342 				/*
343 				** From Arnold Robbins' strftime version 3.0:
344 				** "ISO 8601: Weekday as a decimal number
345 				** [1 (Monday) - 7]"
346 				** (ado, 1993-05-24)
347 				*/
348 				pt = _conv((t->tm_wday == 0) ?
349 					DAYSPERWEEK : t->tm_wday,
350 					"%d", pt, ptlim);
351 				continue;
352 			case 'V':	/* ISO 8601 week number */
353 			case 'G':	/* ISO 8601 year (four digits) */
354 			case 'g':	/* ISO 8601 year (two digits) */
355 /*
356 ** From Arnold Robbins' strftime version 3.0: "the week number of the
357 ** year (the first Monday as the first day of week 1) as a decimal number
358 ** (01-53)."
359 ** (ado, 1993-05-24)
360 **
361 ** From <http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html> by Markus Kuhn:
362 ** "Week 01 of a year is per definition the first week which has the
363 ** Thursday in this year, which is equivalent to the week which contains
364 ** the fourth day of January. In other words, the first week of a new year
365 ** is the week which has the majority of its days in the new year. Week 01
366 ** might also contain days from the previous year and the week before week
367 ** 01 of a year is the last week (52 or 53) of the previous year even if
368 ** it contains days from the new year. A week starts with Monday (day 1)
369 ** and ends with Sunday (day 7). For example, the first week of the year
370 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
371 ** (ado, 1996-01-02)
372 */
373 				{
374 					int	year;
375 					int	base;
376 					int	yday;
377 					int	wday;
378 					int	w;
379 
380 					year = t->tm_year;
381 					base = TM_YEAR_BASE;
382 					yday = t->tm_yday;
383 					wday = t->tm_wday;
384 					for ( ; ; ) {
385 						int	len;
386 						int	bot;
387 						int	top;
388 
389 						len = isleap_sum(year, base) ?
390 							DAYSPERLYEAR :
391 							DAYSPERNYEAR;
392 						/*
393 						** What yday (-3 ... 3) does
394 						** the ISO year begin on?
395 						*/
396 						bot = ((yday + 11 - wday) %
397 							DAYSPERWEEK) - 3;
398 						/*
399 						** What yday does the NEXT
400 						** ISO year begin on?
401 						*/
402 						top = bot -
403 							(len % DAYSPERWEEK);
404 						if (top < -3)
405 							top += DAYSPERWEEK;
406 						top += len;
407 						if (yday >= top) {
408 							++base;
409 							w = 1;
410 							break;
411 						}
412 						if (yday >= bot) {
413 							w = 1 + ((yday - bot) /
414 								DAYSPERWEEK);
415 							break;
416 						}
417 						--base;
418 						yday += isleap_sum(year, base) ?
419 							DAYSPERLYEAR :
420 							DAYSPERNYEAR;
421 					}
422 #ifdef XPG4_1994_04_09
423 					if ((w == 52 &&
424 						t->tm_mon == TM_JANUARY) ||
425 						(w == 1 &&
426 						t->tm_mon == TM_DECEMBER))
427 							w = 53;
428 #endif /* defined XPG4_1994_04_09 */
429 					if (*format == 'V')
430 						pt = _conv(w, "%02d",
431 							pt, ptlim);
432 					else if (*format == 'g') {
433 						*warnp = IN_ALL;
434 						pt = _yconv(year, base,
435 							false, true,
436 							pt, ptlim);
437 					} else	pt = _yconv(year, base,
438 							true, true,
439 							pt, ptlim);
440 				}
441 				continue;
442 			case 'v':
443 				/*
444 				** From Arnold Robbins' strftime version 3.0:
445 				** "date as dd-bbb-YYYY"
446 				** (ado, 1993-05-24)
447 				*/
448 				pt = _fmt(sp, "%e-%b-%Y", t, pt, ptlim, warnp,
449 				    loc);
450 				continue;
451 			case 'W':
452 				pt = _conv((t->tm_yday + DAYSPERWEEK -
453 					(t->tm_wday ?
454 					(t->tm_wday - 1) :
455 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
456 					"%02d", pt, ptlim);
457 				continue;
458 			case 'w':
459 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
460 				continue;
461 			case 'X':
462 				pt = _fmt(sp, _TIME_LOCALE(loc)->t_fmt, t, pt,
463 				    ptlim, warnp, loc);
464 				continue;
465 			case 'x':
466 				{
467 				int	warn2 = IN_SOME;
468 
469 				pt = _fmt(sp, _TIME_LOCALE(loc)->d_fmt, t, pt,
470 				    ptlim, &warn2, loc);
471 				if (warn2 == IN_ALL)
472 					warn2 = IN_THIS;
473 				if (warn2 > *warnp)
474 					*warnp = warn2;
475 				}
476 				continue;
477 			case 'y':
478 				*warnp = IN_ALL;
479 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
480 					false, true,
481 					pt, ptlim);
482 				continue;
483 			case 'Y':
484 				pt = _yconv(t->tm_year, TM_YEAR_BASE,
485 					true, true,
486 					pt, ptlim);
487 				continue;
488 			case 'Z':
489 #ifdef TM_ZONE
490 				if (t->TM_ZONE != NULL)
491 					pt = _add(t->TM_ZONE, pt, ptlim);
492 				else
493 #endif /* defined TM_ZONE */
494 				if (t->tm_isdst >= 0)
495 					pt = _add((sp ?
496 					    tzgetname(sp, t->tm_isdst) :
497 					    tzname[t->tm_isdst != 0]),
498 					    pt, ptlim);
499 				/*
500 				** C99 says that %Z must be replaced by the
501 				** empty string if the time zone is not
502 				** determinable.
503 				*/
504 				continue;
505 			case 'z':
506 				{
507 				long		diff;
508 				char const *	sign;
509 
510 				if (t->tm_isdst < 0)
511 					continue;
512 #ifdef TM_GMTOFF
513 				diff = (int)t->TM_GMTOFF;
514 #else /* !defined TM_GMTOFF */
515 				/*
516 				** C99 says that the UT offset must
517 				** be computed by looking only at
518 				** tm_isdst. This requirement is
519 				** incorrect, since it means the code
520 				** must rely on magic (in this case
521 				** altzone and timezone), and the
522 				** magic might not have the correct
523 				** offset. Doing things correctly is
524 				** tricky and requires disobeying C99;
525 				** see GNU C strftime for details.
526 				** For now, punt and conform to the
527 				** standard, even though it's incorrect.
528 				**
529 				** C99 says that %z must be replaced by the
530 				** empty string if the time zone is not
531 				** determinable, so output nothing if the
532 				** appropriate variables are not available.
533 				*/
534 #ifndef STD_INSPIRED
535 				if (t->tm_isdst == 0)
536 #ifdef USG_COMPAT
537 					diff = -timezone;
538 #else /* !defined USG_COMPAT */
539 					continue;
540 #endif /* !defined USG_COMPAT */
541 				else
542 #ifdef ALTZONE
543 					diff = -altzone;
544 #else /* !defined ALTZONE */
545 					continue;
546 #endif /* !defined ALTZONE */
547 #else /* defined STD_INSPIRED */
548 				{
549 					struct tm tmp;
550 					time_t lct, gct;
551 
552 					/*
553 					** Get calendar time from t
554 					** being treated as local.
555 					*/
556 					tmp = *t; /* mktime discards const */
557 					lct = mktime(&tmp);
558 
559 					if (lct == (time_t)-1)
560 						continue;
561 
562 					/*
563 					** Get calendar time from t
564 					** being treated as GMT.
565 					**/
566 					tmp = *t; /* mktime discards const */
567 					gct = timegm(&tmp);
568 
569 					if (gct == (time_t)-1)
570 						continue;
571 
572 					/* LINTED difference will fit int */
573 					diff = (intmax_t)gct - (intmax_t)lct;
574 				}
575 #endif /* defined STD_INSPIRED */
576 #endif /* !defined TM_GMTOFF */
577 				if (diff < 0) {
578 					sign = "-";
579 					diff = -diff;
580 				} else	sign = "+";
581 				pt = _add(sign, pt, ptlim);
582 				diff /= SECSPERMIN;
583 				diff = (diff / MINSPERHOUR) * 100 +
584 					(diff % MINSPERHOUR);
585 				_DIAGASSERT(__type_fit(int, diff));
586 				pt = _conv((int)diff, "%04d", pt, ptlim);
587 				}
588 				continue;
589 #if 0
590 			case '+':
591 				pt = _fmt(sp, _TIME_LOCALE(loc)->date_fmt, t,
592 				    pt, ptlim, warnp, loc);
593 				continue;
594 #endif
595 			case '%':
596 			/*
597 			** X311J/88-090 (4.12.3.5): if conversion char is
598 			** undefined, behavior is undefined. Print out the
599 			** character itself as printf(3) also does.
600 			*/
601 			default:
602 				break;
603 			}
604 		}
605 		if (pt == ptlim)
606 			break;
607 		*pt++ = *format;
608 	}
609 	return pt;
610 }
611 
612 size_t
613 strftime(char * const s, const size_t maxsize,
614     const char * const format, const struct tm * const	t)
615 {
616 	tzset();
617 	return strftime_z(NULL, s, maxsize, format, t);
618 }
619 
620 size_t
621 strftime_l(char * __restrict s, size_t maxsize, const char * __restrict format,
622     const struct tm * __restrict t, locale_t loc)
623 {
624 	tzset();
625 	return strftime_lz(NULL, s, maxsize, format, t, loc);
626 }
627 
628 static char *
629 _conv(const int	n, const char *const format, char *const pt,
630     const char *const ptlim)
631 {
632 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
633 
634 	(void) snprintf(buf, sizeof(buf), format, n);
635 	return _add(buf, pt, ptlim);
636 }
637 
638 static char *
639 _add(const char *str, char *pt, const char *const ptlim)
640 {
641 	while (pt < ptlim && (*pt = *str++) != '\0')
642 		++pt;
643 	return pt;
644 }
645 
646 /*
647 ** POSIX and the C Standard are unclear or inconsistent about
648 ** what %C and %y do if the year is negative or exceeds 9999.
649 ** Use the convention that %C concatenated with %y yields the
650 ** same output as %Y, and that %Y contains at least 4 bytes,
651 ** with more only if necessary.
652 */
653 
654 static char *
655 _yconv(int a, int b, bool convert_top, bool convert_yy,
656     char *pt, const char *const ptlim)
657 {
658 	int	lead;
659 	int	trail;
660 
661 #define DIVISOR	100
662 	trail = a % DIVISOR + b % DIVISOR;
663 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
664 	trail %= DIVISOR;
665 	if (trail < 0 && lead > 0) {
666 		trail += DIVISOR;
667 		--lead;
668 	} else if (lead < 0 && trail > 0) {
669 		trail -= DIVISOR;
670 		++lead;
671 	}
672 	if (convert_top) {
673 		if (lead == 0 && trail < 0)
674 			pt = _add("-0", pt, ptlim);
675 		else	pt = _conv(lead, "%02d", pt, ptlim);
676 	}
677 	if (convert_yy)
678 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
679 	return pt;
680 }
681