strftime.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. static char const weekday_name[][10] =
  73. {
  74. "Sunday", "Monday", "Tuesday", "Wednesday",
  75. "Thursday", "Friday", "Saturday"
  76. };
  77. static char const month_name[][10] =
  78. {
  79. "January", "February", "March", "April", "May", "June",
  80. "July", "August", "September", "October", "November", "December"
  81. };
  82. #endif
  83. /* Write information from TP into S according to the format
  84. string FORMAT, writing no more that MAXSIZE characters
  85. (including the terminating '\0') and returning number of
  86. characters written. If S is NULL, nothing will be written
  87. anywhere, so to determine how many characters would be
  88. written, use NULL for S and (size_t) UINT_MAX for MAXSIZE. */
  89. size_t strftime( char *s , size_t maxsize , const char *format , register const struct tm *tp)
  90. {
  91. int hour12 = tp->tm_hour;
  92. #ifdef _NL_CURRENT
  93. const char *const a_wkday = _NL_CURRENT (LC_TIME, ABDAY_1 + tp->tm_wday);
  94. const char *const f_wkday = _NL_CURRENT (LC_TIME, DAY_1 + tp->tm_wday);
  95. const char *const a_month = _NL_CURRENT (LC_TIME, ABMON_1 + tp->tm_mon);
  96. const char *const f_month = _NL_CURRENT (LC_TIME, MON_1 + tp->tm_mon);
  97. const char *const ampm = _NL_CURRENT (LC_TIME,
  98. hour12 > 11 ? PM_STR : AM_STR);
  99. size_t aw_len = strlen(a_wkday);
  100. size_t am_len = strlen(a_month);
  101. size_t ap_len = strlen (ampm);
  102. #else
  103. const char *const f_wkday = weekday_name[tp->tm_wday];
  104. const char *const f_month = month_name[tp->tm_mon];
  105. const char *const a_wkday = f_wkday;
  106. const char *const a_month = f_month;
  107. const char *const ampm = "AMPM" + 2 * (hour12 > 11);
  108. size_t aw_len = 3;
  109. size_t am_len = 3;
  110. size_t ap_len = 2;
  111. #endif
  112. size_t wkday_len = strlen(f_wkday);
  113. size_t month_len = strlen(f_month);
  114. const unsigned int y_week0 = week (tp, 0, 7);
  115. const unsigned int y_week1 = week (tp, 1, 7);
  116. const unsigned int y_week2 = week (tp, 1, 3);
  117. const char *zone;
  118. size_t zonelen;
  119. register size_t i = 0;
  120. register char *p = s;
  121. register const char *f;
  122. char number_fmt[5];
  123. /* Initialize the buffer we will use for the sprintf format for numbers. */
  124. number_fmt[0] = '%';
  125. zone = 0;
  126. #if HAVE_TM_ZONE
  127. zone = (const char *) tp->tm_zone;
  128. #endif
  129. #if HAVE_TZNAME
  130. if (!(zone && *zone) && tp->tm_isdst >= 0)
  131. zone = tzname[tp->tm_isdst];
  132. #endif
  133. if (!(zone && *zone))
  134. zone = "???";
  135. zonelen = strlen (zone);
  136. if (hour12 > 12)
  137. hour12 -= 12;
  138. else
  139. if (hour12 == 0) hour12 = 12;
  140. for (f = format; *f != '\0'; ++f)
  141. {
  142. enum { pad_zero, pad_space, pad_none } pad; /* Padding for number. */
  143. unsigned int maxdigits; /* Max digits for numeric format. */
  144. unsigned int number_value; /* Numeric value to be printed. */
  145. const char *subfmt;
  146. #if HAVE_MBLEN
  147. if (!isascii(*f))
  148. {
  149. /* Non-ASCII, may be a multibyte. */
  150. int len = mblen(f, strlen(f));
  151. if (len > 0)
  152. {
  153. cpy(len, f);
  154. continue;
  155. }
  156. }
  157. #endif
  158. if (*f != '%')
  159. {
  160. add(1, *p = *f);
  161. continue;
  162. }
  163. /* Check for flags that can modify a number format. */
  164. ++f;
  165. switch (*f)
  166. {
  167. case '_':
  168. pad = pad_space;
  169. ++f;
  170. break;
  171. case '-':
  172. pad = pad_none;
  173. ++f;
  174. break;
  175. default:
  176. pad = pad_zero;
  177. break;
  178. }
  179. /* Now do the specified format. */
  180. switch (*f)
  181. {
  182. case '\0':
  183. case '%':
  184. add(1, *p = *f);
  185. break;
  186. case 'a':
  187. cpy(aw_len, a_wkday);
  188. break;
  189. case 'A':
  190. cpy(wkday_len, f_wkday);
  191. break;
  192. case 'b':
  193. case 'h': /* GNU extension. */
  194. cpy(am_len, a_month);
  195. break;
  196. case 'B':
  197. cpy(month_len, f_month);
  198. break;
  199. case 'c':
  200. #ifdef _NL_CURRENT
  201. subfmt = _NL_CURRENT (LC_TIME, D_T_FMT);
  202. #else
  203. subfmt = "%a %b %d %H:%M:%S %Z %Y";
  204. #endif
  205. subformat:
  206. {
  207. size_t len = strftime (p, maxsize - i, subfmt, tp);
  208. if (len == 0 && *subfmt)
  209. return 0;
  210. add(len, );
  211. }
  212. break;
  213. #define DO_NUMBER(digits, value) \
  214. maxdigits = digits; number_value = value; goto do_number
  215. #define DO_NUMBER_NOPAD(digits, value) \
  216. maxdigits = digits; number_value = value; goto do_number_nopad
  217. case 'C':
  218. DO_NUMBER (2, (1900 + tp->tm_year) / 100);
  219. case 'x':
  220. #ifdef _NL_CURRENT
  221. subfmt = _NL_CURRENT (LC_TIME, D_FMT);
  222. goto subformat;
  223. #endif
  224. /* Fall through. */
  225. case 'D': /* GNU extension. */
  226. subfmt = "%m/%d/%y";
  227. goto subformat;
  228. case 'd':
  229. DO_NUMBER (2, tp->tm_mday);
  230. case 'e': /* GNU extension: %d, but blank-padded. */
  231. #if 0
  232. DO_NUMBER_NOPAD (2, tp->tm_mday);
  233. #else
  234. DO_NUMBER (2, tp->tm_mday);
  235. #endif
  236. /* All numeric formats set MAXDIGITS and NUMBER_VALUE and then
  237. jump to one of these two labels. */
  238. do_number_nopad:
  239. /* Force `-' flag. */
  240. pad = pad_none;
  241. do_number:
  242. {
  243. /* Format the number according to the PAD flag. */
  244. register char *nf = &number_fmt[1];
  245. int printed;
  246. switch (pad)
  247. {
  248. case pad_zero:
  249. *nf++ = '0';
  250. case pad_space:
  251. *nf++ = '0' + maxdigits;
  252. case pad_none:
  253. *nf++ = 'u';
  254. *nf = '\0';
  255. }
  256. #ifdef _LIBC
  257. if (i + maxdigits >= maxsize)
  258. return 0;
  259. printed = sprintf (p, number_fmt, number_value);
  260. i += printed;
  261. p += printed;
  262. #else
  263. add (maxdigits, sprintf (p, number_fmt, number_value);
  264. printed = strlen (p));
  265. #endif
  266. break;
  267. }
  268. case 'H':
  269. DO_NUMBER (2, tp->tm_hour);
  270. case 'I':
  271. DO_NUMBER (2, hour12);
  272. case 'k': /* GNU extension. */
  273. DO_NUMBER_NOPAD (2, tp->tm_hour);
  274. case 'l': /* GNU extension. */
  275. DO_NUMBER_NOPAD (2, hour12);
  276. case 'j':
  277. DO_NUMBER (3, 1 + tp->tm_yday);
  278. case 'M':
  279. DO_NUMBER (2, tp->tm_min);
  280. case 'm':
  281. DO_NUMBER (2, tp->tm_mon + 1);
  282. case 'n': /* GNU extension. */
  283. add (1, *p = '\n');
  284. break;
  285. case 'p':
  286. cpy(ap_len, ampm);
  287. break;
  288. case 'R': /* GNU extension. */
  289. subfmt = "%H:%M";
  290. goto subformat;
  291. case 'r': /* GNU extension. */
  292. subfmt = "%I:%M:%S %p";
  293. goto subformat;
  294. case 'S':
  295. DO_NUMBER (2, tp->tm_sec);
  296. case 'X':
  297. #ifdef _NL_CURRENT
  298. subfmt = _NL_CURRENT (LC_TIME, T_FMT);
  299. goto subformat;
  300. #endif
  301. /* Fall through. */
  302. case 'T': /* GNU extenstion. */
  303. subfmt = "%H:%M:%S";
  304. goto subformat;
  305. case 't': /* GNU extenstion. */
  306. add (1, *p = '\t');
  307. break;
  308. case 'U':
  309. DO_NUMBER (2, y_week0);
  310. case 'V':
  311. DO_NUMBER (2, y_week2);
  312. case 'W':
  313. DO_NUMBER (2, y_week1);
  314. case 'w':
  315. DO_NUMBER (1, tp->tm_wday);
  316. case 'Y':
  317. DO_NUMBER (4, 1900 + tp->tm_year);
  318. case 'y':
  319. DO_NUMBER (2, tp->tm_year % 100);
  320. case 'Z':
  321. cpy(zonelen, zone);
  322. break;
  323. default:
  324. /* Bad format. */
  325. break;
  326. }
  327. }
  328. if (p)
  329. *p = '\0';
  330. return i;
  331. }