strptime.c 3.4 KB

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