svn commit: trunk/busybox: coreutils include testsuite/date

vda at busybox.net vda at busybox.net
Fri Apr 25 02:14:07 UTC 2008


Author: vda
Date: 2008-04-24 19:14:07 -0700 (Thu, 24 Apr 2008)
New Revision: 21853

Log:
date: make it accept ISO date format. Fix help text.
make testsuite actually test something useful.

function                                             old     new   delta
date_main                                           1094    1149     +55



Modified:
   trunk/busybox/coreutils/date.c
   trunk/busybox/include/usage.h
   trunk/busybox/testsuite/date/date-works


Changeset:
Modified: trunk/busybox/coreutils/date.c
===================================================================
--- trunk/busybox/coreutils/date.c	2008-04-24 19:37:11 UTC (rev 21852)
+++ trunk/busybox/coreutils/date.c	2008-04-25 02:14:07 UTC (rev 21853)
@@ -103,67 +103,67 @@
 		if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) {
 			if (strptime(date_str, fmt_str2dt, &tm_time) == NULL)
 				bb_error_msg_and_die(bb_msg_invalid_date, date_str);
-		} else if (strchr(date_str, ':') != NULL) {
-			/* Parse input and assign appropriately to tm_time */
+		} else {
+			char end = '\0';
+			const char *last_colon = strrchr(date_str, ':');
 
-			if (sscanf(date_str, "%d:%d:%d", &tm_time.tm_hour, &tm_time.tm_min,
-								 &tm_time.tm_sec) == 3) {
-				/* no adjustments needed */
-			} else if (sscanf(date_str, "%d:%d", &tm_time.tm_hour,
-										&tm_time.tm_min) == 2) {
-				/* no adjustments needed */
-			} else if (sscanf(date_str, "%d.%d-%d:%d:%d", &tm_time.tm_mon,
-								&tm_time.tm_mday, &tm_time.tm_hour,
-								&tm_time.tm_min, &tm_time.tm_sec) == 5) {
-				/* Adjust dates from 1-12 to 0-11 */
-				tm_time.tm_mon -= 1;
-			} else if (sscanf(date_str, "%d.%d-%d:%d", &tm_time.tm_mon,
-								&tm_time.tm_mday,
-								&tm_time.tm_hour, &tm_time.tm_min) == 4) {
-				/* Adjust dates from 1-12 to 0-11 */
-				tm_time.tm_mon -= 1;
-			} else if (sscanf(date_str, "%d.%d.%d-%d:%d:%d", &tm_time.tm_year,
+			if (last_colon != NULL) {
+				/* Parse input and assign appropriately to tm_time */
+
+				if (sscanf(date_str, "%u:%u%c",
+								&tm_time.tm_hour,
+								&tm_time.tm_min,
+								&end) >= 2) {
+					/* no adjustments needed */
+				} else if (sscanf(date_str, "%u.%u-%u:%u%c",
 								&tm_time.tm_mon, &tm_time.tm_mday,
 								&tm_time.tm_hour, &tm_time.tm_min,
-									&tm_time.tm_sec) == 6) {
-				tm_time.tm_year -= 1900;	/* Adjust years */
-				tm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
-			} else if (sscanf(date_str, "%d.%d.%d-%d:%d", &tm_time.tm_year,
+								&end) >= 4) {
+					/* Adjust dates from 1-12 to 0-11 */
+					tm_time.tm_mon -= 1;
+				} else if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &tm_time.tm_year,
 								&tm_time.tm_mon, &tm_time.tm_mday,
-								&tm_time.tm_hour, &tm_time.tm_min) == 5) {
-				tm_time.tm_year -= 1900;	/* Adjust years */
-				tm_time.tm_mon -= 1;	/* Adjust dates from 1-12 to 0-11 */
+								&tm_time.tm_hour, &tm_time.tm_min,
+								&end) >= 5) {
+					tm_time.tm_year -= 1900; /* Adjust years */
+					tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
+				} else if (sscanf(date_str, "%u-%u-%u %u:%u%c", &tm_time.tm_year,
+								&tm_time.tm_mon, &tm_time.tm_mday,
+								&tm_time.tm_hour, &tm_time.tm_min,
+								&end) >= 5) {
+					tm_time.tm_year -= 1900; /* Adjust years */
+					tm_time.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
+//TODO: coreutils 6.9 also accepts "YYYY-MM-DD HH" (no minutes)
+				} else {
+					bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+				}
+				if (end == ':') {
+					if (sscanf(last_colon + 1, "%u%c", &tm_time.tm_sec, &end) == 1)
+						end = '\0';
+					/* else end != NUL and we error out */
+				}
 			} else {
-				bb_error_msg_and_die(bb_msg_invalid_date, date_str);
-			}
-		} else {
-			int nr;
-			char *cp;
-
-			nr = sscanf(date_str, "%2d%2d%2d%2d%d", &tm_time.tm_mon,
-						&tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
-						&tm_time.tm_year);
-
-			if (nr < 4 || nr > 5) {
-				bb_error_msg_and_die(bb_msg_invalid_date, date_str);
-			}
-
-			cp = strchr(date_str, '.');
-			if (cp) {
-				nr = sscanf(cp + 1, "%2d", &tm_time.tm_sec);
-				if (nr != 1) {
+				if (sscanf(date_str, "%2u%2u%2u%2u%u%c", &tm_time.tm_mon,
+							&tm_time.tm_mday, &tm_time.tm_hour, &tm_time.tm_min,
+							&tm_time.tm_year, &end) < 4)
 					bb_error_msg_and_die(bb_msg_invalid_date, date_str);
+				/* correct for century  - minor Y2K problem here? */
+				if (tm_time.tm_year >= 1900) {
+					tm_time.tm_year -= 1900;
 				}
+				/* adjust date */
+				tm_time.tm_mon -= 1;
+				if (end == '.') {
+					if (sscanf(strchr(date_str, '.') + 1, "%u%c",
+							&tm_time.tm_sec, &end) == 1)
+						end = '\0';
+					/* else end != NUL and we error out */
+				}
 			}
-
-			/* correct for century  - minor Y2K problem here? */
-			if (tm_time.tm_year >= 1900) {
-				tm_time.tm_year -= 1900;
+			if (end != '\0') {
+				bb_error_msg_and_die(bb_msg_invalid_date, date_str);
 			}
-			/* adjust date */
-			tm_time.tm_mon -= 1;
 		}
-
 		/* Correct any day of week and day of year etc. fields */
 		tm_time.tm_isdst = -1;	/* Be sure to recheck dst. */
 		tm = mktime(&tm_time);

Modified: trunk/busybox/include/usage.h
===================================================================
--- trunk/busybox/include/usage.h	2008-04-24 19:37:11 UTC (rev 21852)
+++ trunk/busybox/include/usage.h	2008-04-25 02:14:07 UTC (rev 21853)
@@ -612,8 +612,9 @@
 	) \
      "\n" \
      "\nRecognized formats for TIME:" \
-     "\n	[hh:]mm:ss" \
+     "\n	hh:mm[:ss]" \
      "\n	[YYYY.]MM.DD-hh:mm[:ss]" \
+     "\n	YYYY-MM-DD hh:mm[:ss]" \
      "\n	MMDDhhmm[[YY]YY][.ss]" \
 
 #define date_example_usage \

Modified: trunk/busybox/testsuite/date/date-works
===================================================================
--- trunk/busybox/testsuite/date/date-works	2008-04-24 19:37:11 UTC (rev 21852)
+++ trunk/busybox/testsuite/date/date-works	2008-04-25 02:14:07 UTC (rev 21853)
@@ -1 +1,44 @@
-test x"`date`" = x"`busybox date`"
+dt=`busybox date`
+# Expected format: Fri Apr 25 03:47:55 CEST 2008
+dt=`echo "$dt" | sed 's/^[^ ][^ ][^ ] [^ ][^ ][^ ] [ 0123][0-9] [012][0-9]:[0-5][0-9]:[0-6][0-9] [A-Z][A-Z]* [012][0-9][0-9][0-9]$/OK/'`
+test x"$dt" = x"OK"
+
+dt=`busybox date -d 1:2`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Fri Apr 25 01:02:00"
+
+dt=`busybox date -d 1:2:3`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Fri Apr 25 01:02:03"
+
+dt=`busybox date -d 1.2-3:4`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Wed Jan  2 03:04:00"
+
+dt=`busybox date -d 1.2-3:4:5`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Wed Jan  2 03:04:05"
+
+dt=`busybox date -d 1999.1.2-3:4`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Sat Jan  2 03:04:00"
+
+dt=`busybox date -d 1999.1.2-3:4:5`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Sat Jan  2 03:04:05"
+
+dt=`busybox date -d '1999-1-2 3:4:5'`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Sat Jan  2 03:04:05"
+
+dt=`busybox date -d 01231133`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Wed Jan 23 11:33:00"
+
+dt=`busybox date -d 012311332000`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Sun Jan 23 11:33:00"
+
+dt=`busybox date -d 012311332000.30`
+dt=`echo "$dt" | cut -b1-19`
+test x"$dt" = x"Sun Jan 23 11:33:30"




More information about the busybox-cvs mailing list