strftime.c 46 KB

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