strftime.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /* Copyright (C) 1991-2017 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 Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the 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. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #ifdef _LIBC
  15. # define USE_IN_EXTENDED_LOCALE_MODEL 1
  16. # define HAVE_STRUCT_ERA_ENTRY 1
  17. # define HAVE_TM_GMTOFF 1
  18. # define HAVE_TM_ZONE 1
  19. # define HAVE_TZNAME 1
  20. # define HAVE_TZSET 1
  21. # include "../locale/localeinfo.h"
  22. #else
  23. # include <config.h>
  24. # if FPRINTFTIME
  25. # include "fprintftime.h"
  26. # else
  27. # include "strftime.h"
  28. # endif
  29. # include "time-internal.h"
  30. #endif
  31. #include <ctype.h>
  32. #include <time.h>
  33. #if HAVE_TZNAME && !HAVE_DECL_TZNAME
  34. extern char *tzname[];
  35. #endif
  36. /* Do multibyte processing if multibyte encodings are supported, unless
  37. multibyte sequences are safe in formats. Multibyte sequences are
  38. safe if they cannot contain byte sequences that look like format
  39. conversion specifications. The multibyte encodings used by the
  40. C library on the various platforms (UTF-8, GB2312, GBK, CP936,
  41. GB18030, EUC-TW, BIG5, BIG5-HKSCS, CP950, EUC-JP, EUC-KR, CP949,
  42. SHIFT_JIS, CP932, JOHAB) are safe for formats, because the byte '%'
  43. cannot occur in a multibyte character except in the first byte.
  44. The DEC-HANYU encoding used on OSF/1 is not safe for formats, but
  45. this encoding has never been seen in real-life use, so we ignore
  46. it. */
  47. #if !(defined __osf__ && 0)
  48. # define MULTIBYTE_IS_FORMAT_SAFE 1
  49. #endif
  50. #define DO_MULTIBYTE (! MULTIBYTE_IS_FORMAT_SAFE)
  51. #if DO_MULTIBYTE
  52. # include <wchar.h>
  53. static const mbstate_t mbstate_zero;
  54. #endif
  55. #include <limits.h>
  56. #include <stddef.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <stdbool.h>
  60. #ifdef COMPILE_WIDE
  61. # include <endian.h>
  62. # define CHAR_T wchar_t
  63. # define UCHAR_T unsigned int
  64. # define L_(Str) L##Str
  65. # define NLW(Sym) _NL_W##Sym
  66. # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
  67. # define STRLEN(s) __wcslen (s)
  68. #else
  69. # define CHAR_T char
  70. # define UCHAR_T unsigned char
  71. # define L_(Str) Str
  72. # define NLW(Sym) Sym
  73. # define MEMCPY(d, s, n) memcpy (d, s, n)
  74. # define STRLEN(s) strlen (s)
  75. #endif
  76. /* Shift A right by B bits portably, by dividing A by 2**B and
  77. truncating towards minus infinity. A and B should be free of side
  78. effects, and B should be in the range 0 <= B <= INT_BITS - 2, where
  79. INT_BITS is the number of useful bits in an int. GNU code can
  80. assume that INT_BITS is at least 32.
  81. ISO C99 says that A >> B is implementation-defined if A < 0. Some
  82. implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
  83. right in the usual way when A < 0, so SHR falls back on division if
  84. ordinary A >> B doesn't seem to be the usual signed shift. */
  85. #define SHR(a, b) \
  86. (-1 >> 1 == -1 \
  87. ? (a) >> (b) \
  88. : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
  89. /* Bound on length of the string representing an integer type or expression T.
  90. Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
  91. add 1 for integer division truncation; add 1 more for a minus sign
  92. if needed. */
  93. #define INT_STRLEN_BOUND(t) \
  94. ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
  95. #define TM_YEAR_BASE 1900
  96. #ifndef __isleap
  97. /* Nonzero if YEAR is a leap year (every 4 years,
  98. except every 100th isn't, and every 400th is). */
  99. # define __isleap(year) \
  100. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  101. #endif
  102. #ifdef _LIBC
  103. # define mktime_z(tz, tm) mktime (tm)
  104. # define tzname __tzname
  105. # define tzset __tzset
  106. #endif
  107. #ifndef FPRINTFTIME
  108. # define FPRINTFTIME 0
  109. #endif
  110. #if FPRINTFTIME
  111. # define STREAM_OR_CHAR_T FILE
  112. # define STRFTIME_ARG(x) /* empty */
  113. #else
  114. # define STREAM_OR_CHAR_T CHAR_T
  115. # define STRFTIME_ARG(x) x,
  116. #endif
  117. #if FPRINTFTIME
  118. # define memset_byte(P, Len, Byte) \
  119. do { size_t _i; for (_i = 0; _i < Len; _i++) fputc (Byte, P); } while (0)
  120. # define memset_space(P, Len) memset_byte (P, Len, ' ')
  121. # define memset_zero(P, Len) memset_byte (P, Len, '0')
  122. #elif defined COMPILE_WIDE
  123. # define memset_space(P, Len) (wmemset (P, L' ', Len), (P) += (Len))
  124. # define memset_zero(P, Len) (wmemset (P, L'0', Len), (P) += (Len))
  125. #else
  126. # define memset_space(P, Len) (memset (P, ' ', Len), (P) += (Len))
  127. # define memset_zero(P, Len) (memset (P, '0', Len), (P) += (Len))
  128. #endif
  129. #if FPRINTFTIME
  130. # define advance(P, N)
  131. #else
  132. # define advance(P, N) ((P) += (N))
  133. #endif
  134. #define add(n, f) \
  135. do \
  136. { \
  137. size_t _n = (n); \
  138. size_t _w = (width < 0 ? 0 : width); \
  139. size_t _incr = _n < _w ? _w : _n; \
  140. if (_incr >= maxsize - i) \
  141. return 0; \
  142. if (p) \
  143. { \
  144. if (digits == 0 && _n < _w) \
  145. { \
  146. size_t _delta = width - _n; \
  147. if (pad == L_('0')) \
  148. memset_zero (p, _delta); \
  149. else \
  150. memset_space (p, _delta); \
  151. } \
  152. f; \
  153. advance (p, _n); \
  154. } \
  155. i += _incr; \
  156. } while (0)
  157. #if FPRINTFTIME
  158. # define add1(C) add (1, fputc (C, p))
  159. #else
  160. # define add1(C) add (1, *p = C)
  161. #endif
  162. #if FPRINTFTIME
  163. # define cpy(n, s) \
  164. add ((n), \
  165. do \
  166. { \
  167. if (to_lowcase) \
  168. fwrite_lowcase (p, (s), _n); \
  169. else if (to_uppcase) \
  170. fwrite_uppcase (p, (s), _n); \
  171. else \
  172. { \
  173. /* Ignore the value of fwrite. The caller can determine whether \
  174. an error occurred by inspecting ferror (P). All known fwrite \
  175. implementations set the stream's error indicator when they \
  176. fail due to ENOMEM etc., even though C11 and POSIX.1-2008 do \
  177. not require this. */ \
  178. fwrite (s, _n, 1, p); \
  179. } \
  180. } \
  181. while (0) \
  182. )
  183. #else
  184. # define cpy(n, s) \
  185. add ((n), \
  186. if (to_lowcase) \
  187. memcpy_lowcase (p, (s), _n LOCALE_ARG); \
  188. else if (to_uppcase) \
  189. memcpy_uppcase (p, (s), _n LOCALE_ARG); \
  190. else \
  191. MEMCPY ((void *) p, (void const *) (s), _n))
  192. #endif
  193. #ifdef COMPILE_WIDE
  194. # ifndef USE_IN_EXTENDED_LOCALE_MODEL
  195. # undef __mbsrtowcs_l
  196. # define __mbsrtowcs_l(d, s, l, st, loc) __mbsrtowcs (d, s, l, st)
  197. # endif
  198. # define widen(os, ws, l) \
  199. { \
  200. mbstate_t __st; \
  201. const char *__s = os; \
  202. memset (&__st, '\0', sizeof (__st)); \
  203. l = __mbsrtowcs_l (NULL, &__s, 0, &__st, loc); \
  204. ws = (wchar_t *) alloca ((l + 1) * sizeof (wchar_t)); \
  205. (void) __mbsrtowcs_l (ws, &__s, l, &__st, loc); \
  206. }
  207. #endif
  208. #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
  209. /* We use this code also for the extended locale handling where the
  210. function gets as an additional argument the locale which has to be
  211. used. To access the values we have to redefine the _NL_CURRENT
  212. macro. */
  213. # define strftime __strftime_l
  214. # define wcsftime __wcsftime_l
  215. # undef _NL_CURRENT
  216. # define _NL_CURRENT(category, item) \
  217. (current->values[_NL_ITEM_INDEX (item)].string)
  218. # define LOCALE_PARAM , __locale_t loc
  219. # define LOCALE_ARG , loc
  220. # define HELPER_LOCALE_ARG , current
  221. #else
  222. # define LOCALE_PARAM
  223. # define LOCALE_ARG
  224. # ifdef _LIBC
  225. # define HELPER_LOCALE_ARG , _NL_CURRENT_DATA (LC_TIME)
  226. # else
  227. # define HELPER_LOCALE_ARG
  228. # endif
  229. #endif
  230. #ifdef COMPILE_WIDE
  231. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  232. # define TOUPPER(Ch, L) __towupper_l (Ch, L)
  233. # define TOLOWER(Ch, L) __towlower_l (Ch, L)
  234. # else
  235. # define TOUPPER(Ch, L) towupper (Ch)
  236. # define TOLOWER(Ch, L) towlower (Ch)
  237. # endif
  238. #else
  239. # ifdef USE_IN_EXTENDED_LOCALE_MODEL
  240. # define TOUPPER(Ch, L) __toupper_l (Ch, L)
  241. # define TOLOWER(Ch, L) __tolower_l (Ch, L)
  242. # else
  243. # define TOUPPER(Ch, L) toupper (Ch)
  244. # define TOLOWER(Ch, L) tolower (Ch)
  245. # endif
  246. #endif
  247. /* We don't use 'isdigit' here since the locale dependent
  248. interpretation is not what we want here. We only need to accept
  249. the arabic digits in the ASCII range. One day there is perhaps a
  250. more reliable way to accept other sets of digits. */
  251. #define ISDIGIT(Ch) ((unsigned int) (Ch) - L_('0') <= 9)
  252. #if FPRINTFTIME
  253. static void
  254. fwrite_lowcase (FILE *fp, const CHAR_T *src, size_t len)
  255. {
  256. while (len-- > 0)
  257. {
  258. fputc (TOLOWER ((UCHAR_T) *src, loc), fp);
  259. ++src;
  260. }
  261. }
  262. static void
  263. fwrite_uppcase (FILE *fp, const CHAR_T *src, size_t len)
  264. {
  265. while (len-- > 0)
  266. {
  267. fputc (TOUPPER ((UCHAR_T) *src, loc), fp);
  268. ++src;
  269. }
  270. }
  271. #else
  272. static CHAR_T *memcpy_lowcase (CHAR_T *dest, const CHAR_T *src,
  273. size_t len LOCALE_PARAM);
  274. static CHAR_T *
  275. memcpy_lowcase (CHAR_T *dest, const CHAR_T *src, size_t len LOCALE_PARAM)
  276. {
  277. while (len-- > 0)
  278. dest[len] = TOLOWER ((UCHAR_T) src[len], loc);
  279. return dest;
  280. }
  281. static CHAR_T *memcpy_uppcase (CHAR_T *dest, const CHAR_T *src,
  282. size_t len LOCALE_PARAM);
  283. static CHAR_T *
  284. memcpy_uppcase (CHAR_T *dest, const CHAR_T *src, size_t len LOCALE_PARAM)
  285. {
  286. while (len-- > 0)
  287. dest[len] = TOUPPER ((UCHAR_T) src[len], loc);
  288. return dest;
  289. }
  290. #endif
  291. #if ! HAVE_TM_GMTOFF
  292. /* Yield the difference between *A and *B,
  293. measured in seconds, ignoring leap seconds. */
  294. # define tm_diff ftime_tm_diff
  295. static int tm_diff (const struct tm *, const struct tm *);
  296. static int
  297. tm_diff (const struct tm *a, const struct tm *b)
  298. {
  299. /* Compute intervening leap days correctly even if year is negative.
  300. Take care to avoid int overflow in leap day calculations,
  301. but it's OK to assume that A and B are close to each other. */
  302. int a4 = SHR (a->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (a->tm_year & 3);
  303. int b4 = SHR (b->tm_year, 2) + SHR (TM_YEAR_BASE, 2) - ! (b->tm_year & 3);
  304. int a100 = a4 / 25 - (a4 % 25 < 0);
  305. int b100 = b4 / 25 - (b4 % 25 < 0);
  306. int a400 = SHR (a100, 2);
  307. int b400 = SHR (b100, 2);
  308. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  309. int years = a->tm_year - b->tm_year;
  310. int days = (365 * years + intervening_leap_days
  311. + (a->tm_yday - b->tm_yday));
  312. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  313. + (a->tm_min - b->tm_min))
  314. + (a->tm_sec - b->tm_sec));
  315. }
  316. #endif /* ! HAVE_TM_GMTOFF */
  317. /* The number of days from the first day of the first ISO week of this
  318. year to the year day YDAY with week day WDAY. ISO weeks start on
  319. Monday; the first ISO week has the year's first Thursday. YDAY may
  320. be as small as YDAY_MINIMUM. */
  321. #define ISO_WEEK_START_WDAY 1 /* Monday */
  322. #define ISO_WEEK1_WDAY 4 /* Thursday */
  323. #define YDAY_MINIMUM (-366)
  324. static int iso_week_days (int, int);
  325. #ifdef __GNUC__
  326. __inline__
  327. #endif
  328. static int
  329. iso_week_days (int yday, int wday)
  330. {
  331. /* Add enough to the first operand of % to make it nonnegative. */
  332. int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
  333. return (yday
  334. - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7
  335. + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);
  336. }
  337. /* When compiling this file, GNU applications can #define my_strftime
  338. to a symbol (typically nstrftime) to get an extended strftime with
  339. extra arguments TZ and NS. */
  340. #if FPRINTFTIME
  341. # undef my_strftime
  342. # define my_strftime fprintftime
  343. #endif
  344. #ifdef my_strftime
  345. # undef HAVE_TZSET
  346. # define extra_args , tz, ns
  347. # define extra_args_spec , timezone_t tz, int ns
  348. #else
  349. # if defined COMPILE_WIDE
  350. # define my_strftime wcsftime
  351. # define nl_get_alt_digit _nl_get_walt_digit
  352. # else
  353. # define my_strftime strftime
  354. # define nl_get_alt_digit _nl_get_alt_digit
  355. # endif
  356. # define extra_args
  357. # define extra_args_spec
  358. /* We don't have this information in general. */
  359. # define tz 1
  360. # define ns 0
  361. #endif
  362. static size_t __strftime_internal (STREAM_OR_CHAR_T *, STRFTIME_ARG (size_t)
  363. const CHAR_T *, const struct tm *,
  364. bool, bool *
  365. extra_args_spec LOCALE_PARAM);
  366. /* Write information from TP into S according to the format
  367. string FORMAT, writing no more that MAXSIZE characters
  368. (including the terminating '\0') and returning number of
  369. characters written. If S is NULL, nothing will be written
  370. anywhere, so to determine how many characters would be
  371. written, use NULL for S and (size_t) -1 for MAXSIZE. */
  372. size_t
  373. my_strftime (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
  374. const CHAR_T *format,
  375. const struct tm *tp extra_args_spec LOCALE_PARAM)
  376. {
  377. bool tzset_called = false;
  378. return __strftime_internal (s, STRFTIME_ARG (maxsize) format, tp,
  379. false, &tzset_called extra_args LOCALE_ARG);
  380. }
  381. #if defined _LIBC && ! FPRINTFTIME
  382. libc_hidden_def (my_strftime)
  383. #endif
  384. /* Just like my_strftime, above, but with two more parameters.
  385. UPCASE indicate that the result should be converted to upper case,
  386. and *TZSET_CALLED indicates whether tzset has been called here. */
  387. static size_t
  388. __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
  389. const CHAR_T *format,
  390. const struct tm *tp, bool upcase, bool *tzset_called
  391. extra_args_spec LOCALE_PARAM)
  392. {
  393. #if defined _LIBC && defined USE_IN_EXTENDED_LOCALE_MODEL
  394. struct __locale_data *const current = loc->__locales[LC_TIME];
  395. #endif
  396. #if FPRINTFTIME
  397. size_t maxsize = (size_t) -1;
  398. #endif
  399. int hour12 = tp->tm_hour;
  400. #ifdef _NL_CURRENT
  401. /* We cannot make the following values variables since we must delay
  402. the evaluation of these values until really needed since some
  403. expressions might not be valid in every situation. The 'struct tm'
  404. might be generated by a strptime() call that initialized
  405. only a few elements. Dereference the pointers only if the format
  406. requires this. Then it is ok to fail if the pointers are invalid. */
  407. # define a_wkday \
  408. ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 \
  409. ? "?" : _NL_CURRENT (LC_TIME, NLW(ABDAY_1) + tp->tm_wday)))
  410. # define f_wkday \
  411. ((const CHAR_T *) (tp->tm_wday < 0 || tp->tm_wday > 6 \
  412. ? "?" : _NL_CURRENT (LC_TIME, NLW(DAY_1) + tp->tm_wday)))
  413. # define a_month \
  414. ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 \
  415. ? "?" : _NL_CURRENT (LC_TIME, NLW(ABMON_1) + tp->tm_mon)))
  416. # define f_month \
  417. ((const CHAR_T *) (tp->tm_mon < 0 || tp->tm_mon > 11 \
  418. ? "?" : _NL_CURRENT (LC_TIME, NLW(MON_1) + tp->tm_mon)))
  419. # define ampm \
  420. ((const CHAR_T *) _NL_CURRENT (LC_TIME, tp->tm_hour > 11 \
  421. ? NLW(PM_STR) : NLW(AM_STR)))
  422. # define aw_len STRLEN (a_wkday)
  423. # define am_len STRLEN (a_month)
  424. # define ap_len STRLEN (ampm)
  425. #endif
  426. #if HAVE_TZNAME
  427. char **tzname_vec = tzname;
  428. #endif
  429. const char *zone;
  430. size_t i = 0;
  431. STREAM_OR_CHAR_T *p = s;
  432. const CHAR_T *f;
  433. #if DO_MULTIBYTE && !defined COMPILE_WIDE
  434. const char *format_end = NULL;
  435. #endif
  436. #if ! defined _LIBC && ! HAVE_RUN_TZSET_TEST
  437. /* Solaris 2.5.x and 2.6 tzset sometimes modify the storage returned
  438. by localtime. On such systems, we must either use the tzset and
  439. localtime wrappers to work around the bug (which sets
  440. HAVE_RUN_TZSET_TEST) or make a copy of the structure. */
  441. struct tm copy = *tp;
  442. tp = &copy;
  443. #endif
  444. zone = NULL;
  445. #if HAVE_TM_ZONE
  446. /* The POSIX test suite assumes that setting
  447. the environment variable TZ to a new value before calling strftime()
  448. will influence the result (the %Z format) even if the information in
  449. TP is computed with a totally different time zone.
  450. This is bogus: though POSIX allows bad behavior like this,
  451. POSIX does not require it. Do the right thing instead. */
  452. zone = (const char *) tp->tm_zone;
  453. #endif
  454. #if HAVE_TZNAME
  455. if (!tz)
  456. {
  457. if (! (zone && *zone))
  458. zone = "GMT";
  459. }
  460. else
  461. {
  462. # if !HAVE_TM_ZONE
  463. /* Infer the zone name from *TZ instead of from TZNAME. */
  464. tzname_vec = tz->tzname_copy;
  465. # endif
  466. }
  467. /* The tzset() call might have changed the value. */
  468. if (!(zone && *zone) && tp->tm_isdst >= 0)
  469. {
  470. /* POSIX.1 requires that local time zone information be used as
  471. though strftime called tzset. */
  472. # if HAVE_TZSET
  473. if (!*tzset_called)
  474. {
  475. tzset ();
  476. *tzset_called = true;
  477. }
  478. # endif
  479. zone = tzname_vec[tp->tm_isdst != 0];
  480. }
  481. #endif
  482. if (! zone)
  483. zone = "";
  484. if (hour12 > 12)
  485. hour12 -= 12;
  486. else
  487. if (hour12 == 0)
  488. hour12 = 12;
  489. for (f = format; *f != '\0'; ++f)
  490. {
  491. int pad = 0; /* Padding for number ('-', '_', or 0). */
  492. int modifier; /* Field modifier ('E', 'O', or 0). */
  493. int digits = 0; /* Max digits for numeric format. */
  494. int number_value; /* Numeric value to be printed. */
  495. unsigned int u_number_value; /* (unsigned int) number_value. */
  496. bool negative_number; /* The number is negative. */
  497. bool always_output_a_sign; /* +/- should always be output. */
  498. int tz_colon_mask; /* Bitmask of where ':' should appear. */
  499. const CHAR_T *subfmt;
  500. CHAR_T sign_char;
  501. CHAR_T *bufp;
  502. CHAR_T buf[1
  503. + 2 /* for the two colons in a %::z or %:::z time zone */
  504. + (sizeof (int) < sizeof (time_t)
  505. ? INT_STRLEN_BOUND (time_t)
  506. : INT_STRLEN_BOUND (int))];
  507. int width = -1;
  508. bool to_lowcase = false;
  509. bool to_uppcase = upcase;
  510. size_t colons;
  511. bool change_case = false;
  512. int format_char;
  513. #if DO_MULTIBYTE && !defined COMPILE_WIDE
  514. switch (*f)
  515. {
  516. case L_('%'):
  517. break;
  518. case L_('\b'): case L_('\t'): case L_('\n'):
  519. case L_('\v'): case L_('\f'): case L_('\r'):
  520. case L_(' '): case L_('!'): case L_('"'): case L_('#'): case L_('&'):
  521. case L_('\''): case L_('('): case L_(')'): case L_('*'): case L_('+'):
  522. case L_(','): case L_('-'): case L_('.'): case L_('/'): case L_('0'):
  523. case L_('1'): case L_('2'): case L_('3'): case L_('4'): case L_('5'):
  524. case L_('6'): case L_('7'): case L_('8'): case L_('9'): case L_(':'):
  525. case L_(';'): case L_('<'): case L_('='): case L_('>'): case L_('?'):
  526. case L_('A'): case L_('B'): case L_('C'): case L_('D'): case L_('E'):
  527. case L_('F'): case L_('G'): case L_('H'): case L_('I'): case L_('J'):
  528. case L_('K'): case L_('L'): case L_('M'): case L_('N'): case L_('O'):
  529. case L_('P'): case L_('Q'): case L_('R'): case L_('S'): case L_('T'):
  530. case L_('U'): case L_('V'): case L_('W'): case L_('X'): case L_('Y'):
  531. case L_('Z'): case L_('['): case L_('\\'): case L_(']'): case L_('^'):
  532. case L_('_'): case L_('a'): case L_('b'): case L_('c'): case L_('d'):
  533. case L_('e'): case L_('f'): case L_('g'): case L_('h'): case L_('i'):
  534. case L_('j'): case L_('k'): case L_('l'): case L_('m'): case L_('n'):
  535. case L_('o'): case L_('p'): case L_('q'): case L_('r'): case L_('s'):
  536. case L_('t'): case L_('u'): case L_('v'): case L_('w'): case L_('x'):
  537. case L_('y'): case L_('z'): case L_('{'): case L_('|'): case L_('}'):
  538. case L_('~'):
  539. /* The C Standard requires these 98 characters (plus '%') to
  540. be in the basic execution character set. None of these
  541. characters can start a multibyte sequence, so they need
  542. not be analyzed further. */
  543. add1 (*f);
  544. continue;
  545. default:
  546. /* Copy this multibyte sequence until we reach its end, find
  547. an error, or come back to the initial shift state. */
  548. {
  549. mbstate_t mbstate = mbstate_zero;
  550. size_t len = 0;
  551. size_t fsize;
  552. if (! format_end)
  553. format_end = f + strlen (f) + 1;
  554. fsize = format_end - f;
  555. do
  556. {
  557. size_t bytes = mbrlen (f + len, fsize - len, &mbstate);
  558. if (bytes == 0)
  559. break;
  560. if (bytes == (size_t) -2)
  561. {
  562. len += strlen (f + len);
  563. break;
  564. }
  565. if (bytes == (size_t) -1)
  566. {
  567. len++;
  568. break;
  569. }
  570. len += bytes;
  571. }
  572. while (! mbsinit (&mbstate));
  573. cpy (len, f);
  574. f += len - 1;
  575. continue;
  576. }
  577. }
  578. #else /* ! DO_MULTIBYTE */
  579. /* Either multibyte encodings are not supported, they are
  580. safe for formats, so any non-'%' byte can be copied through,
  581. or this is the wide character version. */
  582. if (*f != L_('%'))
  583. {
  584. add1 (*f);
  585. continue;
  586. }
  587. #endif /* ! DO_MULTIBYTE */
  588. /* Check for flags that can modify a format. */
  589. while (1)
  590. {
  591. switch (*++f)
  592. {
  593. /* This influences the number formats. */
  594. case L_('_'):
  595. case L_('-'):
  596. case L_('0'):
  597. pad = *f;
  598. continue;
  599. /* This changes textual output. */
  600. case L_('^'):
  601. to_uppcase = true;
  602. continue;
  603. case L_('#'):
  604. change_case = true;
  605. continue;
  606. default:
  607. break;
  608. }
  609. break;
  610. }
  611. /* As a GNU extension we allow the field width to be specified. */
  612. if (ISDIGIT (*f))
  613. {
  614. width = 0;
  615. do
  616. {
  617. if (width > INT_MAX / 10
  618. || (width == INT_MAX / 10 && *f - L_('0') > INT_MAX % 10))
  619. /* Avoid overflow. */
  620. width = INT_MAX;
  621. else
  622. {
  623. width *= 10;
  624. width += *f - L_('0');
  625. }
  626. ++f;
  627. }
  628. while (ISDIGIT (*f));
  629. }
  630. /* Check for modifiers. */
  631. switch (*f)
  632. {
  633. case L_('E'):
  634. case L_('O'):
  635. modifier = *f++;
  636. break;
  637. default:
  638. modifier = 0;
  639. break;
  640. }
  641. /* Now do the specified format. */
  642. format_char = *f;
  643. switch (format_char)
  644. {
  645. #define DO_NUMBER(d, v) \
  646. do \
  647. { \
  648. digits = d; \
  649. number_value = v; \
  650. goto do_number; \
  651. } \
  652. while (0)
  653. #define DO_SIGNED_NUMBER(d, negative, v) \
  654. do \
  655. { \
  656. digits = d; \
  657. negative_number = negative; \
  658. u_number_value = v; \
  659. goto do_signed_number; \
  660. } \
  661. while (0)
  662. /* The mask is not what you might think.
  663. When the ordinal i'th bit is set, insert a colon
  664. before the i'th digit of the time zone representation. */
  665. #define DO_TZ_OFFSET(d, mask, v) \
  666. do \
  667. { \
  668. digits = d; \
  669. tz_colon_mask = mask; \
  670. u_number_value = v; \
  671. goto do_tz_offset; \
  672. } \
  673. while (0)
  674. #define DO_NUMBER_SPACEPAD(d, v) \
  675. do \
  676. { \
  677. digits = d; \
  678. number_value = v; \
  679. goto do_number_spacepad; \
  680. } \
  681. while (0)
  682. case L_('%'):
  683. if (modifier != 0)
  684. goto bad_format;
  685. add1 (*f);
  686. break;
  687. case L_('a'):
  688. if (modifier != 0)
  689. goto bad_format;
  690. if (change_case)
  691. {
  692. to_uppcase = true;
  693. to_lowcase = false;
  694. }
  695. #ifdef _NL_CURRENT
  696. cpy (aw_len, a_wkday);
  697. break;
  698. #else
  699. goto underlying_strftime;
  700. #endif
  701. case 'A':
  702. if (modifier != 0)
  703. goto bad_format;
  704. if (change_case)
  705. {
  706. to_uppcase = true;
  707. to_lowcase = false;
  708. }
  709. #ifdef _NL_CURRENT
  710. cpy (STRLEN (f_wkday), f_wkday);
  711. break;
  712. #else
  713. goto underlying_strftime;
  714. #endif
  715. case L_('b'):
  716. case L_('h'):
  717. if (change_case)
  718. {
  719. to_uppcase = true;
  720. to_lowcase = false;
  721. }
  722. if (modifier != 0)
  723. goto bad_format;
  724. #ifdef _NL_CURRENT
  725. cpy (am_len, a_month);
  726. break;
  727. #else
  728. goto underlying_strftime;
  729. #endif
  730. case L_('B'):
  731. if (modifier != 0)
  732. goto bad_format;
  733. if (change_case)
  734. {
  735. to_uppcase = true;
  736. to_lowcase = false;
  737. }
  738. #ifdef _NL_CURRENT
  739. cpy (STRLEN (f_month), f_month);
  740. break;
  741. #else
  742. goto underlying_strftime;
  743. #endif
  744. case L_('c'):
  745. if (modifier == L_('O'))
  746. goto bad_format;
  747. #ifdef _NL_CURRENT
  748. if (! (modifier == 'E'
  749. && (*(subfmt =
  750. (const CHAR_T *) _NL_CURRENT (LC_TIME,
  751. NLW(ERA_D_T_FMT)))
  752. != '\0')))
  753. subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_T_FMT));
  754. #else
  755. goto underlying_strftime;
  756. #endif
  757. subformat:
  758. {
  759. size_t len = __strftime_internal (NULL, STRFTIME_ARG ((size_t) -1)
  760. subfmt,
  761. tp, to_uppcase, tzset_called
  762. extra_args LOCALE_ARG);
  763. add (len, __strftime_internal (p,
  764. STRFTIME_ARG (maxsize - i)
  765. subfmt,
  766. tp, to_uppcase, tzset_called
  767. extra_args LOCALE_ARG));
  768. }
  769. break;
  770. #if !(defined _NL_CURRENT && HAVE_STRUCT_ERA_ENTRY)
  771. underlying_strftime:
  772. {
  773. /* The relevant information is available only via the
  774. underlying strftime implementation, so use that. */
  775. char ufmt[5];
  776. char *u = ufmt;
  777. char ubuf[1024]; /* enough for any single format in practice */
  778. size_t len;
  779. /* Make sure we're calling the actual underlying strftime.
  780. In some cases, config.h contains something like
  781. "#define strftime rpl_strftime". */
  782. # ifdef strftime
  783. # undef strftime
  784. size_t strftime ();
  785. # endif
  786. /* The space helps distinguish strftime failure from empty
  787. output. */
  788. *u++ = ' ';
  789. *u++ = '%';
  790. if (modifier != 0)
  791. *u++ = modifier;
  792. *u++ = format_char;
  793. *u = '\0';
  794. len = strftime (ubuf, sizeof ubuf, ufmt, tp);
  795. if (len != 0)
  796. cpy (len - 1, ubuf + 1);
  797. }
  798. break;
  799. #endif
  800. case L_('C'):
  801. if (modifier == L_('E'))
  802. {
  803. #if HAVE_STRUCT_ERA_ENTRY
  804. struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
  805. if (era)
  806. {
  807. # ifdef COMPILE_WIDE
  808. size_t len = __wcslen (era->era_wname);
  809. cpy (len, era->era_wname);
  810. # else
  811. size_t len = strlen (era->era_name);
  812. cpy (len, era->era_name);
  813. # endif
  814. break;
  815. }
  816. #else
  817. goto underlying_strftime;
  818. #endif
  819. }
  820. {
  821. int century = tp->tm_year / 100 + TM_YEAR_BASE / 100;
  822. century -= tp->tm_year % 100 < 0 && 0 < century;
  823. DO_SIGNED_NUMBER (2, tp->tm_year < - TM_YEAR_BASE, century);
  824. }
  825. case L_('x'):
  826. if (modifier == L_('O'))
  827. goto bad_format;
  828. #ifdef _NL_CURRENT
  829. if (! (modifier == L_('E')
  830. && (*(subfmt =
  831. (const CHAR_T *)_NL_CURRENT (LC_TIME, NLW(ERA_D_FMT)))
  832. != L_('\0'))))
  833. subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(D_FMT));
  834. goto subformat;
  835. #else
  836. goto underlying_strftime;
  837. #endif
  838. case L_('D'):
  839. if (modifier != 0)
  840. goto bad_format;
  841. subfmt = L_("%m/%d/%y");
  842. goto subformat;
  843. case L_('d'):
  844. if (modifier == L_('E'))
  845. goto bad_format;
  846. DO_NUMBER (2, tp->tm_mday);
  847. case L_('e'):
  848. if (modifier == L_('E'))
  849. goto bad_format;
  850. DO_NUMBER_SPACEPAD (2, tp->tm_mday);
  851. /* All numeric formats set DIGITS and NUMBER_VALUE (or U_NUMBER_VALUE)
  852. and then jump to one of these labels. */
  853. do_tz_offset:
  854. always_output_a_sign = true;
  855. goto do_number_body;
  856. do_number_spacepad:
  857. /* Force '_' flag unless overridden by '0' or '-' flag. */
  858. if (pad != L_('0') && pad != L_('-'))
  859. pad = L_('_');
  860. do_number:
  861. /* Format NUMBER_VALUE according to the MODIFIER flag. */
  862. negative_number = number_value < 0;
  863. u_number_value = number_value;
  864. do_signed_number:
  865. always_output_a_sign = false;
  866. tz_colon_mask = 0;
  867. do_number_body:
  868. /* Format U_NUMBER_VALUE according to the MODIFIER flag.
  869. NEGATIVE_NUMBER is nonzero if the original number was
  870. negative; in this case it was converted directly to
  871. unsigned int (i.e., modulo (UINT_MAX + 1)) without
  872. negating it. */
  873. if (modifier == L_('O') && !negative_number)
  874. {
  875. #ifdef _NL_CURRENT
  876. /* Get the locale specific alternate representation of
  877. the number. If none exist NULL is returned. */
  878. const CHAR_T *cp = nl_get_alt_digit (u_number_value
  879. HELPER_LOCALE_ARG);
  880. if (cp != NULL)
  881. {
  882. size_t digitlen = STRLEN (cp);
  883. if (digitlen != 0)
  884. {
  885. cpy (digitlen, cp);
  886. break;
  887. }
  888. }
  889. #else
  890. goto underlying_strftime;
  891. #endif
  892. }
  893. bufp = buf + sizeof (buf) / sizeof (buf[0]);
  894. if (negative_number)
  895. u_number_value = - u_number_value;
  896. do
  897. {
  898. if (tz_colon_mask & 1)
  899. *--bufp = ':';
  900. tz_colon_mask >>= 1;
  901. *--bufp = u_number_value % 10 + L_('0');
  902. u_number_value /= 10;
  903. }
  904. while (u_number_value != 0 || tz_colon_mask != 0);
  905. do_number_sign_and_padding:
  906. if (digits < width)
  907. digits = width;
  908. sign_char = (negative_number ? L_('-')
  909. : always_output_a_sign ? L_('+')
  910. : 0);
  911. if (pad == L_('-'))
  912. {
  913. if (sign_char)
  914. add1 (sign_char);
  915. }
  916. else
  917. {
  918. int padding = digits - (buf + (sizeof (buf) / sizeof (buf[0]))
  919. - bufp) - !!sign_char;
  920. if (padding > 0)
  921. {
  922. if (pad == L_('_'))
  923. {
  924. if ((size_t) padding >= maxsize - i)
  925. return 0;
  926. if (p)
  927. memset_space (p, padding);
  928. i += padding;
  929. width = width > padding ? width - padding : 0;
  930. if (sign_char)
  931. add1 (sign_char);
  932. }
  933. else
  934. {
  935. if ((size_t) digits >= maxsize - i)
  936. return 0;
  937. if (sign_char)
  938. add1 (sign_char);
  939. if (p)
  940. memset_zero (p, padding);
  941. i += padding;
  942. width = 0;
  943. }
  944. }
  945. else
  946. {
  947. if (sign_char)
  948. add1 (sign_char);
  949. }
  950. }
  951. cpy (buf + sizeof (buf) / sizeof (buf[0]) - bufp, bufp);
  952. break;
  953. case L_('F'):
  954. if (modifier != 0)
  955. goto bad_format;
  956. subfmt = L_("%Y-%m-%d");
  957. goto subformat;
  958. case L_('H'):
  959. if (modifier == L_('E'))
  960. goto bad_format;
  961. DO_NUMBER (2, tp->tm_hour);
  962. case L_('I'):
  963. if (modifier == L_('E'))
  964. goto bad_format;
  965. DO_NUMBER (2, hour12);
  966. case L_('k'): /* GNU extension. */
  967. if (modifier == L_('E'))
  968. goto bad_format;
  969. DO_NUMBER_SPACEPAD (2, tp->tm_hour);
  970. case L_('l'): /* GNU extension. */
  971. if (modifier == L_('E'))
  972. goto bad_format;
  973. DO_NUMBER_SPACEPAD (2, hour12);
  974. case L_('j'):
  975. if (modifier == L_('E'))
  976. goto bad_format;
  977. DO_SIGNED_NUMBER (3, tp->tm_yday < -1, tp->tm_yday + 1U);
  978. case L_('M'):
  979. if (modifier == L_('E'))
  980. goto bad_format;
  981. DO_NUMBER (2, tp->tm_min);
  982. case L_('m'):
  983. if (modifier == L_('E'))
  984. goto bad_format;
  985. DO_SIGNED_NUMBER (2, tp->tm_mon < -1, tp->tm_mon + 1U);
  986. #ifndef _LIBC
  987. case L_('N'): /* GNU extension. */
  988. if (modifier == L_('E'))
  989. goto bad_format;
  990. number_value = ns;
  991. if (width == -1)
  992. width = 9;
  993. else
  994. {
  995. /* Take an explicit width less than 9 as a precision. */
  996. int j;
  997. for (j = width; j < 9; j++)
  998. number_value /= 10;
  999. }
  1000. DO_NUMBER (width, number_value);
  1001. #endif
  1002. case L_('n'):
  1003. add1 (L_('\n'));
  1004. break;
  1005. case L_('P'):
  1006. to_lowcase = true;
  1007. #ifndef _NL_CURRENT
  1008. format_char = L_('p');
  1009. #endif
  1010. /* FALLTHROUGH */
  1011. case L_('p'):
  1012. if (change_case)
  1013. {
  1014. to_uppcase = false;
  1015. to_lowcase = true;
  1016. }
  1017. #ifdef _NL_CURRENT
  1018. cpy (ap_len, ampm);
  1019. break;
  1020. #else
  1021. goto underlying_strftime;
  1022. #endif
  1023. case L_('q'): /* GNU extension. */
  1024. DO_SIGNED_NUMBER (1, false, ((tp->tm_mon * 11) >> 5) + 1);
  1025. break;
  1026. case L_('R'):
  1027. subfmt = L_("%H:%M");
  1028. goto subformat;
  1029. case L_('r'):
  1030. #ifdef _NL_CURRENT
  1031. if (*(subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME,
  1032. NLW(T_FMT_AMPM)))
  1033. == L_('\0'))
  1034. subfmt = L_("%I:%M:%S %p");
  1035. goto subformat;
  1036. #else
  1037. goto underlying_strftime;
  1038. #endif
  1039. case L_('S'):
  1040. if (modifier == L_('E'))
  1041. goto bad_format;
  1042. DO_NUMBER (2, tp->tm_sec);
  1043. case L_('s'): /* GNU extension. */
  1044. {
  1045. struct tm ltm;
  1046. time_t t;
  1047. ltm = *tp;
  1048. t = mktime_z (tz, &ltm);
  1049. /* Generate string value for T using time_t arithmetic;
  1050. this works even if sizeof (long) < sizeof (time_t). */
  1051. bufp = buf + sizeof (buf) / sizeof (buf[0]);
  1052. negative_number = t < 0;
  1053. do
  1054. {
  1055. int d = t % 10;
  1056. t /= 10;
  1057. *--bufp = (negative_number ? -d : d) + L_('0');
  1058. }
  1059. while (t != 0);
  1060. digits = 1;
  1061. always_output_a_sign = false;
  1062. goto do_number_sign_and_padding;
  1063. }
  1064. case L_('X'):
  1065. if (modifier == L_('O'))
  1066. goto bad_format;
  1067. #ifdef _NL_CURRENT
  1068. if (! (modifier == L_('E')
  1069. && (*(subfmt =
  1070. (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(ERA_T_FMT)))
  1071. != L_('\0'))))
  1072. subfmt = (const CHAR_T *) _NL_CURRENT (LC_TIME, NLW(T_FMT));
  1073. goto subformat;
  1074. #else
  1075. goto underlying_strftime;
  1076. #endif
  1077. case L_('T'):
  1078. subfmt = L_("%H:%M:%S");
  1079. goto subformat;
  1080. case L_('t'):
  1081. add1 (L_('\t'));
  1082. break;
  1083. case L_('u'):
  1084. DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1);
  1085. case L_('U'):
  1086. if (modifier == L_('E'))
  1087. goto bad_format;
  1088. DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7);
  1089. case L_('V'):
  1090. case L_('g'):
  1091. case L_('G'):
  1092. if (modifier == L_('E'))
  1093. goto bad_format;
  1094. {
  1095. /* YEAR is a leap year if and only if (tp->tm_year + TM_YEAR_BASE)
  1096. is a leap year, except that YEAR and YEAR - 1 both work
  1097. correctly even when (tp->tm_year + TM_YEAR_BASE) would
  1098. overflow. */
  1099. int year = (tp->tm_year
  1100. + (tp->tm_year < 0
  1101. ? TM_YEAR_BASE % 400
  1102. : TM_YEAR_BASE % 400 - 400));
  1103. int year_adjust = 0;
  1104. int days = iso_week_days (tp->tm_yday, tp->tm_wday);
  1105. if (days < 0)
  1106. {
  1107. /* This ISO week belongs to the previous year. */
  1108. year_adjust = -1;
  1109. days = iso_week_days (tp->tm_yday + (365 + __isleap (year - 1)),
  1110. tp->tm_wday);
  1111. }
  1112. else
  1113. {
  1114. int d = iso_week_days (tp->tm_yday - (365 + __isleap (year)),
  1115. tp->tm_wday);
  1116. if (0 <= d)
  1117. {
  1118. /* This ISO week belongs to the next year. */
  1119. year_adjust = 1;
  1120. days = d;
  1121. }
  1122. }
  1123. switch (*f)
  1124. {
  1125. case L_('g'):
  1126. {
  1127. int yy = (tp->tm_year % 100 + year_adjust) % 100;
  1128. DO_NUMBER (2, (0 <= yy
  1129. ? yy
  1130. : tp->tm_year < -TM_YEAR_BASE - year_adjust
  1131. ? -yy
  1132. : yy + 100));
  1133. }
  1134. case L_('G'):
  1135. DO_SIGNED_NUMBER (4, tp->tm_year < -TM_YEAR_BASE - year_adjust,
  1136. (tp->tm_year + (unsigned int) TM_YEAR_BASE
  1137. + year_adjust));
  1138. default:
  1139. DO_NUMBER (2, days / 7 + 1);
  1140. }
  1141. }
  1142. case L_('W'):
  1143. if (modifier == L_('E'))
  1144. goto bad_format;
  1145. DO_NUMBER (2, (tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7);
  1146. case L_('w'):
  1147. if (modifier == L_('E'))
  1148. goto bad_format;
  1149. DO_NUMBER (1, tp->tm_wday);
  1150. case L_('Y'):
  1151. if (modifier == 'E')
  1152. {
  1153. #if HAVE_STRUCT_ERA_ENTRY
  1154. struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
  1155. if (era)
  1156. {
  1157. # ifdef COMPILE_WIDE
  1158. subfmt = era->era_wformat;
  1159. # else
  1160. subfmt = era->era_format;
  1161. # endif
  1162. goto subformat;
  1163. }
  1164. #else
  1165. goto underlying_strftime;
  1166. #endif
  1167. }
  1168. if (modifier == L_('O'))
  1169. goto bad_format;
  1170. DO_SIGNED_NUMBER (4, tp->tm_year < -TM_YEAR_BASE,
  1171. tp->tm_year + (unsigned int) TM_YEAR_BASE);
  1172. case L_('y'):
  1173. if (modifier == L_('E'))
  1174. {
  1175. #if HAVE_STRUCT_ERA_ENTRY
  1176. struct era_entry *era = _nl_get_era_entry (tp HELPER_LOCALE_ARG);
  1177. if (era)
  1178. {
  1179. int delta = tp->tm_year - era->start_date[0];
  1180. DO_NUMBER (1, (era->offset
  1181. + delta * era->absolute_direction));
  1182. }
  1183. #else
  1184. goto underlying_strftime;
  1185. #endif
  1186. }
  1187. {
  1188. int yy = tp->tm_year % 100;
  1189. if (yy < 0)
  1190. yy = tp->tm_year < - TM_YEAR_BASE ? -yy : yy + 100;
  1191. DO_NUMBER (2, yy);
  1192. }
  1193. case L_('Z'):
  1194. if (change_case)
  1195. {
  1196. to_uppcase = false;
  1197. to_lowcase = true;
  1198. }
  1199. #ifdef COMPILE_WIDE
  1200. {
  1201. /* The zone string is always given in multibyte form. We have
  1202. to transform it first. */
  1203. wchar_t *wczone;
  1204. size_t len;
  1205. widen (zone, wczone, len);
  1206. cpy (len, wczone);
  1207. }
  1208. #else
  1209. cpy (strlen (zone), zone);
  1210. #endif
  1211. break;
  1212. case L_(':'):
  1213. /* :, ::, and ::: are valid only just before 'z'.
  1214. :::: etc. are rejected later. */
  1215. for (colons = 1; f[colons] == L_(':'); colons++)
  1216. continue;
  1217. if (f[colons] != L_('z'))
  1218. goto bad_format;
  1219. f += colons;
  1220. goto do_z_conversion;
  1221. case L_('z'):
  1222. colons = 0;
  1223. do_z_conversion:
  1224. if (tp->tm_isdst < 0)
  1225. break;
  1226. {
  1227. int diff;
  1228. int hour_diff;
  1229. int min_diff;
  1230. int sec_diff;
  1231. #if HAVE_TM_GMTOFF
  1232. diff = tp->tm_gmtoff;
  1233. #else
  1234. if (!tz)
  1235. diff = 0;
  1236. else
  1237. {
  1238. struct tm gtm;
  1239. struct tm ltm;
  1240. time_t lt;
  1241. /* POSIX.1 requires that local time zone information be used as
  1242. though strftime called tzset. */
  1243. # if HAVE_TZSET
  1244. if (!*tzset_called)
  1245. {
  1246. tzset ();
  1247. *tzset_called = true;
  1248. }
  1249. # endif
  1250. ltm = *tp;
  1251. lt = mktime_z (tz, &ltm);
  1252. if (lt == (time_t) -1)
  1253. {
  1254. /* mktime returns -1 for errors, but -1 is also a
  1255. valid time_t value. Check whether an error really
  1256. occurred. */
  1257. struct tm tm;
  1258. if (! localtime_rz (tz, &lt, &tm)
  1259. || ((ltm.tm_sec ^ tm.tm_sec)
  1260. | (ltm.tm_min ^ tm.tm_min)
  1261. | (ltm.tm_hour ^ tm.tm_hour)
  1262. | (ltm.tm_mday ^ tm.tm_mday)
  1263. | (ltm.tm_mon ^ tm.tm_mon)
  1264. | (ltm.tm_year ^ tm.tm_year)))
  1265. break;
  1266. }
  1267. if (! localtime_rz (0, &lt, &gtm))
  1268. break;
  1269. diff = tm_diff (&ltm, &gtm);
  1270. }
  1271. #endif
  1272. negative_number = diff < 0 || (diff == 0 && *zone == '-');
  1273. hour_diff = diff / 60 / 60;
  1274. min_diff = diff / 60 % 60;
  1275. sec_diff = diff % 60;
  1276. switch (colons)
  1277. {
  1278. case 0: /* +hhmm */
  1279. DO_TZ_OFFSET (5, 0, hour_diff * 100 + min_diff);
  1280. case 1: tz_hh_mm: /* +hh:mm */
  1281. DO_TZ_OFFSET (6, 04, hour_diff * 100 + min_diff);
  1282. case 2: tz_hh_mm_ss: /* +hh:mm:ss */
  1283. DO_TZ_OFFSET (9, 024,
  1284. hour_diff * 10000 + min_diff * 100 + sec_diff);
  1285. case 3: /* +hh if possible, else +hh:mm, else +hh:mm:ss */
  1286. if (sec_diff != 0)
  1287. goto tz_hh_mm_ss;
  1288. if (min_diff != 0)
  1289. goto tz_hh_mm;
  1290. DO_TZ_OFFSET (3, 0, hour_diff);
  1291. default:
  1292. goto bad_format;
  1293. }
  1294. }
  1295. case L_('\0'): /* GNU extension: % at end of format. */
  1296. --f;
  1297. /* Fall through. */
  1298. default:
  1299. /* Unknown format; output the format, including the '%',
  1300. since this is most likely the right thing to do if a
  1301. multibyte string has been misparsed. */
  1302. bad_format:
  1303. {
  1304. int flen;
  1305. for (flen = 1; f[1 - flen] != L_('%'); flen++)
  1306. continue;
  1307. cpy (flen, &f[1 - flen]);
  1308. }
  1309. break;
  1310. }
  1311. }
  1312. #if ! FPRINTFTIME
  1313. if (p && maxsize != 0)
  1314. *p = L_('\0');
  1315. #endif
  1316. return i;
  1317. }