strftime.c 16 KB

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