strftime.c 16 KB

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