strftime.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Copyright (C) 1991, 92, 93, 94, 95, 96 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
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. #include <stddef.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <sys/types.h> /* Some systems define `time_t' here. */
  20. #include <sys/time.h>
  21. #include <time.h>
  22. #include <ctype.h>
  23. #include <limits.h>
  24. static unsigned int week (const struct tm *const, int, int);
  25. #define add(n, f) \
  26. do \
  27. { \
  28. i += (n); \
  29. if (i >= maxsize) \
  30. return 0; \
  31. else \
  32. if (p) \
  33. { \
  34. f; \
  35. p += (n); \
  36. } \
  37. } while (0)
  38. #define cpy(n, s) add((n), memcpy((void *) p, (void *) (s), (n)))
  39. #ifdef _LIBC
  40. #define fmt(n, args) add((n), if (sprintf args != (n)) return 0)
  41. #else
  42. #define fmt(n, args) add((n), sprintf args; if (strlen (p) != (n)) return 0)
  43. #endif
  44. /* Return the week in the year specified by TP,
  45. with weeks starting on STARTING_DAY. */
  46. static unsigned int week(const struct tm *const tp , int starting_day , int max_preceding )
  47. {
  48. int wday, dl, base;
  49. wday = tp->tm_wday - starting_day;
  50. if (wday < 0)
  51. wday += 7;
  52. /* Set DL to the day in the year of the first day of the week
  53. containing the day specified in TP. */
  54. dl = tp->tm_yday - wday;
  55. /* For the computation following ISO 8601:1988 we set the number of
  56. the week containing January 1st to 1 if this week has more than
  57. MAX_PRECEDING days in the new year. For ISO 8601 this number is
  58. 3, for the other representation it is 7 (i.e., not to be
  59. fulfilled). */
  60. base = ((dl + 7) % 7) > max_preceding ? 1 : 0;
  61. /* If DL is negative we compute the result as 0 unless we have to
  62. compute it according ISO 8601. In this case we have to return 53
  63. or 1 if the week containing January 1st has less than 4 days in
  64. the new year or not. If DL is not negative we calculate the
  65. number of complete weeks for our week (DL / 7) plus 1 (because
  66. only for DL < 0 we are in week 0/53 and plus the number of the
  67. first week computed in the last step. */
  68. return dl < 0 ? (dl < -max_preceding ? 53 : base)
  69. : base + 1 + dl / 7;
  70. }
  71. #ifndef __UCLIBC_HAS_LOCALE__
  72. extern char const __weekday_name[][10];
  73. extern char const __month_name[][10];
  74. #endif
  75. /* Write information from TP into S according to the format
  76. string FORMAT, writing no more that MAXSIZE characters
  77. (including the terminating '\0') and returning number of
  78. characters written. If S is NULL, nothing will be written
  79. anywhere, so to determine how many characters would be
  80. written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */
  81. size_t strftime( char *s , size_t maxsize , const char *format , register const struct tm *tp)
  82. {
  83. int hour12 = tp->tm_hour;
  84. #ifdef __UCLIBC_HAS_LOCALE__
  85. const char *const a_wkday = _NL_CURRENT (LC_TIME, ABDAY_1 + tp->tm_wday);
  86. const char *const f_wkday = _NL_CURRENT (LC_TIME, DAY_1 + tp->tm_wday);
  87. const char *const a_month = _NL_CURRENT (LC_TIME, ABMON_1 + tp->tm_mon);
  88. const char *const f_month = _NL_CURRENT (LC_TIME, MON_1 + tp->tm_mon);
  89. const char *const ampm = _NL_CURRENT (LC_TIME,
  90. hour12 > 11 ? PM_STR : AM_STR);
  91. size_t aw_len = strlen(a_wkday);
  92. size_t am_len = strlen(a_month);
  93. size_t ap_len = strlen (ampm);
  94. #else
  95. const char *const f_wkday = __weekday_name[tp->tm_wday];
  96. const char *const f_month = __month_name[tp->tm_mon];
  97. const char *const a_wkday = f_wkday;
  98. const char *const a_month = f_month;
  99. const char *const ampm = "AMPM" + 2 * (hour12 > 11);
  100. size_t aw_len = 3;
  101. size_t am_len = 3;
  102. size_t ap_len = 2;
  103. #endif
  104. size_t wkday_len = strlen(f_wkday);
  105. size_t month_len = strlen(f_month);
  106. const unsigned int y_week0 = week (tp, 0, 7);
  107. const unsigned int y_week1 = week (tp, 1, 7);
  108. const unsigned int y_week2 = week (tp, 1, 3);
  109. const char *zone;
  110. size_t zonelen;
  111. register size_t i = 0;
  112. register char *p = s;
  113. register const char *f;
  114. char number_fmt[5];
  115. /* Initialize the buffer we will use for the sprintf format for numbers. */
  116. number_fmt[0] = '%';
  117. /* The POSIX test suite assumes that setting
  118. the environment variable TZ to a new value before calling strftime()
  119. will influence the result (the %Z format) even if the information in
  120. TP is computed with a totally different time zone.
  121. This is bogus: though POSIX allows bad behavior like this,
  122. POSIX does not require it. Do the right thing instead. */
  123. zone = (const char *) tp->tm_zone;
  124. /* POSIX.1 8.1.1 requires that whenever strftime() is called, the
  125. time zone names contained in the external variable `tzname' shall
  126. be set as if the tzset() function had been called. */
  127. tzset ();
  128. zonelen = strlen (zone);
  129. if (hour12 > 12)
  130. hour12 -= 12;
  131. else
  132. if (hour12 == 0) hour12 = 12;
  133. for (f = format; *f != '\0'; ++f)
  134. {
  135. enum { pad_zero, pad_space, pad_none } pad; /* Padding for number. */
  136. unsigned int maxdigits; /* Max digits for numeric format. */
  137. unsigned int number_value; /* Numeric value to be printed. */
  138. const char *subfmt;
  139. #if HAVE_MBLEN
  140. if (!isascii(*f))
  141. {
  142. /* Non-ASCII, may be a multibyte. */
  143. int len = mblen(f, strlen(f));
  144. if (len > 0)
  145. {
  146. cpy(len, f);
  147. continue;
  148. }
  149. }
  150. #endif
  151. if (*f != '%')
  152. {
  153. add(1, *p = *f);
  154. continue;
  155. }
  156. /* Check for flags that can modify a number format. */
  157. ++f;
  158. switch (*f)
  159. {
  160. case '_':
  161. pad = pad_space;
  162. ++f;
  163. break;
  164. case '-':
  165. pad = pad_none;
  166. ++f;
  167. break;
  168. default:
  169. pad = pad_zero;
  170. break;
  171. }
  172. /* Now do the specified format. */
  173. switch (*f)
  174. {
  175. case '\0':
  176. case '%':
  177. add(1, *p = *f);
  178. break;
  179. case 'a':
  180. cpy(aw_len, a_wkday);
  181. break;
  182. case 'A':
  183. cpy(wkday_len, f_wkday);
  184. break;
  185. case 'b':
  186. case 'h': /* GNU extension. */
  187. cpy(am_len, a_month);
  188. break;
  189. case 'B':
  190. cpy(month_len, f_month);
  191. break;
  192. case 'c':
  193. #ifdef __UCLIBC_HAS_LOCALE__
  194. subfmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  195. #else
  196. subfmt = "%a %b %d %H:%M:%S %Z %Y";
  197. #endif
  198. subformat:
  199. {
  200. size_t len = strftime (p, maxsize - i, subfmt, tp);
  201. if (len == 0 && *subfmt)
  202. return 0;
  203. add(len, );
  204. }
  205. break;
  206. #define DO_NUMBER(digits, value) \
  207. maxdigits = digits; number_value = value; goto do_number
  208. #define DO_NUMBER_NOPAD(digits, value) \
  209. maxdigits = digits; number_value = value; goto do_number_nopad
  210. case 'C':
  211. DO_NUMBER (2, (1900 + tp->tm_year) / 100);
  212. case 'x':
  213. #ifdef __UCLIBC_HAS_LOCALE__
  214. subfmt = _NL_CURRENT (LC_TIME, D_FMT);
  215. goto subformat;
  216. #endif
  217. /* Fall through. */
  218. case 'D': /* GNU extension. */
  219. subfmt = "%m/%d/%y";
  220. goto subformat;
  221. case 'd':
  222. DO_NUMBER (2, tp->tm_mday);
  223. case 'e': /* GNU extension: %d, but blank-padded. */
  224. #if 0
  225. DO_NUMBER_NOPAD (2, tp->tm_mday);
  226. #else
  227. DO_NUMBER (2, tp->tm_mday);
  228. #endif
  229. /* All numeric formats set MAXDIGITS and NUMBER_VALUE and then
  230. jump to one of these two labels. */
  231. do_number_nopad:
  232. /* Force `-' flag. */
  233. pad = pad_none;
  234. do_number:
  235. {
  236. /* Format the number according to the PAD flag. */
  237. register char *nf = &number_fmt[1];
  238. int printed;
  239. switch (pad)
  240. {
  241. case pad_zero:
  242. *nf++ = '0';
  243. case pad_space:
  244. *nf++ = '0' + maxdigits;
  245. case pad_none:
  246. *nf++ = 'u';
  247. *nf = '\0';
  248. }
  249. #ifdef _LIBC
  250. if (i + maxdigits >= maxsize)
  251. return 0;
  252. printed = sprintf (p, number_fmt, number_value);
  253. i += printed;
  254. p += printed;
  255. #else
  256. add (maxdigits, sprintf (p, number_fmt, number_value);
  257. printed = strlen (p));
  258. #endif
  259. break;
  260. }
  261. case 'H':
  262. DO_NUMBER (2, tp->tm_hour);
  263. case 'I':
  264. DO_NUMBER (2, hour12);
  265. case 'k': /* GNU extension. */
  266. DO_NUMBER_NOPAD (2, tp->tm_hour);
  267. case 'l': /* GNU extension. */
  268. DO_NUMBER_NOPAD (2, hour12);
  269. case 'j':
  270. DO_NUMBER (3, 1 + tp->tm_yday);
  271. case 'M':
  272. DO_NUMBER (2, tp->tm_min);
  273. case 'm':
  274. DO_NUMBER (2, tp->tm_mon + 1);
  275. case 'n': /* GNU extension. */
  276. add (1, *p = '\n');
  277. break;
  278. case 'p':
  279. cpy(ap_len, ampm);
  280. break;
  281. case 'R': /* GNU extension. */
  282. subfmt = "%H:%M";
  283. goto subformat;
  284. case 'r': /* GNU extension. */
  285. subfmt = "%I:%M:%S %p";
  286. goto subformat;
  287. case 'S':
  288. DO_NUMBER (2, tp->tm_sec);
  289. case 'X':
  290. #ifdef __UCLIBC_HAS_LOCALE__
  291. subfmt = _NL_CURRENT (LC_TIME, T_FMT);
  292. goto subformat;
  293. #endif
  294. /* Fall through. */
  295. case 'T': /* GNU extenstion. */
  296. subfmt = "%H:%M:%S";
  297. goto subformat;
  298. case 't': /* GNU extenstion. */
  299. add (1, *p = '\t');
  300. break;
  301. case 'U':
  302. DO_NUMBER (2, y_week0);
  303. case 'V':
  304. DO_NUMBER (2, y_week2);
  305. case 'W':
  306. DO_NUMBER (2, y_week1);
  307. case 'w':
  308. DO_NUMBER (1, tp->tm_wday);
  309. case 'Y':
  310. DO_NUMBER (4, 1900 + tp->tm_year);
  311. case 'y':
  312. DO_NUMBER (2, tp->tm_year % 100);
  313. case 'Z':
  314. /* The tzset() call might have changed the value. */
  315. if (!(zone && *zone) && tp->tm_isdst >= 0)
  316. zone = tzname[tp->tm_isdst];
  317. if (! zone)
  318. zone = ""; /* POSIX.2 requires the empty string here. */
  319. cpy(zonelen, zone);
  320. break;
  321. default:
  322. /* Bad format. */
  323. break;
  324. }
  325. }
  326. if (p)
  327. *p = '\0';
  328. return i;
  329. }