strptime.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 1994 Powerdog Industries. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in the documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. All advertising materials mentioning features or use of this
  14. * software must display the following acknowledgement:
  15. * This product includes software developed by Powerdog Industries.
  16. * 4. The name of Powerdog Industries may not be used to endorse or
  17. * promote products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  27. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  29. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "precompile.h"
  33. #include "strptime.h"
  34. #define asizeof(a) (sizeof (a) / sizeof ((a)[0]))
  35. struct dtconv {
  36. char *abbrev_month_names[12];
  37. char *month_names[12];
  38. char *abbrev_weekday_names[7];
  39. char *weekday_names[7];
  40. char *time_format;
  41. char *sdate_format;
  42. char *dtime_format;
  43. char *am_string;
  44. char *pm_string;
  45. char *ldate_format;
  46. };
  47. static const struct dtconv En_US = {
  48. { "jan", "feb", "mar", "apr", "may", "jun",
  49. "jul", "aug", "sep", "oct", "nov", "dec" },
  50. { "january", "february", "march", "april",
  51. "may", "june", "july", "august",
  52. "september", "october", "november", "december" },
  53. { "sun", "mon", "tue", "wed", "thu", "fri", "sat" },
  54. { "sunday", "monday", "tuesday", "wednesday",
  55. "thursday", "friday", "saturday" },
  56. "%H:%M:%S",
  57. "%m/%d/%y",
  58. "%a %b %e %T %Z %Y",
  59. "AM",
  60. "PM",
  61. "%A, %B, %e, %Y"
  62. };
  63. static void lowercase_string(char *buffer) {
  64. while(*buffer) {
  65. *buffer = tolower(*buffer);
  66. buffer++;
  67. }
  68. }
  69. TOOLKIT_API char *strptime(char *buf, char *fmt, struct tm *tm) {
  70. char c,
  71. *ptr;
  72. int i, j,
  73. len;
  74. ptr = fmt;
  75. while (*ptr != 0) {
  76. if (*buf == 0)
  77. break;
  78. c = *ptr++;
  79. if (c != '%') {
  80. if (isspace(c))
  81. while (*buf != 0 && isspace(*buf))
  82. buf++;
  83. else if (c != *buf++)
  84. return 0;
  85. continue;
  86. }
  87. c = *ptr++;
  88. switch (c) {
  89. case 0:
  90. case '%':
  91. if (*buf++ != '%')
  92. return 0;
  93. break;
  94. case 'C':
  95. buf = strptime(buf, En_US.ldate_format, tm);
  96. if (buf == 0)
  97. return 0;
  98. break;
  99. case 'c':
  100. buf = strptime(buf, "%x %X", tm);
  101. if (buf == 0)
  102. return 0;
  103. break;
  104. case 'D':
  105. buf = strptime(buf, "%m/%d/%y", tm);
  106. if (buf == 0)
  107. return 0;
  108. break;
  109. case 'R':
  110. buf = strptime(buf, "%H:%M", tm);
  111. if (buf == 0)
  112. return 0;
  113. break;
  114. case 'r':
  115. buf = strptime(buf, "%I:%M:%S %p", tm);
  116. if (buf == 0)
  117. return 0;
  118. break;
  119. case 'T':
  120. buf = strptime(buf, "%H:%M:%S", tm);
  121. if (buf == 0)
  122. return 0;
  123. break;
  124. case 'X':
  125. buf = strptime(buf, En_US.time_format, tm);
  126. if (buf == 0)
  127. return 0;
  128. break;
  129. case 'x':
  130. buf = strptime(buf, En_US.sdate_format, tm);
  131. if (buf == 0)
  132. return 0;
  133. break;
  134. case 'j':
  135. if (!isdigit(*buf))
  136. return 0;
  137. for (i = 0; *buf != 0 && isdigit(*buf); buf++) {
  138. i *= 10;
  139. i += *buf - '0';
  140. }
  141. if (i > 365)
  142. return 0;
  143. tm->tm_yday = i;
  144. break;
  145. case 'M':
  146. case 'S':
  147. if (*buf == 0 || isspace(*buf))
  148. break;
  149. if (!isdigit(*buf))
  150. return 0;
  151. for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
  152. i *= 10;
  153. i += *buf - '0';
  154. }
  155. if (i > 59)
  156. return 0;
  157. if (c == 'M')
  158. tm->tm_min = i;
  159. else
  160. tm->tm_sec = i;
  161. if (*buf != 0 && isspace(*buf))
  162. while (*ptr != 0 && !isspace(*ptr))
  163. ptr++;
  164. break;
  165. case 'H':
  166. case 'I':
  167. case 'k':
  168. case 'l':
  169. if (!isdigit(*buf))
  170. return 0;
  171. for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
  172. i *= 10;
  173. i += *buf - '0';
  174. }
  175. if (c == 'H' || c == 'k') {
  176. if (i > 23)
  177. return 0;
  178. } else if (i > 11)
  179. return 0;
  180. tm->tm_hour = i;
  181. if (*buf != 0 && isspace(*buf))
  182. while (*ptr != 0 && !isspace(*ptr))
  183. ptr++;
  184. break;
  185. case 'p':
  186. len = (int) strlen(En_US.am_string);
  187. lowercase_string( buf );
  188. if (strncmp(buf, En_US.am_string, len) == 0) {
  189. if (tm->tm_hour > 12)
  190. return 0;
  191. if (tm->tm_hour == 12)
  192. tm->tm_hour = 0;
  193. buf += len;
  194. break;
  195. }
  196. len = (int) strlen(En_US.pm_string);
  197. if (strncmp(buf, En_US.pm_string, len) == 0) {
  198. if (tm->tm_hour > 12)
  199. return 0;
  200. if (tm->tm_hour != 12)
  201. tm->tm_hour += 12;
  202. buf += len;
  203. break;
  204. }
  205. return 0;
  206. case 'A':
  207. case 'a':
  208. for (i = 0; i < asizeof(En_US.weekday_names); i++) {
  209. len = (int) strlen(En_US.weekday_names[i]);
  210. lowercase_string( buf );
  211. if (strncmp(buf,
  212. En_US.weekday_names[i],
  213. len) == 0)
  214. break;
  215. len = (int) strlen(En_US.abbrev_weekday_names[i]);
  216. if (strncmp(buf,
  217. En_US.abbrev_weekday_names[i],
  218. len) == 0)
  219. break;
  220. }
  221. if (i == asizeof(En_US.weekday_names))
  222. return 0;
  223. tm->tm_wday = i;
  224. buf += len;
  225. break;
  226. case 'd':
  227. case 'e':
  228. if (!isdigit(*buf))
  229. return 0;
  230. for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
  231. i *= 10;
  232. i += *buf - '0';
  233. }
  234. if (i > 31)
  235. return 0;
  236. tm->tm_mday = i;
  237. if (*buf != 0 && isspace(*buf))
  238. while (*ptr != 0 && !isspace(*ptr))
  239. ptr++;
  240. break;
  241. case 'B':
  242. case 'b':
  243. case 'h':
  244. for (i = 0; i < asizeof(En_US.month_names); i++) {
  245. len = (int) strlen(En_US.month_names[i]);
  246. lowercase_string( buf );
  247. if (strncmp(buf, En_US.month_names[i],len) == 0)
  248. break;
  249. len = (int) strlen(En_US.abbrev_month_names[i]);
  250. if (strncmp(buf,
  251. En_US.abbrev_month_names[i],
  252. len) == 0)
  253. break;
  254. }
  255. if (i == asizeof(En_US.month_names))
  256. return 0;
  257. tm->tm_mon = i;
  258. buf += len;
  259. break;
  260. case 'm':
  261. if (!isdigit(*buf))
  262. return 0;
  263. for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
  264. i *= 10;
  265. i += *buf - '0';
  266. }
  267. if (i < 1 || i > 12)
  268. return 0;
  269. tm->tm_mon = i - 1;
  270. if (*buf != 0 && isspace(*buf))
  271. while (*ptr != 0 && !isspace(*ptr))
  272. ptr++;
  273. break;
  274. case 'Y':
  275. case 'y':
  276. if (*buf == 0 || isspace(*buf))
  277. break;
  278. if (!isdigit(*buf))
  279. return 0;
  280. for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<((c=='Y')?4:2); j++,buf++) {
  281. i *= 10;
  282. i += *buf - '0';
  283. }
  284. if (c == 'Y')
  285. i -= 1900;
  286. else if (i < 69) /*c=='y', 00-68 is for 20xx, the rest is for 19xx*/
  287. i += 100;
  288. if (i < 0)
  289. return 0;
  290. tm->tm_year = i;
  291. if (*buf != 0 && isspace(*buf))
  292. while (*ptr != 0 && !isspace(*ptr))
  293. ptr++;
  294. break;
  295. }
  296. }
  297. return buf;
  298. }