diff options
author | Rob Landley <rob@landley.net> | 2019-03-26 15:40:00 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-03-26 15:40:00 -0500 |
commit | d78f05e91bb3a87a73b1d3fad29362447ee8e1f6 (patch) | |
tree | 89f486d2e5355f3cda46b8a94a77ffa9ab9da94d /toys/posix | |
parent | 0b7206b22e7ef9d5d72079caa943592e313a6d65 (diff) | |
download | toybox-d78f05e91bb3a87a73b1d3fad29362447ee8e1f6.tar.gz |
Make touch use xparsedate() (result: -t and -d the same, autodetects format),
and fix tests to pass on host too.
Diffstat (limited to 'toys/posix')
-rw-r--r-- | toys/posix/date.c | 2 | ||||
-rw-r--r-- | toys/posix/touch.c | 62 |
2 files changed, 7 insertions, 57 deletions
diff --git a/toys/posix/date.c b/toys/posix/date.c index 685ac8bf..a1762854 100644 --- a/toys/posix/date.c +++ b/toys/posix/date.c @@ -77,7 +77,7 @@ static void parse_date(char *str, time_t *t) tzset(); } time(t); - xparsedate(str, t, &TT.nano); + xparsedate(str, t, &TT.nano, 1); if (new_tz) { if (old_tz) setenv("TZ", old_tz, 1); else unsetenv("TZ"); diff --git a/toys/posix/touch.c b/toys/posix/touch.c index 3775c8bc..5e33d7f7 100644 --- a/toys/posix/touch.c +++ b/toys/posix/touch.c @@ -41,67 +41,17 @@ void touch_main(void) // use current time if no -t or -d ts[0].tv_nsec = UTIME_NOW; - if (FLAG(t) || FLAG(d)) { - char *s, *date, **format; - struct tm tm; - int len = 0; - - // Initialize default values for time fields - ts->tv_sec = time(0); - ts->tv_nsec = 0; - - // List of search types - if (FLAG(d)) { - format = (char *[]){"%Y-%m-%dT%T", "%Y-%m-%d %T", 0}; - date = TT.d; - } else { - format = (char *[]){"%m%d%H%M", "%y%m%d%H%M", "%C%y%m%d%H%M", 0}; - date = TT.t; - } - - // Trailing Z means UTC timezone, don't expect libc to know this. - i = strlen(s = date); - if (i && toupper(date[i-1])=='Z') { - date[i-1] = 0; - setenv("TZ", "UTC0", 1); - } - while (*format) { - if (FLAG(t)) { - s = strchr(date, '.'); - if ((s ? s-date : strlen(date)) != strlen(*format)) { - format++; - continue; - } - } - localtime_r(&(ts->tv_sec), &tm); - // Adjusting for daylight savings time gives the wrong answer. - tm.tm_isdst = 0; - tm.tm_sec = 0; - s = strptime(date, *format++, &tm); - - // parse nanoseconds - if (s && *s=='.' && isdigit(s[1])) { - s++; - if (FLAG(t)) - if (1 == sscanf(s, "%2u%n", &(tm.tm_sec), &len)) s += len; - if (1 == sscanf(s, "%lu%n", &ts->tv_nsec, &len)) { - s += len; - if (ts->tv_nsec > 999999999) s = 0; - else while (len++ < 9) ts->tv_nsec *= 10; - } - } - if (s && !*s) break; - } + if (FLAG(t) || FLAG(d)) { + time_t t = time(0); + unsigned nano; - errno = 0; - ts->tv_sec = mktime(&tm); - if (!s || *s || ts->tv_sec == -1) perror_exit("bad '%s'", date); + xparsedate(TT.t ? TT.t : TT.d, &t, &nano, 0); + ts->tv_sec = t; + ts->tv_nsec = nano; } ts[1]=ts[0]; - // Set time from -r? - if (TT.r) { struct stat st; |