aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-02-07 19:03:30 -0800
committerRob Landley <rob@landley.net>2019-02-11 23:21:13 -0600
commit8326fe1f7e141f49a46a57acc6221651298b42c9 (patch)
tree81e8468040d47011e16d3f236b97c84de91d4fb5
parent1a3c65c868b676c86073025465f57116d0e91881 (diff)
downloadtoybox-8326fe1f7e141f49a46a57acc6221651298b42c9.tar.gz
date: some fixes.
Add support for more input formats, primarily the ISO formats used by the AOSP build. I've improved/added to the tests a bit to cover these changes, and to explain the reasons for the various remaining test failures (none of which are regressions caused by this patch).
-rw-r--r--tests/date.test49
-rw-r--r--toys/posix/date.c34
2 files changed, 60 insertions, 23 deletions
diff --git a/tests/date.test b/tests/date.test
index 431b35ab..e0da9a44 100644
--- a/tests/date.test
+++ b/tests/date.test
@@ -4,25 +4,38 @@
#testing "name" "command" "result" "infile" "stdin"
-# Test Unix date parsing.
-testing "-d @0" "TZ=UTC date -d @0 2>&1" "Thu Jan 1 00:00:00 GMT 1970\n" "" ""
-testing "-d @0x123" "TZ=UTC date -d @0x123 2>&1" "date: bad date '@0x123'\n" "" ""
-
-# Test basic date parsing.
-# Note that toybox's -d format is not the same as coreutils'.
-testing "-d 06021234" "TZ=UTC date -d 06021234 2>&1" "Sun Jun 2 12:34:00 UTC 1900\n" "" ""
-testing "-d 060212341982" "TZ=UTC date -d 060212341982 2>&1" "Sun Jun 2 12:34:00 UTC 1982\n" "" ""
-testing "-d 123" "TZ=UTC date -d 123 2>&1" "date: bad date '123'\n" "" ""
-testing "-d 2018-10-04" "TZ=UTC date -d 2018-10-04 2>&1" "Thu Oct 4 00:00:00 UTC 2018\n" "" ""
-
-# Test parsing 2- and 4-digit years.
-testing "-d 1110143115.30" "TZ=UTC date -d 1110143115.30 2>&1" "Sun Nov 10 14:31:30 UTC 1915\n" "" ""
-testing "-d 111014312015.30" "TZ=UTC date -d 111014312015.30 2>&1" "Sun Nov 10 14:31:30 UTC 2015\n" "" ""
-
-# Accidentally given a Unix time, we should trivially reject that.
-testing "Unix time missing @" "TZ=UTC date 1438053157 2>/dev/null || echo no" \
+# Use a consistent TZ for these tests, but not GMT/UTC because that makes mistakes harder to spot.
+tz=Europe/London
+
+# Unix date parsing.
+testing "-d @0" "TZ=$tz date -d @0 2>&1" "Thu Jan 1 01:00:00 BST 1970\n" "" ""
+testing "-d @0x123 invalid" "TZ=$tz date -d @0x123 2>/dev/null || echo expected error" "expected error\n" "" ""
+
+# TODO: these are rejected by coreutils and interpreted differently by busybox.
+# busybox thinks this should use the current year, not 1900.
+testing "-d 06021234" "TZ=$tz date -d 06021234 2>&1" "Sun Jun 2 12:34:00 UTC 1900\n" "" ""
+# busybox thinks this is the year 603 (ISO time 0602-12-34 19:82 with out of range fields normalized).
+testing "-d 060212341982" "TZ=$tz date -d 060212341982 2>&1" "Sun Jun 2 12:34:00 UTC 1982\n" "" ""
+
+# POSIX format with 2- and 4-digit years.
+# TODO: coreutils rejects POSIX format supplied to -d.
+testing "-d 1110143115.30" "TZ=$tz date -d 1110143115.30 2>&1" "Sun Nov 10 14:31:30 UTC 1915\n" "" ""
+testing "-d 111014312015.30" "TZ=$tz date -d 111014312015.30 2>&1" "Sun Nov 10 14:31:30 UTC 2015\n" "" ""
+
+# ISO date format.
+testing "-d 1980-01-02" "TZ=$tz date -d 1980-01-02 2>&1" "Wed Jan 2 00:00:00 GMT 1980\n" "" ""
+testing "-d 1980-01-02 12:34" "TZ=$tz date -d '1980-01-02 12:34' 2>&1" "Wed Jan 2 12:34:00 GMT 1980\n" "" ""
+testing "-d 1980-01-02 12:34:56" "TZ=$tz date -d '1980-01-02 12:34:56' 2>&1" "Wed Jan 2 12:34:56 GMT 1980\n" "" ""
+
+# Reject Unix times without a leading @.
+testing "Unix time missing @" "TZ=$tz date 1438053157 2>/dev/null || echo no" \
"no\n" "" ""
+# Test just hour and minute (accepted by coreutils and busybox, presumably for setting the time).
+this_year=$(date +%Y)
+testing "-d 12:34" 'TZ=UTC date -d 12:34 | grep -q " 12:34:00 UTC $this_year" && echo OK' "OK\n" "" ""
+testing "-d 12:34:56" 'TZ=UTC date -d 12:34:56 | grep -q " 12:34:56 UTC $this_year" && echo OK' "OK\n" "" ""
+
# Test the %N extension to srtftime(3) format strings.
testing "%N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%N" "20120123-123456.123456789\n" "" ""
testing "%1N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%1N" "20120123-123456.1\n" "" ""
@@ -35,5 +48,7 @@ testing "just %" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%" "%\n
rm -f f
# Test embedded TZ to take a date in one time zone and display it in another.
+# TODO: not yet working correctly in toybox.
+testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/London\" 2018-01-04 08:00'" "Thu Jan 4 00:00:00 PST 2018\n" "" ""
testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/London\" 2018-10-04 08:00'" "Thu Oct 4 00:00:00 PDT 2018\n" "" ""
testing "TZ= @" "TZ='America/Los_Angeles' date -d 'TZ=\"GMT\" @1533427200'" "Sat Aug 4 17:00:00 PDT 2018\n" "" ""
diff --git a/toys/posix/date.c b/toys/posix/date.c
index 9f244ca0..40c1fed2 100644
--- a/toys/posix/date.c
+++ b/toys/posix/date.c
@@ -17,15 +17,18 @@ config DATE
Set/get the current date/time. With no SET shows the current date.
- Default SET format is "MMDDhhmm[[CC]YY][.ss]", that's (2 digits each)
- month, day, hour (0-23), and minute. Optionally century, year, and second.
- Also accepts "@UNIXTIME[.FRACTION]" as seconds since midnight Jan 1 1970.
-
-d Show DATE instead of current time (convert date format)
-D +FORMAT for SET or -d (instead of MMDDhhmm[[CC]YY][.ss])
-r Use modification time of FILE instead of current date
-u Use UTC instead of current timezone
+ Supported input formats:
+
+ MMDDhhmm[[CC]YY][.ss] POSIX
+ @UNIXTIME[.FRACTION] seconds since midnight 1970-01-01
+ YYYY-MM-DD [hh:mm[:ss]] ISO 8601
+ hh:mm[:ss] 24-hour time today
+
+FORMAT specifies display format string using strftime(3) syntax:
%% literal % %n newline %t tab
@@ -53,11 +56,17 @@ GLOBALS(
unsigned nano;
)
+static const char *formats[] = {
+ // Formats with years must come first.
+ "%Y-%m-%d %H:%M:%S", "%Y-%m-%d %H:%M", "%Y-%m-%d",
+ "%H:%M:%S", "%H:%M", 0
+};
+
// Handle default posix date format (mmddhhmm[[cc]yy]) or @UNIX[.FRAC]
// returns 0 success, nonzero for error
static int parse_default(char *str, struct tm *tm)
{
- int len = 0;
+ int len = 0, i;
// Parse @UNIXTIME[.FRACTION]
if (*str == '@') {
@@ -83,7 +92,20 @@ static int parse_default(char *str, struct tm *tm)
return 0;
}
- // Posix format
+ // Is it one of the fancy formats?
+ for (i = 0; formats[i]; ++i) {
+ time_t now = time(NULL);
+ char *p;
+
+ if (!strchr(formats[i], 'Y')) {
+ localtime_r(&now, tm);
+ tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
+ }
+ if ((p = strptime(str, formats[i], tm)) && !*p) return 0;
+ }
+ memset(tm, 0, sizeof(struct tm));
+
+ // Posix format?
sscanf(str, "%2u%2u%2u%2u%n", &tm->tm_mon, &tm->tm_mday, &tm->tm_hour,
&tm->tm_min, &len);
if (len != 8) return 1;