gnu_strftime.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #ifdef _LIBC
  19. # define HAVE_LIMITS_H 1
  20. # define HAVE_MBLEN 1
  21. # define HAVE_MBRLEN 1
  22. # define HAVE_STRUCT_ERA_ENTRY 1
  23. # define HAVE_TM_GMTOFF 1
  24. # define HAVE_TM_ZONE 1
  25. # define HAVE_TZNAME 1
  26. # define HAVE_TZSET 1
  27. # define MULTIBYTE_IS_FORMAT_SAFE 1
  28. # define STDC_HEADERS 1
  29. # include "../locale/localeinfo.h"
  30. #endif
  31. #if defined emacs && !defined HAVE_BCOPY
  32. # define HAVE_MEMCPY 1
  33. #endif
  34. #include <ctype.h>
  35. #include <sys/types.h> /* Some systems define `time_t' here. */
  36. #ifdef TIME_WITH_SYS_TIME
  37. # include <sys/time.h>
  38. # include <time.h>
  39. #else
  40. # ifdef HAVE_SYS_TIME_H
  41. # include <sys/time.h>
  42. # else
  43. # include <time.h>
  44. # endif
  45. #endif
  46. #if HAVE_TZNAME
  47. extern char *tzname[];
  48. #endif
  49. /* Do multibyte processing if multibytes are supported, unless
  50. multibyte sequences are safe in formats. Multibyte sequences are
  51. safe if they cannot contain byte sequences that look like format
  52. conversion specifications. The GNU C Library uses UTF8 multibyte
  53. encoding, which is safe for formats, but strftime.c can be used
  54. with other C libraries that use unsafe encodings. */
  55. #define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_FORMAT_SAFE)
  56. #if DO_MULTIBYTE
  57. # if HAVE_MBRLEN
  58. # include <wchar.h>
  59. # else
  60. /* Simulate mbrlen with mblen as best we can. */
  61. # define mbstate_t int
  62. # define mbrlen(s, n, ps) mblen (s, n)
  63. # define mbsinit(ps) (*(ps) == 0)
  64. # endif
  65. static const mbstate_t mbstate_zero;
  66. #endif
  67. #if HAVE_LIMITS_H
  68. # include <limits.h>
  69. #endif
  70. #if STDC_HEADERS
  71. # include <stddef.h>
  72. # include <stdlib.h>
  73. # include <string.h>
  74. #else
  75. # ifndef HAVE_MEMCPY
  76. # define memcpy(d, s, n) bcopy ((s), (d), (n))
  77. # endif
  78. #endif
  79. #ifdef _LIBC
  80. # define MEMPCPY(d, s, n) __mempcpy (d, s, n)
  81. #else
  82. # ifndef HAVE_MEMPCPY
  83. # define MEMPCPY(d, s, n) ((void *) ((char *) memcpy (d, s, n) + (n)))
  84. # endif
  85. #endif
  86. #ifndef __P
  87. # if defined (__GNUC__) || (defined (__STDC__) && __STDC__)
  88. # define __P(args) args
  89. # else
  90. # define __P(args) ()
  91. # endif /* GCC. */
  92. #endif /* Not __P. */
  93. #ifndef PTR
  94. # ifdef __STDC__
  95. # define PTR void *
  96. # else
  97. # define PTR char *
  98. # endif
  99. #endif
  100. #ifndef CHAR_BIT
  101. # define CHAR_BIT 8
  102. #endif
  103. #ifndef NULL
  104. # define NULL 0
  105. #endif
  106. #define TYPE_SIGNED(t) ((t) -1 < 0)
  107. /* Bound on length of the string representing an integer value of type t.
  108. Subtract one for the sign bit if t is signed;
  109. 302 / 1000 is log10 (2) rounded up;
  110. add one for integer division truncation;
  111. add one more for a minus sign if t is signed. */
  112. #define INT_STRLEN_BOUND(t) \
  113. ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 + 1 + TYPE_SIGNED (t))
  114. #define TM_YEAR_BASE 1900
  115. #ifndef __isleap
  116. /* Nonzero if YEAR is a leap year (every 4 years,
  117. except every 100th isn't, and every 400th is). */
  118. # define __isleap(year) \
  119. ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  120. #endif
  121. #ifdef _LIBC
  122. # define my_strftime_gmtime_r __gmtime_r
  123. # define my_strftime_localtime_r __localtime_r
  124. # define tzname __tzname
  125. # define tzset __tzset
  126. #else
  127. /* If we're a strftime substitute in a GNU program, then prefer gmtime
  128. to gmtime_r, since many gmtime_r implementations are buggy.
  129. Similarly for localtime_r. */
  130. # if ! HAVE_TM_GMTOFF
  131. //static struct tm *my_strftime_gmtime_r __P ((const time_t *, struct tm *));
  132. static struct tm *
  133. my_strftime_gmtime_r (const time_t *t, struct tm *tp)
  134. {
  135. struct tm *l = gmtime (t);
  136. if (! l)
  137. return 0;
  138. *tp = *l;
  139. return tp;
  140. }
  141. # endif /* ! HAVE_TM_GMTOFF */
  142. //static struct tm *my_strftime_localtime_r __P ((const time_t *, struct tm *));
  143. static struct tm *
  144. my_strftime_localtime_r (const time_t *t, struct tm *tp)
  145. {
  146. struct tm *l = localtime (t);
  147. if (! l)
  148. return 0;
  149. *tp = *l;
  150. return tp;
  151. }
  152. #endif /* ! defined _LIBC */
  153. #if !defined memset && !defined HAVE_MEMSET && !defined _LIBC
  154. /* Some systems lack the `memset' function and we don't want to
  155. introduce additional dependencies. */
  156. /* The SGI compiler reportedly barfs on the trailing null
  157. if we use a string constant as the initializer. 28 June 1997, rms. */
  158. static const char spaces[16] = /* " " */
  159. { ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
  160. static const char zeroes[16] = /* "0000000000000000" */
  161. { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' };
  162. # define memset_space(P, Len) \
  163. do { \
  164. int _len = (Len); \
  165. \
  166. do \
  167. { \
  168. int _this = _len > 16 ? 16 : _len; \
  169. (P) = MEMPCPY ((P), spaces, _this); \
  170. _len -= _this; \
  171. } \
  172. while (_len > 0); \
  173. } while (0)
  174. # define memset_zero(P, Len) \
  175. do { \
  176. int _len = (Len); \
  177. \
  178. do \
  179. { \
  180. int _this = _len > 16 ? 16 : _len; \
  181. (P) = MEMPCPY ((P), zeroes, _this); \
  182. _len -= _this; \
  183. } \
  184. while (_len > 0); \
  185. } while (0)
  186. #else
  187. # define memset_space(P, Len) (memset ((P), ' ', (Len)), (P) += (Len))
  188. # define memset_zero(P, Len) (memset ((P), '0', (Len)), (P) += (Len))
  189. #endif
  190. #define add(n, f) \
  191. do \
  192. { \
  193. int _n = (n); \
  194. int _delta = width - _n; \
  195. int _incr = _n + (_delta > 0 ? _delta : 0); \
  196. if (i + _incr >= maxsize) \
  197. return 0; \
  198. if (p) \
  199. { \
  200. if (_delta > 0) \
  201. { \
  202. if (pad == '0') \
  203. memset_zero (p, _delta); \
  204. else \
  205. memset_space (p, _delta); \
  206. } \
  207. f; \
  208. p += _n; \
  209. } \
  210. i += _incr; \
  211. } while (0)
  212. #define cpy(n, s) \
  213. add ((n), \
  214. if (to_lowcase) \
  215. memcpy_lowcase (p, (s), _n); \
  216. else if (to_uppcase) \
  217. memcpy_uppcase (p, (s), _n); \
  218. else \
  219. memcpy ((PTR) p, (PTR) (s), _n))
  220. #ifdef _LIBC
  221. # define TOUPPER(Ch) toupper (Ch)
  222. # define TOLOWER(Ch) tolower (Ch)
  223. #else
  224. # define TOUPPER(Ch) (islower (Ch) ? toupper (Ch) : (Ch))
  225. # define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
  226. #endif
  227. /* We don't use `isdigit' here since the locale dependent
  228. interpretation is not what we want here. We only need to accept
  229. the arabic digits in the ASCII range. One day there is perhaps a
  230. more reliable way to accept other sets of digits. */
  231. #define ISDIGIT(Ch) ((unsigned int) (Ch) - '0' <= 9)
  232. //static char *memcpy_lowcase __P ((char *dest, const char *src, size_t len));
  233. static char *
  234. memcpy_lowcase (char *dest, const char *src, size_t len)
  235. {
  236. while (len-- > 0)
  237. dest[len] = TOLOWER ((unsigned char) src[len]);
  238. return dest;
  239. }
  240. //static char *memcpy_uppcase __P ((char *dest, const char *src, size_t len));
  241. static char *
  242. memcpy_uppcase (char *dest, const char *src, size_t len)
  243. {
  244. while (len-- > 0)
  245. dest[len] = TOUPPER ((unsigned char) src[len]);
  246. return dest;
  247. }
  248. #if ! HAVE_TM_GMTOFF
  249. /* Yield the difference between *A and *B,
  250. measured in seconds, ignoring leap seconds. */
  251. # define tm_diff ftime_tm_diff
  252. //static int tm_diff __P ((const struct tm *, const struct tm *));
  253. static int
  254. tm_diff (const struct tm *a, const struct tm *b)
  255. {
  256. /* Compute intervening leap days correctly even if year is negative.
  257. Take care to avoid int overflow in leap day calculations,
  258. but it's OK to assume that A and B are close to each other. */
  259. int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3);
  260. int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3);
  261. int a100 = a4 / 25 - (a4 % 25 < 0);
  262. int b100 = b4 / 25 - (b4 % 25 < 0);
  263. int a400 = a100 >> 2;
  264. int b400 = b100 >> 2;
  265. int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
  266. int years = a->tm_year - b->tm_year;
  267. int days = (365 * years + intervening_leap_days
  268. + (a->tm_yday - b->tm_yday));
  269. return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour))
  270. + (a->tm_min - b->tm_min))
  271. + (a->tm_sec - b->tm_sec));
  272. }
  273. #endif /* ! HAVE_TM_GMTOFF */
  274. /* The number of days from the first day of the first ISO week of this
  275. year to the year day YDAY with week day WDAY. ISO weeks start on
  276. Monday; the first ISO week has the year's first Thursday. YDAY may
  277. be as small as YDAY_MINIMUM. */
  278. #define ISO_WEEK_START_WDAY 1 /* Monday */
  279. #define ISO_WEEK1_WDAY 4 /* Thursday */
  280. #define YDAY_MINIMUM (-366)
  281. //static int iso_week_days __P ((int, int));
  282. #ifdef __GNUC__
  283. __inline__
  284. #endif
  285. static int
  286. iso_week_days (int yday, int wday)
  287. {
  288. /* Add enough to the first operand of % to make it nonnegative. */
  289. int big_enough_multiple_of_7 = (-YDAY_MINIMUM / 7 + 2) * 7;
  290. return (yday
  291. - (yday - wday + ISO_WEEK1_WDAY + big_enough_multiple_of_7) % 7
  292. + ISO_WEEK1_WDAY - ISO_WEEK_START_WDAY);
  293. }
  294. #if !(defined _NL_CURRENT || HAVE_STRFTIME)
  295. static char const weekday_name[][10] =
  296. {
  297. "Sunday", "Monday", "Tuesday", "Wednesday",
  298. "Thursday", "Friday", "Saturday"
  299. };
  300. static char const month_name[][10] =
  301. {
  302. "January", "February", "March", "April", "May", "June",
  303. "July", "August", "September", "October", "November", "December"
  304. };
  305. #endif
  306. #ifdef emacs
  307. # define my_strftime emacs_strftimeu
  308. # define ut_argument , ut
  309. # define ut_argument_spec int ut;
  310. # define ut_argument_spec_iso , int ut
  311. #else
  312. # define my_strftime strftime
  313. # define ut_argument
  314. # define ut_argument_spec
  315. # define ut_argument_spec_iso
  316. /* We don't have this information in general. */
  317. # define ut 0
  318. #endif
  319. #if !defined _LIBC && HAVE_TZNAME && HAVE_TZSET
  320. /* Solaris 2.5 tzset sometimes modifies the storage returned by localtime.
  321. Work around this bug by copying *tp before it might be munged. */
  322. // size_t _strftime_copytm __P ((char *, size_t, const char *, const struct tm * ut_argument_spec_iso));
  323. size_t
  324. my_strftime (char *s, size_t maxsize, const char *format, const struct tm *tp ut_argument)
  325. {
  326. struct tm tmcopy;
  327. tmcopy = *tp;
  328. return _strftime_copytm (s, maxsize, format, &tmcopy ut_argument);
  329. }
  330. # undef my_strftime
  331. # define my_strftime(S, Maxsize, Format, Tp) \
  332. _strftime_copytm (S, Maxsize, Format, Tp)
  333. #endif
  334. /* Write information from TP into S according to the format
  335. string FORMAT, writing no more that MAXSIZE characters
  336. (including the terminating '\0') and returning number of
  337. characters written. If S is NULL, nothing will be written
  338. anywhere, so to determine how many characters would be
  339. written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */
  340. size_t
  341. my_strftime (char *s, size_t maxsize, const char *format, const struct tm *tp ut_argument)
  342. {
  343. int hour12 = tp->tm_hour;
  344. #ifdef _NL_CURRENT
  345. /* We cannot make the following values variables since we must delay
  346. the evaluation of these values until really needed since some
  347. expressions might not be valid in every situation. The `struct tm'
  348. might be generated by a strptime() call that initialized
  349. only a few elements. Dereference the pointers only if the format
  350. requires this. Then it is ok to fail if the pointers are invalid. */
  351. # define a_wkday _NL_CURRENT (LC_TIME, ABDAY_1 + tp->tm_wday)
  352. # define f_wkday _NL_CURRENT (LC_TIME, DAY_1 + tp->tm_wday)
  353. # define a_month _NL_CURRENT (LC_TIME, ABMON_1 + tp->tm_mon)
  354. # define f_month _NL_CURRENT (LC_TIME, MON_1 + tp->tm_mon)
  355. # define ampm _NL_CURRENT (LC_TIME, tp->tm_hour > 11 ? PM_STR : AM_STR)
  356. # define aw_len strlen (a_wkday)
  357. # define am_len strlen (a_month)
  358. # define ap_len strlen (ampm)
  359. #else
  360. # if !HAVE_STRFTIME
  361. # define f_wkday (weekday_name[tp->tm_wday])
  362. # define f_month (month_name[tp->tm_mon])
  363. # define a_wkday f_wkday
  364. # define a_month f_month
  365. # define ampm ("AMPM" + 2 * (tp->tm_hour > 11))
  366. size_t aw_len = 3;
  367. size_t am_len = 3;
  368. size_t ap_len = 2;
  369. # endif
  370. #endif
  371. const char *zone;
  372. size_t i = 0;
  373. char *p = s;
  374. const char *f;
  375. zone = NULL;
  376. #if HAVE_TM_ZONE
  377. /* The POSIX test suite assumes that setting
  378. the environment variable TZ to a new value before calling strftime()
  379. will influence the result (the %Z format) even if the information in
  380. TP is computed with a totally different time zone.
  381. This is bogus: though POSIX allows bad behavior like this,
  382. POSIX does not require it. Do the right thing instead. */
  383. zone = (const char *) tp->tm_zone;
  384. #endif
  385. #if HAVE_TZNAME
  386. if (ut)
  387. {
  388. if (! (zone && *zone))
  389. zone = "GMT";
  390. }
  391. else
  392. {
  393. /* POSIX.1 8.1.1 requires that whenever strftime() is called, the
  394. time zone names contained in the external variable `tzname' shall
  395. be set as if the tzset() function had been called. */
  396. # if HAVE_TZSET
  397. tzset ();
  398. # endif
  399. }
  400. #endif
  401. if (hour12 > 12)
  402. hour12 -= 12;
  403. else
  404. if (hour12 == 0)
  405. hour12 = 12;
  406. for (f = format; *f != '\0'; ++f)
  407. {
  408. int pad = 0; /* Padding for number ('-', '_', or 0). */
  409. int modifier; /* Field modifier ('E', 'O', or 0). */
  410. int digits; /* Max digits for numeric format. */
  411. int number_value; /* Numeric value to be printed. */
  412. int negative_number; /* 1 if the number is negative. */
  413. const char *subfmt;
  414. char *bufp;
  415. char buf[1 + (sizeof (int) < sizeof (time_t)
  416. ? INT_STRLEN_BOUND (time_t)
  417. : INT_STRLEN_BOUND (int))];
  418. int width = -1;
  419. int to_lowcase = 0;
  420. int to_uppcase = 0;
  421. int change_case = 0;
  422. int format_char;
  423. #if DO_MULTIBYTE
  424. switch (*f)
  425. {
  426. case '%':
  427. break;
  428. case '\a': case '\b': case '\t': case '\n':
  429. case '\v': case '\f': case '\r':
  430. case ' ': case '!': case '"': case '#': case '&': case'\'':
  431. case '(': case ')': case '*': case '+': case ',': case '-':
  432. case '.': case '/': case '0': case '1': case '2': case '3':
  433. case '4': case '5': case '6': case '7': case '8': case '9':
  434. case ':': case ';': case '<': case '=': case '>': case '?':
  435. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  436. case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  437. case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  438. case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  439. case 'Y': case 'Z': case '[': case'\\': case ']': case '^':
  440. case '_': case 'a': case 'b': case 'c': case 'd': case 'e':
  441. case 'f': case 'g': case 'h': case 'i': case 'j': case 'k':
  442. case 'l': case 'm': case 'n': case 'o': case 'p': case 'q':
  443. case 'r': case 's': case 't': case 'u': case 'v': case 'w':
  444. case 'x': case 'y': case 'z': case '{': case '|': case '}':
  445. case '~':
  446. /* The C Standard requires these 98 characters (plus '%') to
  447. be in the basic execution character set. None of these
  448. characters can start a multibyte sequence, so they need
  449. not be analyzed further. */
  450. add (1, *p = *f);
  451. continue;
  452. default:
  453. /* Copy this multibyte sequence until we reach its end, find
  454. an error, or come back to the initial shift state. */
  455. {
  456. mbstate_t mbstate = mbstate_zero;
  457. size_t len = 0;
  458. do
  459. {
  460. size_t bytes = mbrlen (f + len, (size_t) -1, &mbstate);
  461. if (bytes == 0)
  462. break;
  463. if (bytes == (size_t) -2)
  464. {
  465. len += strlen (f + len);
  466. break;
  467. }
  468. if (bytes == (size_t) -1)
  469. {
  470. len++;
  471. break;
  472. }
  473. len += bytes;
  474. }
  475. while (! mbsinit (&mbstate));
  476. cpy (len, f);
  477. f += len - 1;
  478. continue;
  479. }
  480. }
  481. #else /* ! DO_MULTIBYTE */
  482. /* Either multibyte encodings are not supported, or they are
  483. safe for formats, so any non-'%' byte can be copied through. */
  484. if (*f != '%')
  485. {
  486. add (1, *p = *f);
  487. continue;
  488. }
  489. #endif /* ! DO_MULTIBYTE */
  490. /* Check for flags that can modify a format. */
  491. while (1)
  492. {
  493. switch (*++f)
  494. {
  495. /* This influences the number formats. */
  496. case '_':
  497. case '-':
  498. case '0':
  499. pad = *f;
  500. continue;
  501. /* This changes textual output. */
  502. case '^':
  503. to_uppcase = 1;
  504. continue;
  505. case '#':
  506. change_case = 1;
  507. continue;
  508. default:
  509. break;
  510. }
  511. break;
  512. }
  513. /* As a GNU extension we allow to specify the field width. */
  514. if (ISDIGIT (*f))
  515. {
  516. width = 0;
  517. do
  518. {
  519. width *= 10;
  520. width += *f - '0';
  521. ++f;
  522. }
  523. while (ISDIGIT (*f));
  524. }
  525. /* Check for modifiers. */
  526. switch (*f)
  527. {
  528. case 'E':
  529. case 'O':
  530. modifier = *f++;
  531. break;
  532. default:
  533. modifier = 0;
  534. break;
  535. }
  536. /* Now do the specified format. */
  537. format_char = *f;
  538. switch (format_char)
  539. {
  540. #define DO_NUMBER(d, v) \
  541. digits = width == -1 ? d : width; \
  542. number_value = v; goto do_number
  543. #define DO_NUMBER_SPACEPAD(d, v) \
  544. digits = width == -1 ? d : width; \
  545. number_value = v; goto do_number_spacepad
  546. case '%':
  547. if (modifier != 0)
  548. goto bad_format;
  549. add (1, *p = *f);
  550. break;
  551. case 'a':
  552. if (modifier != 0)
  553. goto bad_format;
  554. if (change_case)
  555. {
  556. to_uppcase = 1;
  557. to_lowcase = 0;
  558. }
  559. #if defined _NL_CURRENT || !HAVE_STRFTIME
  560. cpy (aw_len, a_wkday);
  561. break;
  562. #else
  563. goto underlying_strftime;
  564. #endif
  565. case 'A':
  566. if (modifier != 0)
  567. goto bad_format;
  568. if (change_case)
  569. {
  570. to_uppcase = 1;
  571. to_lowcase = 0;
  572. }
  573. #if defined _NL_CURRENT || !HAVE_STRFTIME
  574. cpy (strlen (f_wkday), f_wkday);
  575. break;
  576. #else
  577. goto underlying_strftime;
  578. #endif
  579. case 'b':
  580. case 'h': /* POSIX.2 extension. */
  581. if (modifier != 0)
  582. goto bad_format;
  583. #if defined _NL_CURRENT || !HAVE_STRFTIME
  584. cpy (am_len, a_month);
  585. break;
  586. #else
  587. goto underlying_strftime;
  588. #endif
  589. case 'B':
  590. if (modifier != 0)
  591. goto bad_format;
  592. if (change_case)
  593. {
  594. to_uppcase = 1;
  595. to_lowcase = 0;
  596. }
  597. #if defined _NL_CURRENT || !HAVE_STRFTIME
  598. cpy (strlen (f_month), f_month);
  599. break;
  600. #else
  601. goto underlying_strftime;
  602. #endif
  603. case 'c':
  604. if (modifier == 'O')
  605. goto bad_format;
  606. #ifdef _NL_CURRENT
  607. if (! (modifier == 'E'
  608. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_T_FMT)) != '\0'))
  609. subfmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  610. #else
  611. # if HAVE_STRFTIME
  612. goto underlying_strftime;
  613. # else
  614. subfmt = "%a %b %e %H:%M:%S %Y";
  615. # endif
  616. #endif
  617. subformat:
  618. {
  619. char *old_start = p;
  620. size_t len = my_strftime (NULL, (size_t) -1, subfmt, tp);
  621. add (len, my_strftime (p, maxsize - i, subfmt, tp));
  622. if (to_uppcase)
  623. while (old_start < p)
  624. {
  625. *old_start = TOUPPER ((unsigned char) *old_start);
  626. ++old_start;
  627. }
  628. }
  629. break;
  630. #if HAVE_STRFTIME && ! (defined _NL_CURRENT && HAVE_STRUCT_ERA_ENTRY)
  631. underlying_strftime:
  632. {
  633. /* The relevant information is available only via the
  634. underlying strftime implementation, so use that. */
  635. char ufmt[4];
  636. char *u = ufmt;
  637. char ubuf[1024]; /* enough for any single format in practice */
  638. size_t len;
  639. *u++ = '%';
  640. if (modifier != 0)
  641. *u++ = modifier;
  642. *u++ = format_char;
  643. *u = '\0';
  644. len = strftime (ubuf, sizeof ubuf, ufmt, tp);
  645. if (len == 0 && ubuf[0] != '\0')
  646. return 0;
  647. cpy (len, ubuf);
  648. }
  649. break;
  650. #endif
  651. case 'C': /* POSIX.2 extension. */
  652. if (modifier == 'O')
  653. goto bad_format;
  654. if (modifier == 'E')
  655. {
  656. #if HAVE_STRUCT_ERA_ENTRY
  657. struct era_entry *era = _nl_get_era_entry (tp);
  658. if (era)
  659. {
  660. size_t len = strlen (era->name_fmt);
  661. cpy (len, era->name_fmt);
  662. break;
  663. }
  664. #else
  665. # if HAVE_STRFTIME
  666. goto underlying_strftime;
  667. # endif
  668. #endif
  669. }
  670. {
  671. int year = tp->tm_year + TM_YEAR_BASE;
  672. DO_NUMBER (1, year / 100 - (year % 100 < 0));
  673. }
  674. case 'x':
  675. if (modifier == 'O')
  676. goto bad_format;
  677. #ifdef _NL_CURRENT
  678. if (! (modifier == 'E'
  679. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_D_FMT)) != '\0'))
  680. subfmt = _NL_CURRENT (LC_TIME, D_FMT);
  681. goto subformat;
  682. #else
  683. # if HAVE_STRFTIME
  684. goto underlying_strftime;
  685. # else
  686. /* Fall through. */
  687. # endif
  688. #endif
  689. case 'D': /* POSIX.2 extension. */
  690. if (modifier != 0)
  691. goto bad_format;
  692. subfmt = "%m/%d/%y";
  693. goto subformat;
  694. case 'd':
  695. if (modifier == 'E')
  696. goto bad_format;
  697. DO_NUMBER (2, tp->tm_mday);
  698. case 'e': /* POSIX.2 extension. */
  699. if (modifier == 'E')
  700. goto bad_format;
  701. DO_NUMBER_SPACEPAD (2, tp->tm_mday);
  702. /* All numeric formats set DIGITS and NUMBER_VALUE and then
  703. jump to one of these two labels. */
  704. do_number_spacepad:
  705. /* Force `_' flag unless overwritten by `0' flag. */
  706. if (pad != '0')
  707. pad = '_';
  708. do_number:
  709. /* Format the number according to the MODIFIER flag. */
  710. if (modifier == 'O' && 0 <= number_value)
  711. {
  712. #ifdef _NL_CURRENT
  713. /* Get the locale specific alternate representation of
  714. the number NUMBER_VALUE. If none exist NULL is returned. */
  715. const char *cp = _nl_get_alt_digit (number_value);
  716. if (cp != NULL)
  717. {
  718. size_t digitlen = strlen (cp);
  719. if (digitlen != 0)
  720. {
  721. cpy (digitlen, cp);
  722. break;
  723. }
  724. }
  725. #else
  726. # if HAVE_STRFTIME
  727. goto underlying_strftime;
  728. # endif
  729. #endif
  730. }
  731. {
  732. unsigned int u = number_value;
  733. bufp = buf + sizeof (buf);
  734. negative_number = number_value < 0;
  735. if (negative_number)
  736. u = -u;
  737. do
  738. *--bufp = u % 10 + '0';
  739. while ((u /= 10) != 0);
  740. }
  741. do_number_sign_and_padding:
  742. if (negative_number)
  743. *--bufp = '-';
  744. if (pad != '-')
  745. {
  746. int padding = digits - (buf + sizeof (buf) - bufp);
  747. if (pad == '_')
  748. {
  749. while (0 < padding--)
  750. *--bufp = ' ';
  751. }
  752. else
  753. {
  754. bufp += negative_number;
  755. while (0 < padding--)
  756. *--bufp = '0';
  757. if (negative_number)
  758. *--bufp = '-';
  759. }
  760. }
  761. cpy (buf + sizeof (buf) - bufp, bufp);
  762. break;
  763. case 'F':
  764. if (modifier != 0)
  765. goto bad_format;
  766. subfmt = "%Y-%m-%d";
  767. goto subformat;
  768. case 'H':
  769. if (modifier == 'E')
  770. goto bad_format;
  771. DO_NUMBER (2, tp->tm_hour);
  772. case 'I':
  773. if (modifier == 'E')
  774. goto bad_format;
  775. DO_NUMBER (2, hour12);
  776. case 'k': /* GNU extension. */
  777. if (modifier == 'E')
  778. goto bad_format;
  779. DO_NUMBER_SPACEPAD (2, tp->tm_hour);
  780. case 'l': /* GNU extension. */
  781. if (modifier == 'E')
  782. goto bad_format;
  783. DO_NUMBER_SPACEPAD (2, hour12);
  784. case 'j':
  785. if (modifier == 'E')
  786. goto bad_format;
  787. DO_NUMBER (3, 1 + tp->tm_yday);
  788. case 'M':
  789. if (modifier == 'E')
  790. goto bad_format;
  791. DO_NUMBER (2, tp->tm_min);
  792. case 'm':
  793. if (modifier == 'E')
  794. goto bad_format;
  795. DO_NUMBER (2, tp->tm_mon + 1);
  796. case 'n': /* POSIX.2 extension. */
  797. add (1, *p = '\n');
  798. break;
  799. case 'P':
  800. to_lowcase = 1;
  801. #if !defined _NL_CURRENT && HAVE_STRFTIME
  802. format_char = 'p';
  803. #endif
  804. /* FALLTHROUGH */
  805. case 'p':
  806. if (change_case)
  807. {
  808. to_uppcase = 0;
  809. to_lowcase = 1;
  810. }
  811. #if defined _NL_CURRENT || !HAVE_STRFTIME
  812. cpy (ap_len, ampm);
  813. break;
  814. #else
  815. goto underlying_strftime;
  816. #endif
  817. case 'R': /* GNU extension. */
  818. subfmt = "%H:%M";
  819. goto subformat;
  820. case 'r': /* POSIX.2 extension. */
  821. #ifdef _NL_CURRENT
  822. if (*(subfmt = _NL_CURRENT (LC_TIME, T_FMT_AMPM)) == '\0')
  823. #endif
  824. subfmt = "%I:%M:%S %p";
  825. goto subformat;
  826. case 'S':
  827. if (modifier == 'E')
  828. goto bad_format;
  829. DO_NUMBER (2, tp->tm_sec);
  830. case 's': /* GNU extension. */
  831. {
  832. struct tm ltm;
  833. time_t t;
  834. ltm = *tp;
  835. t = mktime (&ltm);
  836. /* Generate string value for T using time_t arithmetic;
  837. this works even if sizeof (long) < sizeof (time_t). */
  838. bufp = buf + sizeof (buf);
  839. negative_number = t < 0;
  840. do
  841. {
  842. int d = t % 10;
  843. t /= 10;
  844. if (negative_number)
  845. {
  846. d = -d;
  847. /* Adjust if division truncates to minus infinity. */
  848. if (0 < -1 % 10 && d < 0)
  849. {
  850. t++;
  851. d += 10;
  852. }
  853. }
  854. *--bufp = d + '0';
  855. }
  856. while (t != 0);
  857. digits = 1;
  858. goto do_number_sign_and_padding;
  859. }
  860. case 'X':
  861. if (modifier == 'O')
  862. goto bad_format;
  863. #ifdef _NL_CURRENT
  864. if (! (modifier == 'E'
  865. && *(subfmt = _NL_CURRENT (LC_TIME, ERA_T_FMT)) != '\0'))
  866. subfmt = _NL_CURRENT (LC_TIME, T_FMT);
  867. goto subformat;
  868. #else
  869. # if HAVE_STRFTIME
  870. goto underlying_strftime;
  871. # else
  872. /* Fall through. */
  873. # endif
  874. #endif
  875. case 'T': /* POSIX.2 extension. */
  876. subfmt = "%H:%M:%S";
  877. goto subformat;
  878. case 't': /* POSIX.2 extension. */
  879. add (1, *p = '\t');
  880. break;
  881. case 'u': /* POSIX.2 extension. */
  882. DO_NUMBER (1, (tp->tm_wday - 1 + 7) % 7 + 1);
  883. case 'U':
  884. if (modifier == 'E')
  885. goto bad_format;
  886. DO_NUMBER (2, (tp->tm_yday - tp->tm_wday + 7) / 7);
  887. case 'V':
  888. case 'g': /* GNU extension. */
  889. case 'G': /* GNU extension. */
  890. if (modifier == 'E')
  891. goto bad_format;
  892. {
  893. int year = tp->tm_year + TM_YEAR_BASE;
  894. int days = iso_week_days (tp->tm_yday, tp->tm_wday);
  895. if (days < 0)
  896. {
  897. /* This ISO week belongs to the previous year. */
  898. year--;
  899. days = iso_week_days (tp->tm_yday + (365 + __isleap (year)),
  900. tp->tm_wday);
  901. }
  902. else
  903. {
  904. int d = iso_week_days (tp->tm_yday - (365 + __isleap (year)),
  905. tp->tm_wday);
  906. if (0 <= d)
  907. {
  908. /* This ISO week belongs to the next year. */
  909. year++;
  910. days = d;
  911. }
  912. }
  913. switch (*f)
  914. {
  915. case 'g':
  916. DO_NUMBER (2, (year % 100 + 100) % 100);
  917. case 'G':
  918. DO_NUMBER (1, year);
  919. default:
  920. DO_NUMBER (2, days / 7 + 1);
  921. }
  922. }
  923. case 'W':
  924. if (modifier == 'E')
  925. goto bad_format;
  926. DO_NUMBER (2, (tp->tm_yday - (tp->tm_wday - 1 + 7) % 7 + 7) / 7);
  927. case 'w':
  928. if (modifier == 'E')
  929. goto bad_format;
  930. DO_NUMBER (1, tp->tm_wday);
  931. case 'Y':
  932. if (modifier == 'E')
  933. {
  934. #if HAVE_STRUCT_ERA_ENTRY
  935. struct era_entry *era = _nl_get_era_entry (tp);
  936. if (era)
  937. {
  938. subfmt = strchr (era->name_fmt, '\0') + 1;
  939. goto subformat;
  940. }
  941. #else
  942. # if HAVE_STRFTIME
  943. goto underlying_strftime;
  944. # endif
  945. #endif
  946. }
  947. if (modifier == 'O')
  948. goto bad_format;
  949. else
  950. DO_NUMBER (1, tp->tm_year + TM_YEAR_BASE);
  951. case 'y':
  952. if (modifier == 'E')
  953. {
  954. #if HAVE_STRUCT_ERA_ENTRY
  955. struct era_entry *era = _nl_get_era_entry (tp);
  956. if (era)
  957. {
  958. int delta = tp->tm_year - era->start_date[0];
  959. DO_NUMBER (1, (era->offset
  960. + (era->direction == '-' ? -delta : delta)));
  961. }
  962. #else
  963. # if HAVE_STRFTIME
  964. goto underlying_strftime;
  965. # endif
  966. #endif
  967. }
  968. DO_NUMBER (2, (tp->tm_year % 100 + 100) % 100);
  969. case 'Z':
  970. if (change_case)
  971. {
  972. to_uppcase = 0;
  973. to_lowcase = 1;
  974. }
  975. #if HAVE_TZNAME
  976. /* The tzset() call might have changed the value. */
  977. if (!(zone && *zone) && tp->tm_isdst >= 0)
  978. zone = tzname[tp->tm_isdst];
  979. #endif
  980. if (! zone)
  981. zone = ""; /* POSIX.2 requires the empty string here. */
  982. cpy (strlen (zone), zone);
  983. break;
  984. case 'z': /* GNU extension. */
  985. if (tp->tm_isdst < 0)
  986. break;
  987. {
  988. int diff;
  989. #if HAVE_TM_GMTOFF
  990. diff = tp->tm_gmtoff;
  991. #else
  992. if (ut)
  993. diff = 0;
  994. else
  995. {
  996. struct tm gtm;
  997. struct tm ltm;
  998. time_t lt;
  999. ltm = *tp;
  1000. lt = mktime (&ltm);
  1001. if (lt == (time_t) -1)
  1002. {
  1003. /* mktime returns -1 for errors, but -1 is also a
  1004. valid time_t value. Check whether an error really
  1005. occurred. */
  1006. struct tm tm;
  1007. if (! my_strftime_localtime_r (&lt, &tm)
  1008. || ((ltm.tm_sec ^ tm.tm_sec)
  1009. | (ltm.tm_min ^ tm.tm_min)
  1010. | (ltm.tm_hour ^ tm.tm_hour)
  1011. | (ltm.tm_mday ^ tm.tm_mday)
  1012. | (ltm.tm_mon ^ tm.tm_mon)
  1013. | (ltm.tm_year ^ tm.tm_year)))
  1014. break;
  1015. }
  1016. if (! my_strftime_gmtime_r (&lt, &gtm))
  1017. break;
  1018. diff = tm_diff (&ltm, &gtm);
  1019. }
  1020. #endif
  1021. if (diff < 0)
  1022. {
  1023. add (1, *p = '-');
  1024. diff = -diff;
  1025. }
  1026. else
  1027. add (1, *p = '+');
  1028. diff /= 60;
  1029. DO_NUMBER (4, (diff / 60) * 100 + diff % 60);
  1030. }
  1031. case '\0': /* GNU extension: % at end of format. */
  1032. --f;
  1033. /* Fall through. */
  1034. default:
  1035. /* Unknown format; output the format, including the '%',
  1036. since this is most likely the right thing to do if a
  1037. multibyte string has been misparsed. */
  1038. bad_format:
  1039. {
  1040. int flen;
  1041. for (flen = 1; f[1 - flen] != '%'; flen++)
  1042. continue;
  1043. cpy (flen, &f[1 - flen]);
  1044. }
  1045. break;
  1046. }
  1047. }
  1048. if (p && maxsize != 0)
  1049. *p = '\0';
  1050. return i;
  1051. }
  1052. #ifdef emacs
  1053. /* For Emacs we have a separate interface which corresponds to the normal
  1054. strftime function and does not have the extra information whether the
  1055. TP arguments comes from a `gmtime' call or not. */
  1056. size_t
  1057. emacs_strftime (char *s, size_t maxsize, const char *format, const struct tm *tp)
  1058. {
  1059. return my_strftime (s, maxsize, format, tp, 0);
  1060. }
  1061. #endif