strftime.c 46 KB

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