strftime.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 _NL_CURRENT
  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 _NL_CURRENT
  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. zone = 0;
  118. #if HAVE_TM_ZONE
  119. zone = (const char *) tp->tm_zone;
  120. #endif
  121. if (!(zone && *zone) && tp->tm_isdst >= 0)
  122. zone = tzname[tp->tm_isdst];
  123. if (!(zone && *zone))
  124. zone = "???";
  125. zonelen = strlen (zone);
  126. if (hour12 > 12)
  127. hour12 -= 12;
  128. else
  129. if (hour12 == 0) hour12 = 12;
  130. for (f = format; *f != '\0'; ++f)
  131. {
  132. enum { pad_zero, pad_space, pad_none } pad; /* Padding for number. */
  133. unsigned int maxdigits; /* Max digits for numeric format. */
  134. unsigned int number_value; /* Numeric value to be printed. */
  135. const char *subfmt;
  136. #if HAVE_MBLEN
  137. if (!isascii(*f))
  138. {
  139. /* Non-ASCII, may be a multibyte. */
  140. int len = mblen(f, strlen(f));
  141. if (len > 0)
  142. {
  143. cpy(len, f);
  144. continue;
  145. }
  146. }
  147. #endif
  148. if (*f != '%')
  149. {
  150. add(1, *p = *f);
  151. continue;
  152. }
  153. /* Check for flags that can modify a number format. */
  154. ++f;
  155. switch (*f)
  156. {
  157. case '_':
  158. pad = pad_space;
  159. ++f;
  160. break;
  161. case '-':
  162. pad = pad_none;
  163. ++f;
  164. break;
  165. default:
  166. pad = pad_zero;
  167. break;
  168. }
  169. /* Now do the specified format. */
  170. switch (*f)
  171. {
  172. case '\0':
  173. case '%':
  174. add(1, *p = *f);
  175. break;
  176. case 'a':
  177. cpy(aw_len, a_wkday);
  178. break;
  179. case 'A':
  180. cpy(wkday_len, f_wkday);
  181. break;
  182. case 'b':
  183. case 'h': /* GNU extension. */
  184. cpy(am_len, a_month);
  185. break;
  186. case 'B':
  187. cpy(month_len, f_month);
  188. break;
  189. case 'c':
  190. #ifdef _NL_CURRENT
  191. subfmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  192. #else
  193. subfmt = "%a %b %d %H:%M:%S %Z %Y";
  194. #endif
  195. subformat:
  196. {
  197. size_t len = strftime (p, maxsize - i, subfmt, tp);
  198. if (len == 0 && *subfmt)
  199. return 0;
  200. add(len, );
  201. }
  202. break;
  203. #define DO_NUMBER(digits, value) \
  204. maxdigits = digits; number_value = value; goto do_number
  205. #define DO_NUMBER_NOPAD(digits, value) \
  206. maxdigits = digits; number_value = value; goto do_number_nopad
  207. case 'C':
  208. DO_NUMBER (2, (1900 + tp->tm_year) / 100);
  209. case 'x':
  210. #ifdef _NL_CURRENT
  211. subfmt = _NL_CURRENT (LC_TIME, D_FMT);
  212. goto subformat;
  213. #endif
  214. /* Fall through. */
  215. case 'D': /* GNU extension. */
  216. subfmt = "%m/%d/%y";
  217. goto subformat;
  218. case 'd':
  219. DO_NUMBER (2, tp->tm_mday);
  220. case 'e': /* GNU extension: %d, but blank-padded. */
  221. #if 0
  222. DO_NUMBER_NOPAD (2, tp->tm_mday);
  223. #else
  224. DO_NUMBER (2, tp->tm_mday);
  225. #endif
  226. /* All numeric formats set MAXDIGITS and NUMBER_VALUE and then
  227. jump to one of these two labels. */
  228. do_number_nopad:
  229. /* Force `-' flag. */
  230. pad = pad_none;
  231. do_number:
  232. {
  233. /* Format the number according to the PAD flag. */
  234. register char *nf = &number_fmt[1];
  235. int printed;
  236. switch (pad)
  237. {
  238. case pad_zero:
  239. *nf++ = '0';
  240. case pad_space:
  241. *nf++ = '0' + maxdigits;
  242. case pad_none:
  243. *nf++ = 'u';
  244. *nf = '\0';
  245. }
  246. #ifdef _LIBC
  247. if (i + maxdigits >= maxsize)
  248. return 0;
  249. printed = sprintf (p, number_fmt, number_value);
  250. i += printed;
  251. p += printed;
  252. #else
  253. add (maxdigits, sprintf (p, number_fmt, number_value);
  254. printed = strlen (p));
  255. #endif
  256. break;
  257. }
  258. case 'H':
  259. DO_NUMBER (2, tp->tm_hour);
  260. case 'I':
  261. DO_NUMBER (2, hour12);
  262. case 'k': /* GNU extension. */
  263. DO_NUMBER_NOPAD (2, tp->tm_hour);
  264. case 'l': /* GNU extension. */
  265. DO_NUMBER_NOPAD (2, hour12);
  266. case 'j':
  267. DO_NUMBER (3, 1 + tp->tm_yday);
  268. case 'M':
  269. DO_NUMBER (2, tp->tm_min);
  270. case 'm':
  271. DO_NUMBER (2, tp->tm_mon + 1);
  272. case 'n': /* GNU extension. */
  273. add (1, *p = '\n');
  274. break;
  275. case 'p':
  276. cpy(ap_len, ampm);
  277. break;
  278. case 'R': /* GNU extension. */
  279. subfmt = "%H:%M";
  280. goto subformat;
  281. case 'r': /* GNU extension. */
  282. subfmt = "%I:%M:%S %p";
  283. goto subformat;
  284. case 'S':
  285. DO_NUMBER (2, tp->tm_sec);
  286. case 'X':
  287. #ifdef _NL_CURRENT
  288. subfmt = _NL_CURRENT (LC_TIME, T_FMT);
  289. goto subformat;
  290. #endif
  291. /* Fall through. */
  292. case 'T': /* GNU extenstion. */
  293. subfmt = "%H:%M:%S";
  294. goto subformat;
  295. case 't': /* GNU extenstion. */
  296. add (1, *p = '\t');
  297. break;
  298. case 'U':
  299. DO_NUMBER (2, y_week0);
  300. case 'V':
  301. DO_NUMBER (2, y_week2);
  302. case 'W':
  303. DO_NUMBER (2, y_week1);
  304. case 'w':
  305. DO_NUMBER (1, tp->tm_wday);
  306. case 'Y':
  307. DO_NUMBER (4, 1900 + tp->tm_year);
  308. case 'y':
  309. DO_NUMBER (2, tp->tm_year % 100);
  310. case 'Z':
  311. cpy(zonelen, zone);
  312. break;
  313. default:
  314. /* Bad format. */
  315. break;
  316. }
  317. }
  318. if (p)
  319. *p = '\0';
  320. return i;
  321. }