From 14d6771b9ddb413d2e870c57a4ade070b5b045f0 Mon Sep 17 00:00:00 2001 From: Jim Rees Date: Mon, 3 Apr 2006 17:22:30 +0000 Subject: [PATCH] iso-8601-20060403 Parse a pitiful subset of ISO 8601 in date/time strings --- src/util/ktime.c | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/util/ktime.c b/src/util/ktime.c index f113b4f01..cf082f0e0 100644 --- a/src/util/ktime.c +++ b/src/util/ktime.c @@ -517,6 +517,7 @@ ktime_ParseDate(char *adate, struct ktime_date *akdate) if (akdate->mask) return 0; + /* Old ambiguous mm/dd/yy hh:mm:ss format */ code = sscanf(adate, "%d / %d / %d %d : %d : %d%1s", &month, &day2, &year, @@ -528,13 +529,28 @@ ktime_ParseDate(char *adate, struct ktime_date *akdate) &hour, &min, &c[0]); if (code != 5) { hour = min = 0; - code = sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]); + code = + sscanf(adate, "%d / %d / %d%1s", &month, &day2, &year, &c[0]); if (code != 3) { - return -1; + code = -1; } } } + /* New ISO 8601 (subset) format */ + + if (code < 0) { + hour = min = sec = 0; + code = + sscanf(adate, "%d-%d-%d %d:%d:%d%1s", &year, &month, &day2, + &hour, &min, &sec, c); + if (code != 3 && code != 5 && code != 6) + code = -1; + } + + if (code < 0) + return code; + if ((year < 0) || (month < 1) || (month > 12) || (day2 < 1) || (day2 > 31) || /* more or less */ (hour < 0) || (hour > 23) || (min < 0) || (min > 59) || (sec < 0) || (sec > 59)) @@ -575,23 +591,28 @@ afs_int32 ktime_DateToInt32(char *adate, afs_int32 * aint32) { struct ktime_date tdate; - register afs_int32 code; + afs_int32 code; + unsigned long l; + char c[2]; - /* parse the date into a ktime_date structure */ - code = ktime_ParseDate(adate, &tdate); - if (code) - return code; /* failed to parse */ + if (sscanf(adate, "%lu%1s", &l, c) == 1 && l > 200000000) + *aint32 = l; + else { + /* parse the date into a ktime_date structure */ + code = ktime_ParseDate(adate, &tdate); + if (code) + return code; /* failed to parse */ + *aint32 = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */ + } - code = ktime_InterpretDate(&tdate); /* interpret as seconds since 1970 */ - *aint32 = code; /* return it */ - return 0; /* and declare no errors */ + return 0; } /* get useful error message to print about date input format */ char * ktime_GetDateUsage(void) { - return "date format is 'mm/dd/yy [hh:mm]', using a 24 hour clock"; + return "date format is '(yyyy-mm-dd | mm/dd/yy) [hh:mm]', using a 24 hour clock"; } -- 2.39.5