From cdd04374ed435fc75fb9a76298ed08074e8d5ed7 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 13 Mar 2022 15:13:09 -0400 Subject: do fractional seconds in -d date --- touch.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/touch.c b/touch.c index 06d0e69..adf8097 100644 --- a/touch.c +++ b/touch.c @@ -163,7 +163,7 @@ static int getnext(const char *current, char **next, size_t ndigits, char *term, return ret; } -static struct timespec convert_date(const char *date) +static struct timespec convert_date(char *date) { struct timespec ts = { 0 }; struct tm tm = { 0 }; @@ -176,7 +176,13 @@ static struct timespec convert_date(const char *date) tm.tm_min = getnext(next, &next, 2, ":", 0, 59); tm.tm_sec = getnext(next, &next, 2, ".,", 0, 60); - /* TODO: frac */ + char *dec = strchr(date, '.') ? strchr(date, '.') : strchr(date, ','); + if (dec) { + *dec = '.'; + double d = strtod(dec, &next); + ts.tv_nsec = d * 1000000000; + } + /* TODO: tz */ /* TODO: time is too large */ @@ -240,6 +246,11 @@ int main(int argc, char *argv[]) } } + if (optind >= argc) { + fprintf(stderr, "touch: missing operands\n"); + return 1; + } + struct timespec times[2] = { { 0 } }; struct stat st; -- cgit v1.2.1