strftime.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <langinfo.h>
  5. #include <locale.h>
  6. #include <time.h>
  7. #include <limits.h>
  8. #include "libc.h"
  9. #include "time_impl.h"
  10. const char *__nl_langinfo_l(nl_item, locale_t);
  11. static int is_leap(int y)
  12. {
  13. /* Avoid overflow */
  14. if (y>INT_MAX-1900) y -= 2000;
  15. y += 1900;
  16. return !(y%4) && ((y%100) || !(y%400));
  17. }
  18. static int week_num(const struct tm *tm)
  19. {
  20. int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
  21. /* If 1 Jan is just 1-3 days past Monday,
  22. * the previous week is also in this year. */
  23. if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
  24. val++;
  25. if (!val) {
  26. val = 52;
  27. /* If 31 December of prev year a Thursday,
  28. * or Friday of a leap year, then the
  29. * prev year has 53 weeks. */
  30. int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
  31. if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
  32. val++;
  33. } else if (val == 53) {
  34. /* If 1 January is not a Thursday, and not
  35. * a Wednesday of a leap year, then this
  36. * year has only 52 weeks. */
  37. int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
  38. if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
  39. val = 1;
  40. }
  41. return val;
  42. }
  43. const char *__tm_to_tzname(const struct tm *);
  44. size_t __strftime_l(char *restrict, size_t, const char *restrict, const struct tm *restrict, locale_t);
  45. const char *__strftime_fmt_1(char (*s)[100], size_t *l, int f, const struct tm *tm, locale_t loc)
  46. {
  47. nl_item item;
  48. long long val;
  49. const char *fmt;
  50. int width = 2;
  51. switch (f) {
  52. case 'a':
  53. item = ABDAY_1 + tm->tm_wday;
  54. goto nl_strcat;
  55. case 'A':
  56. item = DAY_1 + tm->tm_wday;
  57. goto nl_strcat;
  58. case 'h':
  59. case 'b':
  60. item = ABMON_1 + tm->tm_mon;
  61. goto nl_strcat;
  62. case 'B':
  63. item = MON_1 + tm->tm_mon;
  64. goto nl_strcat;
  65. case 'c':
  66. item = D_T_FMT;
  67. goto nl_strftime;
  68. case 'C':
  69. val = (1900LL+tm->tm_year) / 100;
  70. goto number;
  71. case 'd':
  72. val = tm->tm_mday;
  73. goto number;
  74. case 'D':
  75. fmt = "%m/%d/%y";
  76. goto recu_strftime;
  77. case 'e':
  78. *l = snprintf(*s, sizeof *s, "%2d", tm->tm_mday);
  79. return *s;
  80. case 'F':
  81. fmt = "%Y-%m-%d";
  82. goto recu_strftime;
  83. case 'g':
  84. case 'G':
  85. val = tm->tm_year + 1900LL;
  86. if (tm->tm_yday < 3 && week_num(tm) != 1) val--;
  87. else if (tm->tm_yday > 360 && week_num(tm) == 1) val++;
  88. if (f=='g') val %= 100;
  89. else width = 4;
  90. goto number;
  91. case 'H':
  92. val = tm->tm_hour;
  93. goto number;
  94. case 'I':
  95. val = tm->tm_hour;
  96. if (!val) val = 12;
  97. else if (val > 12) val -= 12;
  98. goto number;
  99. case 'j':
  100. val = tm->tm_yday+1;
  101. width = 3;
  102. goto number;
  103. case 'm':
  104. val = tm->tm_mon+1;
  105. goto number;
  106. case 'M':
  107. val = tm->tm_min;
  108. goto number;
  109. case 'n':
  110. *l = 1;
  111. return "\n";
  112. case 'p':
  113. item = tm->tm_hour >= 12 ? PM_STR : AM_STR;
  114. goto nl_strcat;
  115. case 'r':
  116. item = T_FMT_AMPM;
  117. goto nl_strftime;
  118. case 'R':
  119. fmt = "%H:%M";
  120. goto recu_strftime;
  121. case 's':
  122. val = __tm_to_secs(tm) + tm->__tm_gmtoff;
  123. width = 1;
  124. goto number;
  125. case 'S':
  126. val = tm->tm_sec;
  127. goto number;
  128. case 't':
  129. *l = 1;
  130. return "\t";
  131. case 'T':
  132. fmt = "%H:%M:%S";
  133. goto recu_strftime;
  134. case 'u':
  135. val = tm->tm_wday ? tm->tm_wday : 7;
  136. width = 1;
  137. goto number;
  138. case 'U':
  139. val = (tm->tm_yday + 7 - tm->tm_wday) / 7;
  140. goto number;
  141. case 'W':
  142. val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
  143. goto number;
  144. case 'V':
  145. val = week_num(tm);
  146. goto number;
  147. case 'w':
  148. val = tm->tm_wday;
  149. width = 1;
  150. goto number;
  151. case 'x':
  152. item = D_FMT;
  153. goto nl_strftime;
  154. case 'X':
  155. item = T_FMT;
  156. goto nl_strftime;
  157. case 'y':
  158. val = tm->tm_year % 100;
  159. goto number;
  160. case 'Y':
  161. val = tm->tm_year + 1900;
  162. if (val >= 10000) {
  163. *l = snprintf(*s, sizeof *s, "+%lld", val);
  164. return *s;
  165. }
  166. width = 4;
  167. goto number;
  168. case 'z':
  169. if (tm->tm_isdst < 0) {
  170. *l = 0;
  171. return "";
  172. }
  173. *l = snprintf(*s, sizeof *s, "%+.2d%.2d",
  174. (-tm->__tm_gmtoff)/3600,
  175. abs(tm->__tm_gmtoff%3600)/60);
  176. return *s;
  177. case 'Z':
  178. if (tm->tm_isdst < 0) {
  179. *l = 0;
  180. return "";
  181. }
  182. fmt = __tm_to_tzname(tm);
  183. goto string;
  184. case '%':
  185. *l = 1;
  186. return "%";
  187. default:
  188. return 0;
  189. }
  190. number:
  191. *l = snprintf(*s, sizeof *s, "%0*lld", width, val);
  192. return *s;
  193. nl_strcat:
  194. fmt = __nl_langinfo_l(item, loc);
  195. string:
  196. *l = strlen(fmt);
  197. return fmt;
  198. nl_strftime:
  199. fmt = __nl_langinfo_l(item, loc);
  200. recu_strftime:
  201. *l = __strftime_l(*s, sizeof *s, fmt, tm, loc);
  202. if (!*l) return 0;
  203. return *s;
  204. }
  205. size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc)
  206. {
  207. size_t l, k;
  208. char buf[100];
  209. char *p;
  210. const char *t;
  211. int plus;
  212. unsigned long width;
  213. for (l=0; l<n; f++) {
  214. if (!*f) {
  215. s[l] = 0;
  216. return l;
  217. }
  218. if (*f != '%') {
  219. s[l++] = *f;
  220. continue;
  221. }
  222. f++;
  223. if ((plus = (*f == '+'))) f++;
  224. width = strtoul(f, &p, 10);
  225. if (*p == 'C' || *p == 'F' || *p == 'G' || *p == 'Y') {
  226. if (!width && p!=f) width = 1;
  227. } else {
  228. width = 0;
  229. }
  230. f = p;
  231. if (*f == 'E' || *f == 'O') f++;
  232. t = __strftime_fmt_1(&buf, &k, *f, tm, loc);
  233. if (!t) break;
  234. if (width) {
  235. for (; *t=='+' || *t=='-' || (*t=='0'&&t[1]); t++, k--);
  236. width--;
  237. if (plus && tm->tm_year >= 10000-1900)
  238. s[l++] = '+';
  239. else if (tm->tm_year < -1900)
  240. s[l++] = '-';
  241. else
  242. width++;
  243. for (; width > k && l < n; width--)
  244. s[l++] = '0';
  245. }
  246. if (k > n-l) k = n-l;
  247. memcpy(s+l, t, k);
  248. l += k;
  249. }
  250. if (n) {
  251. if (l==n) l=n-1;
  252. s[l] = 0;
  253. }
  254. return 0;
  255. }
  256. size_t strftime(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm)
  257. {
  258. return __strftime_l(s, n, f, tm, 0);
  259. }
  260. weak_alias(__strftime_l, strftime_l);