nstrftime.c 48 KB

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