[git commit] date: accept 'yyyy-mm-dd HH' and 'yyyy-mm-dd' date formats

Denys Vlasenko vda.linux at googlemail.com
Thu Jul 25 03:10:01 UTC 2013


commit: http://git.busybox.net/busybox/commit/?id=688a7e3f0454363e1dfed481e95afcdb818b1c91
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
parse_datestr                                        794     885     +91

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/time.c |   33 +++++++++++++++++++++++----------
 1 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/libbb/time.c b/libbb/time.c
index 57e14b6..ea2f72e 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -23,14 +23,16 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
 		if (sscanf(date_str, "%u:%u%c",
 					&ptm->tm_hour,
 					&ptm->tm_min,
-					&end) >= 2) {
+					&end) >= 2
+		) {
 			/* no adjustments needed */
 		} else
 		/* mm.dd-HH:MM */
 		if (sscanf(date_str, "%u.%u-%u:%u%c",
 					&ptm->tm_mon, &ptm->tm_mday,
 					&ptm->tm_hour, &ptm->tm_min,
-					&end) >= 4) {
+					&end) >= 4
+		) {
 			/* Adjust month from 1-12 to 0-11 */
 			ptm->tm_mon -= 1;
 		} else
@@ -38,15 +40,13 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
 		if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
 					&ptm->tm_mon, &ptm->tm_mday,
 					&ptm->tm_hour, &ptm->tm_min,
-					&end) >= 5) {
-			ptm->tm_year -= 1900; /* Adjust years */
-			ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
-		} else
+					&end) >= 5
 		/* yyyy-mm-dd HH:MM */
-		if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
+		 || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
 					&ptm->tm_mon, &ptm->tm_mday,
 					&ptm->tm_hour, &ptm->tm_min,
-					&end) >= 5) {
+					&end) >= 5
+		) {
 			ptm->tm_year -= 1900; /* Adjust years */
 			ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
 		} else
@@ -58,7 +58,6 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
 			return; /* don't fall through to end == ":" check */
 		} else
 #endif
-//TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
 		{
 			bb_error_msg_and_die(bb_msg_invalid_date, date_str);
 		}
@@ -68,7 +67,21 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
 				end = '\0';
 			/* else end != NUL and we error out */
 		}
-	} else if (date_str[0] == '@') {
+	} else
+	/* yyyy-mm-dd HH */
+	if (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
+				&ptm->tm_mon, &ptm->tm_mday,
+				&ptm->tm_hour,
+				&end) >= 4
+	/* yyyy-mm-dd */
+	 || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
+				&ptm->tm_mon, &ptm->tm_mday,
+				&end) >= 3
+	) {
+		ptm->tm_year -= 1900; /* Adjust years */
+		ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
+	} else
+	if (date_str[0] == '@') {
 		time_t t = bb_strtol(date_str + 1, NULL, 10);
 		if (!errno) {
 			struct tm *lt = localtime(&t);


More information about the busybox-cvs mailing list