strptime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <stdlib.h>
  2. #include <langinfo.h>
  3. #include <time.h>
  4. #include <ctype.h>
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm)
  9. {
  10. int i, w, neg, adj, min, range, *dest, dummy;
  11. const char *ex;
  12. size_t len;
  13. int want_century = 0, century = 0;
  14. while (*f) {
  15. if (*f != '%') {
  16. if (isspace(*f)) for (; *s && isspace(*s); s++);
  17. else if (*s != *f) return 0;
  18. else s++;
  19. f++;
  20. continue;
  21. }
  22. f++;
  23. if (*f == '+') f++;
  24. if (isdigit(*f)) {
  25. char *new_f;
  26. w=strtoul(f, &new_f, 10);
  27. f = new_f;
  28. } else {
  29. w=-1;
  30. }
  31. adj=0;
  32. switch (*f++) {
  33. case 'a': case 'A':
  34. dest = &tm->tm_wday;
  35. min = ABDAY_1;
  36. range = 7;
  37. goto symbolic_range;
  38. case 'b': case 'B': case 'h':
  39. dest = &tm->tm_mon;
  40. min = ABMON_1;
  41. range = 12;
  42. goto symbolic_range;
  43. case 'c':
  44. s = strptime(s, nl_langinfo(D_T_FMT), tm);
  45. if (!s) return 0;
  46. break;
  47. case 'C':
  48. dest = &century;
  49. if (w<0) w=2;
  50. want_century |= 2;
  51. goto numeric_digits;
  52. case 'd': case 'e':
  53. dest = &tm->tm_mday;
  54. min = 1;
  55. range = 31;
  56. goto numeric_range;
  57. case 'D':
  58. s = strptime(s, "%m/%d/%y", tm);
  59. if (!s) return 0;
  60. break;
  61. case 'H':
  62. dest = &tm->tm_hour;
  63. min = 0;
  64. range = 24;
  65. goto numeric_range;
  66. case 'I':
  67. dest = &tm->tm_hour;
  68. min = 1;
  69. range = 12;
  70. goto numeric_range;
  71. case 'j':
  72. dest = &tm->tm_yday;
  73. min = 1;
  74. range = 366;
  75. adj = 1;
  76. goto numeric_range;
  77. case 'm':
  78. dest = &tm->tm_mon;
  79. min = 1;
  80. range = 12;
  81. adj = 1;
  82. goto numeric_range;
  83. case 'M':
  84. dest = &tm->tm_min;
  85. min = 0;
  86. range = 60;
  87. goto numeric_range;
  88. case 'n': case 't':
  89. for (; *s && isspace(*s); s++);
  90. break;
  91. case 'p':
  92. ex = nl_langinfo(AM_STR);
  93. len = strlen(ex);
  94. if (!strncasecmp(s, ex, len)) {
  95. tm->tm_hour %= 12;
  96. s += len;
  97. break;
  98. }
  99. ex = nl_langinfo(PM_STR);
  100. len = strlen(ex);
  101. if (!strncasecmp(s, ex, len)) {
  102. tm->tm_hour %= 12;
  103. tm->tm_hour += 12;
  104. s += len;
  105. break;
  106. }
  107. return 0;
  108. case 'r':
  109. s = strptime(s, nl_langinfo(T_FMT_AMPM), tm);
  110. if (!s) return 0;
  111. break;
  112. case 'R':
  113. s = strptime(s, "%H:%M", tm);
  114. if (!s) return 0;
  115. break;
  116. case 'S':
  117. dest = &tm->tm_sec;
  118. min = 0;
  119. range = 61;
  120. goto numeric_range;
  121. case 'T':
  122. s = strptime(s, "%H:%M:%S", tm);
  123. if (!s) return 0;
  124. break;
  125. case 'U':
  126. case 'W':
  127. /* Throw away result, for now. (FIXME?) */
  128. dest = &dummy;
  129. min = 0;
  130. range = 54;
  131. goto numeric_range;
  132. case 'w':
  133. dest = &tm->tm_wday;
  134. min = 0;
  135. range = 7;
  136. goto numeric_range;
  137. case 'x':
  138. s = strptime(s, nl_langinfo(D_FMT), tm);
  139. if (!s) return 0;
  140. break;
  141. case 'X':
  142. s = strptime(s, nl_langinfo(T_FMT), tm);
  143. if (!s) return 0;
  144. break;
  145. case 'y':
  146. dest = &tm->tm_year;
  147. w = 2;
  148. want_century |= 1;
  149. goto numeric_digits;
  150. case 'Y':
  151. dest = &tm->tm_year;
  152. if (w<0) w=4;
  153. adj = 1900;
  154. want_century = 0;
  155. goto numeric_digits;
  156. case '%':
  157. if (*s++ != '%') return 0;
  158. break;
  159. default:
  160. return 0;
  161. numeric_range:
  162. if (!isdigit(*s)) return 0;
  163. *dest = 0;
  164. for (i=1; i<=min+range && isdigit(*s); i*=10)
  165. *dest = *dest * 10 + *s++ - '0';
  166. if (*dest - min >= (unsigned)range) return 0;
  167. *dest -= adj;
  168. switch((char *)dest - (char *)tm) {
  169. case offsetof(struct tm, tm_yday):
  170. ;
  171. }
  172. goto update;
  173. numeric_digits:
  174. neg = 0;
  175. if (*s == '+') s++;
  176. else if (*s == '-') neg=1, s++;
  177. if (!isdigit(*s)) return 0;
  178. for (*dest=i=0; i<w && isdigit(*s); i++)
  179. *dest = *dest * 10 + *s++ - '0';
  180. if (neg) *dest = -*dest;
  181. *dest -= adj;
  182. goto update;
  183. symbolic_range:
  184. for (i=2*range-1; i>=0; i--) {
  185. ex = nl_langinfo(min+i);
  186. len = strlen(ex);
  187. if (strncasecmp(s, ex, len)) continue;
  188. s += len;
  189. *dest = i % range;
  190. break;
  191. }
  192. if (i<0) return 0;
  193. goto update;
  194. update:
  195. //FIXME
  196. ;
  197. }
  198. }
  199. if (want_century) {
  200. if (want_century & 2) tm->tm_year += century * 100 - 1900;
  201. else if (tm->tm_year <= 68) tm->tm_year += 100;
  202. }
  203. return (char *)s;
  204. }