nstrftime.c 48 KB

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