nstrftime.c 48 KB

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