| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- /*
- * Copyright (c) 1994 Powerdog Industries. All rights reserved.
- *
- * Redistribution and use in source and binary forms, without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgement:
- * This product includes software developed by Powerdog Industries.
- * 4. The name of Powerdog Industries may not be used to endorse or
- * promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY POWERDOG INDUSTRIES ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE POWERDOG INDUSTRIES BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "precompile.h"
- #include "strptime.h"
- #define asizeof(a) (sizeof (a) / sizeof ((a)[0]))
- struct dtconv {
- char *abbrev_month_names[12];
- char *month_names[12];
- char *abbrev_weekday_names[7];
- char *weekday_names[7];
- char *time_format;
- char *sdate_format;
- char *dtime_format;
- char *am_string;
- char *pm_string;
- char *ldate_format;
- };
- static const struct dtconv En_US = {
- { "jan", "feb", "mar", "apr", "may", "jun",
- "jul", "aug", "sep", "oct", "nov", "dec" },
- { "january", "february", "march", "april",
- "may", "june", "july", "august",
- "september", "october", "november", "december" },
- { "sun", "mon", "tue", "wed", "thu", "fri", "sat" },
- { "sunday", "monday", "tuesday", "wednesday",
- "thursday", "friday", "saturday" },
- "%H:%M:%S",
- "%m/%d/%y",
- "%a %b %e %T %Z %Y",
- "AM",
- "PM",
- "%A, %B, %e, %Y"
- };
- static void lowercase_string(char *buffer) {
- while(*buffer) {
- *buffer = tolower(*buffer);
- buffer++;
- }
- }
- TOOLKIT_API char *strptime(char *buf, char *fmt, struct tm *tm) {
- char c,
- *ptr;
- int i, j,
- len;
- ptr = fmt;
- while (*ptr != 0) {
- if (*buf == 0)
- break;
- c = *ptr++;
- if (c != '%') {
- if (isspace(c))
- while (*buf != 0 && isspace(*buf))
- buf++;
- else if (c != *buf++)
- return 0;
- continue;
- }
- c = *ptr++;
- switch (c) {
- case 0:
- case '%':
- if (*buf++ != '%')
- return 0;
- break;
- case 'C':
- buf = strptime(buf, En_US.ldate_format, tm);
- if (buf == 0)
- return 0;
- break;
- case 'c':
- buf = strptime(buf, "%x %X", tm);
- if (buf == 0)
- return 0;
- break;
- case 'D':
- buf = strptime(buf, "%m/%d/%y", tm);
- if (buf == 0)
- return 0;
- break;
- case 'R':
- buf = strptime(buf, "%H:%M", tm);
- if (buf == 0)
- return 0;
- break;
- case 'r':
- buf = strptime(buf, "%I:%M:%S %p", tm);
- if (buf == 0)
- return 0;
- break;
- case 'T':
- buf = strptime(buf, "%H:%M:%S", tm);
- if (buf == 0)
- return 0;
- break;
- case 'X':
- buf = strptime(buf, En_US.time_format, tm);
- if (buf == 0)
- return 0;
- break;
- case 'x':
- buf = strptime(buf, En_US.sdate_format, tm);
- if (buf == 0)
- return 0;
- break;
- case 'j':
- if (!isdigit(*buf))
- return 0;
- for (i = 0; *buf != 0 && isdigit(*buf); buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (i > 365)
- return 0;
- tm->tm_yday = i;
- break;
- case 'M':
- case 'S':
- if (*buf == 0 || isspace(*buf))
- break;
- if (!isdigit(*buf))
- return 0;
- for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (i > 59)
- return 0;
- if (c == 'M')
- tm->tm_min = i;
- else
- tm->tm_sec = i;
- if (*buf != 0 && isspace(*buf))
- while (*ptr != 0 && !isspace(*ptr))
- ptr++;
- break;
- case 'H':
- case 'I':
- case 'k':
- case 'l':
- if (!isdigit(*buf))
- return 0;
- for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (c == 'H' || c == 'k') {
- if (i > 23)
- return 0;
- } else if (i > 11)
- return 0;
- tm->tm_hour = i;
- if (*buf != 0 && isspace(*buf))
- while (*ptr != 0 && !isspace(*ptr))
- ptr++;
- break;
- case 'p':
- len = (int) strlen(En_US.am_string);
- lowercase_string( buf );
- if (strncmp(buf, En_US.am_string, len) == 0) {
- if (tm->tm_hour > 12)
- return 0;
- if (tm->tm_hour == 12)
- tm->tm_hour = 0;
- buf += len;
- break;
- }
- len = (int) strlen(En_US.pm_string);
- if (strncmp(buf, En_US.pm_string, len) == 0) {
- if (tm->tm_hour > 12)
- return 0;
- if (tm->tm_hour != 12)
- tm->tm_hour += 12;
- buf += len;
- break;
- }
- return 0;
- case 'A':
- case 'a':
- for (i = 0; i < asizeof(En_US.weekday_names); i++) {
- len = (int) strlen(En_US.weekday_names[i]);
- lowercase_string( buf );
- if (strncmp(buf,
- En_US.weekday_names[i],
- len) == 0)
- break;
- len = (int) strlen(En_US.abbrev_weekday_names[i]);
- if (strncmp(buf,
- En_US.abbrev_weekday_names[i],
- len) == 0)
- break;
- }
- if (i == asizeof(En_US.weekday_names))
- return 0;
- tm->tm_wday = i;
- buf += len;
- break;
- case 'd':
- case 'e':
- if (!isdigit(*buf))
- return 0;
- for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (i > 31)
- return 0;
- tm->tm_mday = i;
- if (*buf != 0 && isspace(*buf))
- while (*ptr != 0 && !isspace(*ptr))
- ptr++;
- break;
- case 'B':
- case 'b':
- case 'h':
- for (i = 0; i < asizeof(En_US.month_names); i++) {
- len = (int) strlen(En_US.month_names[i]);
- lowercase_string( buf );
- if (strncmp(buf, En_US.month_names[i],len) == 0)
- break;
- len = (int) strlen(En_US.abbrev_month_names[i]);
- if (strncmp(buf,
- En_US.abbrev_month_names[i],
- len) == 0)
- break;
- }
- if (i == asizeof(En_US.month_names))
- return 0;
- tm->tm_mon = i;
- buf += len;
- break;
- case 'm':
- if (!isdigit(*buf))
- return 0;
- for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<2; j++,buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (i < 1 || i > 12)
- return 0;
- tm->tm_mon = i - 1;
- if (*buf != 0 && isspace(*buf))
- while (*ptr != 0 && !isspace(*ptr))
- ptr++;
- break;
- case 'Y':
- case 'y':
- if (*buf == 0 || isspace(*buf))
- break;
- if (!isdigit(*buf))
- return 0;
- for (j=0,i = 0; *buf != 0 && isdigit(*buf) && j<((c=='Y')?4:2); j++,buf++) {
- i *= 10;
- i += *buf - '0';
- }
- if (c == 'Y')
- i -= 1900;
- else if (i < 69) /*c=='y', 00-68 is for 20xx, the rest is for 19xx*/
- i += 100;
- if (i < 0)
- return 0;
- tm->tm_year = i;
- if (*buf != 0 && isspace(*buf))
- while (*ptr != 0 && !isspace(*ptr))
- ptr++;
- break;
- }
- }
- return buf;
- }
|