[OpenAFS-devel] Re: [OpenAFS] vos: failed to parse date...

Jim Rees rees@umich.edu
Fri, 31 Mar 2006 09:49:24 -0500


Ok, how about this?  I haven't tried to fix any of the bugs in the old
method.  I improved the checks for invalid input and added an optional
method for seconds since epoch (I forget who requested this).

Index: ktime.c
===================================================================
RCS file: /cvs/openafs/src/util/ktime.c,v
retrieving revision 1.11
diff -u -r1.11 ktime.c
--- ktime.c	8 Sep 2004 21:35:53 -0000	1.11
+++ ktime.c	31 Mar 2006 14:47:31 -0000
@@ -517,6 +517,7 @@
     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 @@
 		   &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,29 @@
 ktime_DateToInt32(char *adate, afs_int32 * aint32)
 {
     struct ktime_date tdate;
-    register afs_int32 code;
+    afs_int32 code;
+    long l;
+    char c[2];
+
+    if (sscanf(adate, "%ld%1s", &l, c) == 1)
+	*aint32 = l;
+    else {
+	/* parse the date into a ktime_date structure */
+	code = ktime_ParseDate(adate, &tdate);
+	if (code)
+	    return code;		/* failed to parse */
+	code = ktime_InterpretDate(&tdate);	/* interpret as seconds since 1970 */
+	*aint32 = code;		/* return it */
+    }
 
-    /* parse the date into a ktime_date structure */
-    code = ktime_ParseDate(adate, &tdate);
-    if (code)
-	return code;		/* failed to parse */
-
-    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";
 }