strftime.c 9.3 KB

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