[bwm-tools-devel] COMMIT - r18 - trunk/lib
Brought to you by:
nkukard
From: SVN C. <sv...@li...> - 2005-01-02 15:46:08
|
Author: nkukard Date: 2005-01-02 17:45:54 +0200 (Sun, 02 Jan 2005) New Revision: 18 Modified: trunk/lib/misc.c Log: * Giang Hu <fre...@gm...> - Reworked parseDateTime function, the old one was a bit insane Modified: trunk/lib/misc.c =================================================================== --- trunk/lib/misc.c 2005-01-02 15:32:38 UTC (rev 17) +++ trunk/lib/misc.c 2005-01-02 15:45:54 UTC (rev 18) @@ -51,121 +51,34 @@ // Convert datetime format into unix time format int parseDateTime(char *dateTime) { - char *sday, *smonth, *syear, *shour, *sminute, *ssecond; int day = 0, month = 0, year = 0, hour = 0, minute = 0, second = 0; struct tm time_p; int ret; - - - // Convert all values we need - sday = dateTime; - if (!sday) - { - fprintf(stderr,"ERROR: Failed to find \"day\" in date specified - %s",dateTime); - return -1; - } - - smonth = strchr(sday,'/'); - if (!smonth) - { - fprintf(stderr,"ERROR: Failed to find \"month\" in date specified - %s",dateTime); - return -1; - } - smonth++; + - // Now we have the next marker, we can process the prev variable - day = aptrtoi(sday,smonth); - - syear = strchr(smonth,'/'); - if (!syear) - { - fprintf(stderr,"ERROR: Failed to find \"year\" in date specified - %s",dateTime); - return -1; - } - syear++; + // Scan datetime provided and get our variables + sscanf(dateTime, "%d/%d/%d %d:%d:%d", &day, &month, &year, &hour, + &minute, &second); - // And so forth - month = aptrtoi(smonth,syear); - - // Here we have a shorter version, we don't really need hour/min/sec - shour = strchr(syear,' '); - if (shour) - shour++; - - year = aptrtoi(syear,shour); - - - // Check if we have an hour - if (shour) + // Check if we have an acceptable number of variables scanned + if ( ret == 3 || ret == 5 || ret == 6 ) { - // Same for minutes - sminute = strchr(shour,':'); - if (sminute) - sminute++; - hour = aptrtoi(shour,sminute); - - // And seconds - if (sminute) - { - ssecond = strchr(sminute,':'); - if (ssecond) - ssecond++; - - minute = aptrtoi(sminute,ssecond); - - second = aptrtoi(ssecond,NULL); - } + time_p.tm_mday = day; + time_p.tm_mon = month - 1; // Months since Jan + time_p.tm_year = year - 1900; // Years since 1900 + time_p.tm_hour = hour; + time_p.tm_min = minute; + time_p.tm_sec = second; + ret = mktime(&time_p); } + else + ret = -1; - - // Check all is ok... - if (day <= 0 || day > 31) - { - fprintf(stderr,"Failed to get DAY: %i\n",day); - return -1; - } + // If we encountered an error.. complain + if (ret == -1) + fprintf(stderr,"ERROR: Failed to convert %s to date time",dateTime); - if (month <= 0 || month > 12) - { - fprintf(stderr,"Failed to get MONTH: %i\n",month); - return -1; - } - - if (year <= 0) - { - fprintf(stderr,"Failed to get YEAR: %i\n",year); - return -1; - } - - if (hour < 0 || hour > 23) - { - fprintf(stderr,"Failed to get HOUR: %i\n",hour); - return -1; - } - - if (minute < 0 || minute > 59) - { - fprintf(stderr,"Failed to get MINUTE: %i\n",minute); - return -1; - } - - if (second < 0 || second > 59) - { - fprintf(stderr,"Failed to get SECOND: %i\n",second); - return -1; - } - - // Setup our struct - time_p.tm_mday = day; - time_p.tm_mon = month - 1; // Months since Jan - time_p.tm_year = year - 1900; // Years since 1900 - time_p.tm_hour = hour; - time_p.tm_min = minute; - time_p.tm_sec = second; - - // Return our time construction - ret = mktime(&time_p); - return(ret); + return ret; } |