4
0

gnu_strftime.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #ifdef _LIBC
  19. # define HAVE_LIMITS_H 1
  20. # define HAVE_MBLEN 1
  21. # define HAVE_MBRLEN 1
  22. # define HAVE_STRUCT_ERA_ENTRY 1
  23. # define HAVE_TM_GMTOFF 1
  24. # define HAVE_TM_ZONE 1
  25. # define HAVE_TZNAME 1
  26. # define HAVE_TZSET 1
  27. # define MULTIBYTE_IS_FORMAT_SAFE 1
  28. # define STDC_HEADERS 1
  29. # include "../locale/localeinfo.h"
  30. #endif
  31. #if defined emacs && !defined HAVE_BCOPY
  32. # define HAVE_MEMCPY 1
  33. #endif
  34. #include <ctype.h>
  35. #include <sys/types.h> /* Some systems define `time_t' here. */
  36. #ifdef TIME_WITH_SYS_TIME
  37. # include <sys/time.h>
  38. # include <time.h>
  39. #else
  40. # ifdef HAVE_SYS_TIME_H
  41. # include <sys/time.h>
  42. # else
  43. # include <time.h>
  44. # endif
  45. #endif
  46. #if HAVE_TZNAME
  47. extern char *tzname[];
  48. #endif
  49. /* Do multibyte processing if multibytes are supported, unless
  50. multibyte sequences are safe in formats. Multibyte sequences are
  51. safe if they cannot contain byte sequences that look like format
  52. conversion specifications. The GNU C Library uses UTF8 multibyte
  53. encoding, which is safe for formats, but strftime.c can be used
  54. with other C libraries that use unsafe encodings. */
  55. #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_FORMAT_SAFE)
  56. #if DO_MULTIBYTE
  57. # if HAVE_MBRLEN
  58. # include <wchar.h>
  59. # else
  60. /* Simulate mbrlen with mblen as best we can. */
  61. # define mbstate_t int
  62. # define mbrlen(s, n, ps) mblen (s, n)
  63. # define mbsinit(ps) (*(ps) == 0)
  64. # endif
  65. static const mbstate_t mbstate_zero;
  66. #endif
  67. #if HAVE_LIMITS_H
  68. # include <limits.h>
  69. #endif
  70. #if STDC_HEADERS
  71. # include <stddef.h>
  72. # include <stdlib.h>
  73. # include <string.h>
  74. #else
  75. # ifndef HAVE_MEMCPY
  76. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  77. # endif
  78. #endif
  79. #ifdef _LIBC
  80. # define MEMPCPY(d, s, n) __mempcpy (d, s, n)
  81. #else
  82. # ifndef HAVE_MEMPCPY
  83. # define MEMPCPY(d, s, n) ((void *) ((char *) memcpy (d, s, n) + (n)))
  84. # endif
  85. #endif
  86. #ifndef __P
  87. # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
  88. # define __P(args) args
  89. # else
  90. # define __P(args) ()
  91. # endif /* GCC. */
  92. #endif /* Not __P. */
  93. #ifndef PTR
  94. # ifdef __STDC__
  95. # define PTR void *
  96. # else
  97. # define PTR char *
  98. # endif
  99. #endif
  100. #ifndef CHAR_BIT
  101. # define CHAR_BIT 8
  102. #endif
  103. #ifndef NULL
  104. # define NULL 0
  105. #endif
  106. #define TYPE_SIGNED(t) ((t) -1 < 0)
  107. /* Bound on length of the string representing an integer value of type t.
  108. Subtract one for the sign bit if t is signed;
  109. 302 / 1000 is log10 (2) rounded up;
  110. add one for integer division truncation;
  111. add one more for a minus sign if t is signed. */
  112. #define INT_STRLEN_BOUND(t) \
  113. ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 + 1 + TYPE_SIGNED (t))
  114. #define TM_YEAR_BASE 1900
  115. #ifndef __isleap
  116. /* Nonzero if YEAR is a leap year (every 4 years,
  117. except every 100th isn't, and every 400th is). */
  118. # define __isleap(year) \
  119. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  120. #endif
  121. #ifdef _LIBC
  122. # define my_strftime_gmtime_r __gmtime_r
  123. # define my_strftime_localtime_r __localtime_r
  124. # define tzname __tzname
  125. # define tzset __tzset
  126. #else
  127. /* If we're a strftime substitute in a GNU program, then prefer gmtime
  128. to gmtime_r, since many gmtime_r implementations are buggy.
  129. Similarly for localtime_r. */
  130. # if ! HAVE_TM_GMTOFF
  131. static struct tm *my_strftime_gmtime_r __P ((const time_t *, struct tm *));
  132. static struct tm *
  133. my_strftime_gmtime_r (t, tp)
  134. const time_t *t;
  135. struct tm *tp;
  136. {
  137. struct tm *l = gmtime (t);
  138. if (! l)
  139. return 0;
  140. *tp = *l;
  141. return tp;
  142. }
  143. # endif /* ! HAVE_TM_GMTOFF */
  144. static struct tm *my_strftime_localtime_r __P ((const time_t *, struct tm *));
  145. static struct tm *
  146. my_strftime_localtime_r (t, tp)
  147. const time_t *t;
  148. struct tm *tp;
  149. {
  150. struct tm *l = localtime (t);
  151. if (! l)
  152. return 0;
  153. *tp = *l;
  154. return tp;
  155. }
  156. #endif /* ! defined _LIBC */
  157. #if !defined memset && !defined HAVE_MEMSET && !defined _LIBC
  158. /* Some systems lack the `memset' function and we don't want to
  159. introduce additional dependencies. */
  160. /* The SGI compiler reportedly barfs on the trailing null
  161. if we use a string constant as the initializer. 28 June 1997, rms. */
  162. static const char spaces[16] = /* " " */
  163. { ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
  164. static const char zeroes[16] = /* "0000000000000000" */
  165. { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' };
  166. # define memset_space(P, Len) \
  167. do { \
  168. int _len = (Len); \
  169. \
  170. do \
  171. { \
  172. int _this = _len > 16 ? 16 : _len; \
  173. (P) = MEMPCPY ((P), spaces, _this); \
  174. _len -= _this; \
  175. } \
  176. while (_len > 0); \
  177. } while (0)
  178. # define memset_zero(P, Len) \
  179. do { \
  180. int _len = (Len); \
  181. \
  182. do \
  183. { \
  184. int _this = _len > 16 ? 16 : _len; \
  185. (P) = MEMPCPY ((P), zeroes, _this); \
  186. _len -= _this; \
  187. } \
  188. while (_len > 0); \
  189. } while (0)
  190. #else
  191. # define memset_space(P, Len) (memset ((P), ' ', (Len)), (P) += (Len))
  192. # define memset_zero(P, Len) (memset ((P), '0', (Len)), (P) += (Len))
  193. #endif
  194. #define add(n, f) \
  195. do \
  196. { \
  197. int _n = (n); \
  198. int _delta = width - _n; \
  199. int _incr = _n + (_delta > 0 ? _delta : 0); \
  200. if (i + _incr >= maxsize) \
  201. return 0; \
  202. if (p) \
  203. { \
  204. if (_delta > 0) \
  205. { \
  206. if (pad == '0') \
  207. memset_zero (p, _delta); \
  208. else \
  209. memset_space (p, _delta); \
  210. } \
  211. f; \
  212. p += _n; \
  213. } \
  214. i += _incr; \
  215. } while (0)
  216. #define cpy(n, s) \
  217. add ((n), \
  218. if (to_lowcase) \
  219. memcpy_lowcase (p, (s), _n); \
  220. else if (to_uppcase) \
  221. memcpy_uppcase (p, (s), _n); \
  222. else \
  223. memcpy ((PTR) p, (PTR) (s), _n))
  224. #ifdef _LIBC
  225. # define TOUPPER(Ch) toupper (Ch)
  226. # define TOLOWER(Ch) tolower (Ch)
  227. #else
  228. # define TOUPPER(Ch) (islower (Ch) ? toupper (Ch) : (Ch))
  229. # define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
  230. #endif
  231. /* We don't use `isdigit' here since the locale dependent
  232. interpretation is not what we want here. We only need to accept
  233. the arabic digits in the ASCII range. One day there is perhaps a
  234. more reliable way to accept other sets of digits. */
  235. #define ISDIGIT(Ch) ((unsigned int) (Ch) - '0' <= 9)
  236. static char *memcpy_lowcase __P ((char *dest, const char *src, size_t len));
  237. static char *
  238. memcpy_lowcase (dest, src, len)
  239. char *dest;
  240. const char *src;
  241. size_t len;
  242. {
  243. while (len-- > 0)
  244. dest[len] = TOLOWER ((unsigned char) src[len]);
  245. return dest;
  246. }
  247. static char *memcpy_uppcase __P ((char *dest, const char *src, size_t len));
  248. static char *
  249. memcpy_uppcase (dest, src, len)
  250. char *dest;
  251. const char *src;
  252. size_t len;
  253. {
  254. while (len-- > 0)
  255. dest[len] = TOUPPER ((unsigned char) src[len]);
  256. return dest;
  257. }
  258. #if ! HAVE_TM_GMTOFF
  259. /* Yield the difference between *A and *B,
  260. measured in seconds, ignoring leap seconds. */
  261. # define tm_diff ftime_tm_diff
  262. static int tm_diff __P ((const struct tm *, const struct tm *));
  263. static int
  264. tm_diff (a, b)
  265. const struct tm *a;
  266. const struct tm *b;
  267. {
  268. /* Compute intervening leap days correctly even if year is negative.
  269. Take care to avoid int overflow in leap day calculations,
  270. but it's OK to assume that A and B are close to each other. */
  271. int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
  272. int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
  273. int a100 = a4 / 25 - (a4 % 25 < 0);
  274. int b100 = b4 / 25 - (b4 % 25 < 0);
  275. int a400 = a100 >> 2;
  276. int b400 = b100 >> 2;
  277. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  278. int years = a->tm_year - b->tm_year;
  279. int days = (365 * years + intervening_leap_days
  280. + (a->tm_yday - b->tm_yday));
  281. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  282. + (a->tm_min - b->tm_min))
  283. + (a->tm_sec - b->tm_sec));
  284. }
  285. #endif /* ! HAVE_TM_GMTOFF */
  286. /* The number of days from the first day of the first ISO week of this
  287. year to the year day YDAY with week day WDAY. ISO weeks start on
  288. Monday; the first ISO week has the year's first Thursday. YDAY may
  289. be as small as YDAY_MINIMUM. */
  290. #define ISO_WEEK_START_WDAY 1 /* Monday */
  291. #define ISO_WEEK1_WDAY 4 /* Thursday */
  292. #define YDAY_MINIMUM (-366)
  293. static int iso_week_days __P ((int, int));
  294. #ifdef __GNUC__
  295. __inline__
  296. #endif
  297. static int
  298. iso_week_days (yday, wday)
  299. int yday;
  300. int wday;
  301. {
  302. /* Add enough to the first operand of % to make it nonnegative. */
  303. int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
  304. return (yday
  305. - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7
  306. + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);
  307. }
  308. #if !(defined _NL_CURRENT || HAVE_STRFTIME)
  309. static char const weekday_name[][10] =
  310. {
  311. "Sunday", "Monday", "Tuesday", "Wednesday",
  312. "Thursday", "Friday", "Saturday"
  313. };
  314. static char const month_name[][10] =
  315. {
  316. "January", "February", "March", "April", "May", "June",
  317. "July", "August", "September", "October", "November", "December"
  318. };
  319. #endif
  320. #ifdef emacs
  321. # define my_strftime emacs_strftimeu
  322. # define ut_argument , ut
  323. # define ut_argument_spec int ut;
  324. # define ut_argument_spec_iso , int ut
  325. #else
  326. # define my_strftime strftime
  327. # define ut_argument
  328. # define ut_argument_spec
  329. # define ut_argument_spec_iso
  330. /* We don't have this information in general. */
  331. # define ut 0
  332. #endif
  333. #if !defined _LIBC && HAVE_TZNAME && HAVE_TZSET
  334. /* Solaris 2.5 tzset sometimes modifies the storage returned by localtime.
  335. Work around this bug by copying *tp before it might be munged. */
  336. size_t _strftime_copytm __P ((char *, size_t, const char *,
  337. const struct tm * ut_argument_spec_iso));
  338. size_t
  339. my_strftime (s, maxsize, format, tp ut_argument)
  340. char *s;
  341. size_t maxsize;
  342. const char *format;
  343. const struct tm *tp;
  344. ut_argument_spec
  345. {
  346. struct tm tmcopy;
  347. tmcopy = *tp;
  348. return _strftime_copytm (s, maxsize, format, &tmcopy ut_argument);
  349. }
  350. # undef my_strftime
  351. # define my_strftime(S, Maxsize, Format, Tp) \
  352. _strftime_copytm (S, Maxsize, Format, Tp)
  353. #endif
  354. /* Write information from TP into S according to the format
  355. string FORMAT, writing no more that MAXSIZE characters
  356. (including the terminating '\0') and returning number of
  357. characters written. If S is NULL, nothing will be written
  358. anywhere, so to determine how many characters would be
  359. written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */
  360. size_t
  361. my_strftime (s, maxsize, format, tp ut_argument)
  362. char *s;
  363. size_t maxsize;
  364. const char *format;
  365. const struct tm *tp;
  366. ut_argument_spec
  367. {
  368. int hour12 = tp->tm_hour;
  369. #ifdef _NL_CURRENT
  370. /* We cannot make the following values variables since we must delay
  371. the evaluation of these values until really needed since some
  372. expressions might not be valid in every situation. The `struct tm'
  373. might be generated by a strptime() call that initialized
  374. only a few elements. Dereference the pointers only if the format
  375. requires this. Then it is ok to fail if the pointers are invalid. */
  376. # define a_wkday _NL_CURRENT (LC_TIME, ABDAY_1 + tp->tm_wday)
  377. # define f_wkday _NL_CURRENT (LC_TIME, DAY_1 + tp->tm_wday)
  378. # define a_month _NL_CURRENT (LC_TIME, ABMON_1 + tp->tm_mon)
  379. # define f_month _NL_CURRENT (LC_TIME, MON_1 + tp->tm_mon)
  380. # define ampm _NL_CURRENT (LC_TIME, tp->tm_hour > 11 ? PM_STR : AM_STR)
  381. # define aw_len strlen (a_wkday)
  382. # define am_len strlen (a_month)
  383. # define ap_len strlen (ampm)
  384. #else
  385. # if !HAVE_STRFTIME
  386. # define f_wkday (weekday_name[tp->tm_wday])
  387. # define f_month (month_name[tp->tm_mon])
  388. # define a_wkday f_wkday
  389. # define a_month f_month
  390. # define ampm ("AMPM" + 2 * (tp->tm_hour > 11))
  391. size_t aw_len = 3;
  392. size_t am_len = 3;
  393. size_t ap_len = 2;
  394. # endif
  395. #endif
  396. const char *zone;
  397. size_t i = 0;
  398. char *p = s;
  399. const char *f;
  400. zone = NULL;
  401. #if HAVE_TM_ZONE
  402. /* The POSIX test suite assumes that setting
  403. the environment variable TZ to a new value before calling strftime()
  404. will influence the result (the %Z format) even if the information in
  405. TP is computed with a totally different time zone.
  406. This is bogus: though POSIX allows bad behavior like this,
  407. POSIX does not require it. Do the right thing instead. */
  408. zone = (const char *) tp->tm_zone;
  409. #endif
  410. #if HAVE_TZNAME
  411. if (ut)
  412. {
  413. if (! (zone && *zone))
  414. zone = "GMT";
  415. }
  416. else
  417. {
  418. /* POSIX.1 8.1.1 requires that whenever strftime() is called, the
  419. time zone names contained in the external variable `tzname' shall
  420. be set as if the tzset() function had been called. */
  421. # if HAVE_TZSET
  422. tzset ();
  423. # endif
  424. }
  425. #endif
  426. if (hour12 > 12)
  427. hour12 -= 12;
  428. else
  429. if (hour12 == 0)
  430. hour12 = 12;
  431. for (f = format; *f != '\0'; ++f)
  432. {
  433. int pad = 0; /* Padding for number ('-', '_', or 0). */
  434. int modifier; /* Field modifier ('E', 'O', or 0). */
  435. int digits; /* Max digits for numeric format. */
  436. int number_value; /* Numeric value to be printed. */
  437. int negative_number; /* 1 if the number is negative. */
  438. const char *subfmt;
  439. char *bufp;
  440. char buf[1 + (sizeof (int) < sizeof (time_t)
  441. ? INT_STRLEN_BOUND (time_t)
  442. : INT_STRLEN_BOUND (int))];
  443. int width = -1;
  444. int to_lowcase = 0;
  445. int to_uppcase = 0;
  446. int change_case = 0;
  447. int format_char;
  448. #if DO_MULTIBYTE
  449. switch (*f)
  450. {
  451. case '%':
  452. break;
  453. case '\a': case '\b': case '\t': case '\n':
  454. case '\v': case '\f': case '\r':
  455. case ' ': case '!': case '"': case '#': case '&': case'\'':
  456. case '(': case ')': case '*': case '+': case ',': case '-':
  457. case '.': case '/': case '0': case '1': case '2': case '3':
  458. case '4': case '5': case '6': case '7': case '8': case '9':
  459. case ':': case ';': case '<': case '=': case '>': case '?':
  460. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  461. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  462. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  463. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  464. case 'Y': case 'Z': case '[': case'\\': case ']': case '^':
  465. case '_': case 'a': case 'b': case 'c': case 'd': case 'e':
  466. case 'f': case 'g': case 'h': case 'i': case 'j': case 'k':
  467. case 'l': case 'm': case 'n': case 'o': case 'p': case 'q':
  468. case 'r': case 's': case 't': case 'u': case 'v': case 'w':
  469. case 'x': case 'y': case 'z': case '{': case '|': case '}':
  470. case '~':
  471. /* The C Standard requires these 98 characters (plus '%') to
  472. be in the basic execution character set. None of these
  473. characters can start a multibyte sequence, so they need
  474. not be analyzed further. */
  475. add (1, *p = *f);
  476. continue;
  477. default:
  478. /* Copy this multibyte sequence until we reach its end, find
  479. an error, or come back to the initial shift state. */
  480. {
  481. mbstate_t mbstate = mbstate_zero;
  482. size_t len = 0;
  483. do
  484. {
  485. size_t bytes = mbrlen (f + len, (size_t) -1, &mbstate);
  486. if (bytes == 0)
  487. break;
  488. if (bytes == (size_t) -2)
  489. {
  490. len += strlen (f + len);
  491. break;
  492. }
  493. if (bytes == (size_t) -1)
  494. {
  495. len++;
  496. break;
  497. }
  498. len += bytes;
  499. }
  500. while (! mbsinit (&mbstate));
  501. cpy (len, f);
  502. f += len - 1;
  503. continue;
  504. }
  505. }
  506. #else /* ! DO_MULTIBYTE */
  507. /* Either multibyte encodings are not supported, or they are
  508. safe for formats, so any non-'%' byte can be copied through. */
  509. if (*f != '%')
  510. {
  511. add (1, *p = *f);
  512. continue;
  513. }
  514. #endif /* ! DO_MULTIBYTE */
  515. /* Check for flags that can modify a format. */
  516. while (1)
  517. {
  518. switch (*++f)
  519. {
  520. /* This influences the number formats. */
  521. case '_':
  522. case '-':
  523. case '0':
  524. pad = *f;
  525. continue;
  526. /* This changes textual output. */
  527. case '^':
  528. to_uppcase = 1;
  529. continue;
  530. case '#':
  531. change_case = 1;
  532. continue;
  533. default:
  534. break;
  535. }
  536. break;
  537. }
  538. /* As a GNU extension we allow to specify the field width. */
  539. if (ISDIGIT (*f))
  540. {
  541. width = 0;
  542. do
  543. {
  544. width *= 10;
  545. width += *f - '0';
  546. ++f;
  547. }
  548. while (ISDIGIT (*f));
  549. }
  550. /* Check for modifiers. */
  551. switch (*f)
  552. {
  553. case 'E':
  554. case 'O':
  555. modifier = *f++;
  556. break;
  557. default:
  558. modifier = 0;
  559. break;
  560. }
  561. /* Now do the specified format. */
  562. format_char = *f;
  563. switch (format_char)
  564. {
  565. #define DO_NUMBER(d, v) \
  566. digits = width == -1 ? d : width; \
  567. number_value = v; goto do_number
  568. #define DO_NUMBER_SPACEPAD(d, v) \
  569. digits = width == -1 ? d : width; \
  570. number_value = v; goto do_number_spacepad
  571. case '%':
  572. if (modifier != 0)
  573. goto bad_format;
  574. add (1, *p = *f);
  575. break;
  576. case 'a':
  577. if (modifier != 0)
  578. goto bad_format;
  579. if (change_case)
  580. {
  581. to_uppcase = 1;
  582. to_lowcase = 0;
  583. }
  584. #if defined _NL_CURRENT || !HAVE_STRFTIME
  585. cpy (aw_len, a_wkday);
  586. break;
  587. #else
  588. goto underlying_strftime;
  589. #endif
  590. case 'A':
  591. if (modifier != 0)
  592. goto bad_format;
  593. if (change_case)
  594. {
  595. to_uppcase = 1;
  596. to_lowcase = 0;
  597. }
  598. #if defined _NL_CURRENT || !HAVE_STRFTIME
  599. cpy (strlen (f_wkday), f_wkday);
  600. break;
  601. #else
  602. goto underlying_strftime;
  603. #endif
  604. case 'b':
  605. case 'h': /* POSIX.2 extension. */
  606. if (modifier != 0)
  607. goto bad_format;
  608. #if defined _NL_CURRENT || !HAVE_STRFTIME
  609. cpy (am_len, a_month);
  610. break;
  611. #else
  612. goto underlying_strftime;
  613. #endif
  614. case 'B':
  615. if (modifier != 0)
  616. goto bad_format;
  617. if (change_case)
  618. {
  619. to_uppcase = 1;
  620. to_lowcase = 0;
  621. }
  622. #if defined _NL_CURRENT || !HAVE_STRFTIME
  623. cpy (strlen (f_month), f_month);
  624. break;
  625. #else
  626. goto underlying_strftime;
  627. #endif
  628. case 'c':
  629. if (modifier == 'O')
  630. goto bad_format;
  631. #ifdef _NL_CURRENT
  632. if (! (modifier == 'E'
  633. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT)) != '\0'))
  634. subfmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  635. #else
  636. # if HAVE_STRFTIME
  637. goto underlying_strftime;
  638. # else
  639. subfmt = "%a %b %e %H:%M:%S %Y";
  640. # endif
  641. #endif
  642. subformat:
  643. {
  644. char *old_start = p;
  645. size_t len = my_strftime (NULL, (size_t) -1, subfmt, tp);
  646. add (len, my_strftime (p, maxsize - i, subfmt, tp));
  647. if (to_uppcase)
  648. while (old_start < p)
  649. {
  650. *old_start = TOUPPER ((unsigned char) *old_start);
  651. ++old_start;
  652. }
  653. }
  654. break;
  655. #if HAVE_STRFTIME && ! (defined _NL_CURRENT && HAVE_STRUCT_ERA_ENTRY)
  656. underlying_strftime:
  657. {
  658. /* The relevant information is available only via the
  659. underlying strftime implementation, so use that. */
  660. char ufmt[4];
  661. char *u = ufmt;
  662. char ubuf[1024]; /* enough for any single format in practice */
  663. size_t len;
  664. *u++ = '%';
  665. if (modifier != 0)
  666. *u++ = modifier;
  667. *u++ = format_char;
  668. *u = '\0';
  669. len = strftime (ubuf, sizeof ubuf, ufmt, tp);
  670. if (len == 0 && ubuf[0] != '\0')
  671. return 0;
  672. cpy (len, ubuf);
  673. }
  674. break;
  675. #endif
  676. case 'C': /* POSIX.2 extension. */
  677. if (modifier == 'O')
  678. goto bad_format;
  679. if (modifier == 'E')
  680. {
  681. #if HAVE_STRUCT_ERA_ENTRY
  682. struct era_entry *era = _nl_get_era_entry (tp);
  683. if (era)
  684. {
  685. size_t len = strlen (era->name_fmt);
  686. cpy (len, era->name_fmt);
  687. break;
  688. }
  689. #else
  690. # if HAVE_STRFTIME
  691. goto underlying_strftime;
  692. # endif
  693. #endif
  694. }
  695. {
  696. int year = tp->tm_year + TM_YEAR_BASE;
  697. DO_NUMBER (1, year / 100 - (year % 100 < 0));
  698. }
  699. case 'x':
  700. if (modifier == 'O')
  701. goto bad_format;
  702. #ifdef _NL_CURRENT
  703. if (! (modifier == 'E'
  704. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_FMT)) != '\0'))
  705. subfmt = _NL_CURRENT (LC_TIME, D_FMT);
  706. goto subformat;
  707. #else
  708. # if HAVE_STRFTIME
  709. goto underlying_strftime;
  710. # else
  711. /* Fall through. */
  712. # endif
  713. #endif
  714. case 'D': /* POSIX.2 extension. */
  715. if (modifier != 0)
  716. goto bad_format;
  717. subfmt = "%m/%d/%y";
  718. goto subformat;
  719. case 'd':
  720. if (modifier == 'E')
  721. goto bad_format;
  722. DO_NUMBER (2, tp->tm_mday);
  723. case 'e': /* POSIX.2 extension. */
  724. if (modifier == 'E')
  725. goto bad_format;
  726. DO_NUMBER_SPACEPAD (2, tp->tm_mday);
  727. /* All numeric formats set DIGITS and NUMBER_VALUE and then
  728. jump to one of these two labels. */
  729. do_number_spacepad:
  730. /* Force `_' flag unless overwritten by `0' flag. */
  731. if (pad != '0')
  732. pad = '_';
  733. do_number:
  734. /* Format the number according to the MODIFIER flag. */
  735. if (modifier == 'O' && 0 <= number_value)
  736. {
  737. #ifdef _NL_CURRENT
  738. /* Get the locale specific alternate representation of
  739. the number NUMBER_VALUE. If none exist NULL is returned. */
  740. const char *cp = _nl_get_alt_digit (number_value);
  741. if (cp != NULL)
  742. {
  743. size_t digitlen = strlen (cp);
  744. if (digitlen != 0)
  745. {
  746. cpy (digitlen, cp);
  747. break;
  748. }
  749. }
  750. #else
  751. # if HAVE_STRFTIME
  752. goto underlying_strftime;
  753. # endif
  754. #endif
  755. }
  756. {
  757. unsigned int u = number_value;
  758. bufp = buf + sizeof (buf);
  759. negative_number = number_value < 0;
  760. if (negative_number)
  761. u = -u;
  762. do
  763. *--bufp = u % 10 + '0';
  764. while ((u /= 10) != 0);
  765. }
  766. do_number_sign_and_padding:
  767. if (negative_number)
  768. *--bufp = '-';
  769. if (pad != '-')
  770. {
  771. int padding = digits - (buf + sizeof (buf) - bufp);
  772. if (pad == '_')
  773. {
  774. while (0 < padding--)
  775. *--bufp = ' ';
  776. }
  777. else
  778. {
  779. bufp += negative_number;
  780. while (0 < padding--)
  781. *--bufp = '0';
  782. if (negative_number)
  783. *--bufp = '-';
  784. }
  785. }
  786. cpy (buf + sizeof (buf) - bufp, bufp);
  787. break;
  788. case 'F':
  789. if (modifier != 0)
  790. goto bad_format;
  791. subfmt = "%Y-%m-%d";
  792. goto subformat;
  793. case 'H':
  794. if (modifier == 'E')
  795. goto bad_format;
  796. DO_NUMBER (2, tp->tm_hour);
  797. case 'I':
  798. if (modifier == 'E')
  799. goto bad_format;
  800. DO_NUMBER (2, hour12);
  801. case 'k': /* GNU extension. */
  802. if (modifier == 'E')
  803. goto bad_format;
  804. DO_NUMBER_SPACEPAD (2, tp->tm_hour);
  805. case 'l': /* GNU extension. */
  806. if (modifier == 'E')
  807. goto bad_format;
  808. DO_NUMBER_SPACEPAD (2, hour12);
  809. case 'j':
  810. if (modifier == 'E')
  811. goto bad_format;
  812. DO_NUMBER (3, 1 + tp->tm_yday);
  813. case 'M':
  814. if (modifier == 'E')
  815. goto bad_format;
  816. DO_NUMBER (2, tp->tm_min);
  817. case 'm':
  818. if (modifier == 'E')
  819. goto bad_format;
  820. DO_NUMBER (2, tp->tm_mon + 1);
  821. case 'n': /* POSIX.2 extension. */
  822. add (1, *p = '\n');
  823. break;
  824. case 'P':
  825. to_lowcase = 1;
  826. #if !defined _NL_CURRENT && HAVE_STRFTIME
  827. format_char = 'p';
  828. #endif
  829. /* FALLTHROUGH */
  830. case 'p':
  831. if (change_case)
  832. {
  833. to_uppcase = 0;
  834. to_lowcase = 1;
  835. }
  836. #if defined _NL_CURRENT || !HAVE_STRFTIME
  837. cpy (ap_len, ampm);
  838. break;
  839. #else
  840. goto underlying_strftime;
  841. #endif
  842. case 'R': /* GNU extension. */
  843. subfmt = "%H:%M";
  844. goto subformat;
  845. case 'r': /* POSIX.2 extension. */
  846. #ifdef _NL_CURRENT
  847. if (*(subfmt = _NL_CURRENT (LC_TIME, T_FMT_AMPM)) == '\0')
  848. #endif
  849. subfmt = "%I:%M:%S %p";
  850. goto subformat;
  851. case 'S':
  852. if (modifier == 'E')
  853. goto bad_format;
  854. DO_NUMBER (2, tp->tm_sec);
  855. case 's': /* GNU extension. */
  856. {
  857. struct tm ltm;
  858. time_t t;
  859. ltm = *tp;
  860. t = mktime (&ltm);
  861. /* Generate string value for T using time_t arithmetic;
  862. this works even if sizeof (long) < sizeof (time_t). */
  863. bufp = buf + sizeof (buf);
  864. negative_number = t < 0;
  865. do
  866. {
  867. int d = t % 10;
  868. t /= 10;
  869. if (negative_number)
  870. {
  871. d = -d;
  872. /* Adjust if division truncates to minus infinity. */
  873. if (0 < -1 % 10 && d < 0)
  874. {
  875. t++;
  876. d += 10;
  877. }
  878. }
  879. *--bufp = d + '0';
  880. }
  881. while (t != 0);
  882. digits = 1;
  883. goto do_number_sign_and_padding;
  884. }
  885. case 'X':
  886. if (modifier == 'O')
  887. goto bad_format;
  888. #ifdef _NL_CURRENT
  889. if (! (modifier == 'E'
  890. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_T_FMT)) != '\0'))
  891. subfmt = _NL_CURRENT (LC_TIME, T_FMT);
  892. goto subformat;
  893. #else
  894. # if HAVE_STRFTIME
  895. goto underlying_strftime;
  896. # else
  897. /* Fall through. */
  898. # endif
  899. #endif
  900. case 'T': /* POSIX.2 extension. */
  901. subfmt = "%H:%M:%S";
  902. goto subformat;
  903. case 't': /* POSIX.2 extension. */
  904. add (1, *p = '\t');
  905. break;
  906. case 'u': /* POSIX.2 extension. */
  907. DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1);
  908. case 'U':
  909. if (modifier == 'E')
  910. goto bad_format;
  911. DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7);
  912. case 'V':
  913. case 'g': /* GNU extension. */
  914. case 'G': /* GNU extension. */
  915. if (modifier == 'E')
  916. goto bad_format;
  917. {
  918. int year = tp->tm_year + TM_YEAR_BASE;
  919. int days = iso_week_days (tp->tm_yday, tp->tm_wday);
  920. if (days < 0)
  921. {
  922. /* This ISO week belongs to the previous year. */
  923. year--;
  924. days = iso_week_days (tp->tm_yday + (365 + __isleap (year)),
  925. tp->tm_wday);
  926. }
  927. else
  928. {
  929. int d = iso_week_days (tp->tm_yday - (365 + __isleap (year)),
  930. tp->tm_wday);
  931. if (0 <= d)
  932. {
  933. /* This ISO week belongs to the next year. */
  934. year++;
  935. days = d;
  936. }
  937. }
  938. switch (*f)
  939. {
  940. case 'g':
  941. DO_NUMBER (2, (year % 100 + 100) % 100);
  942. case 'G':
  943. DO_NUMBER (1, year);
  944. default:
  945. DO_NUMBER (2, days / 7 + 1);
  946. }
  947. }
  948. case 'W':
  949. if (modifier == 'E')
  950. goto bad_format;
  951. DO_NUMBER (2, (tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7);
  952. case 'w':
  953. if (modifier == 'E')
  954. goto bad_format;
  955. DO_NUMBER (1, tp->tm_wday);
  956. case 'Y':
  957. if (modifier == 'E')
  958. {
  959. #if HAVE_STRUCT_ERA_ENTRY
  960. struct era_entry *era = _nl_get_era_entry (tp);
  961. if (era)
  962. {
  963. subfmt = strchr (era->name_fmt, '\0') + 1;
  964. goto subformat;
  965. }
  966. #else
  967. # if HAVE_STRFTIME
  968. goto underlying_strftime;
  969. # endif
  970. #endif
  971. }
  972. if (modifier == 'O')
  973. goto bad_format;
  974. else
  975. DO_NUMBER (1, tp->tm_year + TM_YEAR_BASE);
  976. case 'y':
  977. if (modifier == 'E')
  978. {
  979. #if HAVE_STRUCT_ERA_ENTRY
  980. struct era_entry *era = _nl_get_era_entry (tp);
  981. if (era)
  982. {
  983. int delta = tp->tm_year - era->start_date[0];
  984. DO_NUMBER (1, (era->offset
  985. + (era->direction == '-' ? -delta : delta)));
  986. }
  987. #else
  988. # if HAVE_STRFTIME
  989. goto underlying_strftime;
  990. # endif
  991. #endif
  992. }
  993. DO_NUMBER (2, (tp->tm_year % 100 + 100) % 100);
  994. case 'Z':
  995. if (change_case)
  996. {
  997. to_uppcase = 0;
  998. to_lowcase = 1;
  999. }
  1000. #if HAVE_TZNAME
  1001. /* The tzset() call might have changed the value. */
  1002. if (!(zone && *zone) && tp->tm_isdst >= 0)
  1003. zone = tzname[tp->tm_isdst];
  1004. #endif
  1005. if (! zone)
  1006. zone = ""; /* POSIX.2 requires the empty string here. */
  1007. cpy (strlen (zone), zone);
  1008. break;
  1009. case 'z': /* GNU extension. */
  1010. if (tp->tm_isdst < 0)
  1011. break;
  1012. {
  1013. int diff;
  1014. #if HAVE_TM_GMTOFF
  1015. diff = tp->tm_gmtoff;
  1016. #else
  1017. if (ut)
  1018. diff = 0;
  1019. else
  1020. {
  1021. struct tm gtm;
  1022. struct tm ltm;
  1023. time_t lt;
  1024. ltm = *tp;
  1025. lt = mktime (&ltm);
  1026. if (lt == (time_t) -1)
  1027. {
  1028. /* mktime returns -1 for errors, but -1 is also a
  1029. valid time_t value. Check whether an error really
  1030. occurred. */
  1031. struct tm tm;
  1032. if (! my_strftime_localtime_r (&lt, &tm)
  1033. || ((ltm.tm_sec ^ tm.tm_sec)
  1034. | (ltm.tm_min ^ tm.tm_min)
  1035. | (ltm.tm_hour ^ tm.tm_hour)
  1036. | (ltm.tm_mday ^ tm.tm_mday)
  1037. | (ltm.tm_mon ^ tm.tm_mon)
  1038. | (ltm.tm_year ^ tm.tm_year)))
  1039. break;
  1040. }
  1041. if (! my_strftime_gmtime_r (&lt, &gtm))
  1042. break;
  1043. diff = tm_diff (&ltm, &gtm);
  1044. }
  1045. #endif
  1046. if (diff < 0)
  1047. {
  1048. add (1, *p = '-');
  1049. diff = -diff;
  1050. }
  1051. else
  1052. add (1, *p = '+');
  1053. diff /= 60;
  1054. DO_NUMBER (4, (diff / 60) * 100 + diff % 60);
  1055. }
  1056. case '\0': /* GNU extension: % at end of format. */
  1057. --f;
  1058. /* Fall through. */
  1059. default:
  1060. /* Unknown format; output the format, including the '%',
  1061. since this is most likely the right thing to do if a
  1062. multibyte string has been misparsed. */
  1063. bad_format:
  1064. {
  1065. int flen;
  1066. for (flen = 1; f[1 - flen] != '%'; flen++)
  1067. continue;
  1068. cpy (flen, &f[1 - flen]);
  1069. }
  1070. break;
  1071. }
  1072. }
  1073. if (p && maxsize != 0)
  1074. *p = '\0';
  1075. return i;
  1076. }
  1077. #ifdef emacs
  1078. /* For Emacs we have a separate interface which corresponds to the normal
  1079. strftime function and does not have the extra information whether the
  1080. TP arguments comes from a `gmtime' call or not. */
  1081. size_t
  1082. emacs_strftime (s, maxsize, format, tp)
  1083. char *s;
  1084. size_t maxsize;
  1085. const char *format;
  1086. const struct tm *tp;
  1087. {
  1088. return my_strftime (s, maxsize, format, tp, 0);
  1089. }
  1090. #endif