nstrftime.c 48 KB

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