logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 96eb4ca1b43ccd758be30e2e0f7ae17c0f337725
parent 2f9d3d84b8a2a1a9d3f038ae0f33def0bb0af3c4
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat,  3 Jun 2023 12:20:53 +0200

cmd/date: Switch to iso_parse for basic ISO datetime support

Diffstat:

MMakefile4++++
Mcmd/date.19+++++++--
Mcmd/date.c28++++------------------------
Mtest-cmd/date11+++++++++--
4 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile @@ -73,6 +73,10 @@ cmd/sleep: cmd/sleep.c Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) $(LIBBSD_CFLAGS) -o $@ cmd/sleep.c $(LIBBSD_LIBS) $(LDFLAGS) +cmd/date: cmd/date.c lib/iso_parse.c Makefile + rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} + $(CC) -std=c99 $(CFLAGS) -o $@ cmd/date.c lib/iso_parse.c $(LDFLAGS) + cmd/touch: cmd/touch.c lib/iso_parse.c Makefile rm -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno} $(CC) -std=c99 $(CFLAGS) -o $@ cmd/touch.c lib/iso_parse.c $(LDFLAGS) diff --git a/cmd/date.1 b/cmd/date.1 @@ -1,7 +1,7 @@ .\" Collection of Unix tools, comparable to coreutils .\" Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> .\" SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only -.Dd 2022-03-11 +.Dd 2023-06-03 .Dt DATE 1 .Os .Sh NAME @@ -20,7 +20,12 @@ Otherwise, depending on the options specified, will print the datetime in a user .Bl -tag -width Ds .It Fl d Ar datetime .Ar datetime -can contain the Unix timestamp (number of seconds before and after 1970-01-01T00:00:00Z) prefixed by an @ (at) symbol. +can contain the Unix timestamp (number of seconds before and after 1970-01-01T00:00:00Z) prefixed by an @ (at) symbol, +or a date-time formatted as +.Ql YYYY-MM-DDThh:mm:SS[frac][Z] , +see +.Xr touch 1 +for more details on the format. .It Fl u Use UTC (coordinated universal time) instead of the local time. .El diff --git a/cmd/date.c b/cmd/date.c @@ -3,6 +3,9 @@ // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #define _POSIX_C_SOURCE 200809L + +#include "../lib/iso_parse.h" /* iso_parse */ + #include <errno.h> /* errno */ #include <locale.h> /* setlocale() */ #include <stdio.h> /* BUFSIZ, perror(), puts() */ @@ -10,26 +13,6 @@ #include <time.h> /* time, localtime, tm, strftime */ #include <unistd.h> /* getopt(), optarg, optind */ -int -custom_datetime(time_t *now, char *optarg) -{ - if(optarg[0] == '@') - { - optarg++; - errno = 0; - *now = (time_t)strtol(optarg, NULL, 10); - if(errno != 0) - { - perror("date: strtol"); - return 0; - } - - return 1; - } - - return 0; -} - void usage() { @@ -60,10 +43,7 @@ main(int argc, char *argv[]) switch(c) { case 'd': /* Custom datetime */ - if(!custom_datetime(&now, optarg)) - { - return 1; - } + now = iso_parse(optarg).tv_sec; break; case 'u': /* UTC timezone */ uflag++; diff --git a/test-cmd/date b/test-cmd/date @@ -60,10 +60,16 @@ timestamp_body() { atf_check -o "inline:1969-12-31T23:58:51\n" ../cmd/date -u -d @-69 '+%FT%T' atf_check -s 'exit:1' -e "inline:date: Error: Missing operand for option: '-d'\ndate [-uR][-d datetime] [+format]\n" ../cmd/date -u -d - atf_check -s 'exit:1' ../cmd/date -u -d 69 # 36893488147419103232 = 2^65 - atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232 + #atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232 +} + +atf_test_case isodate +isodate_body() { + atf_check -o "inline:0\n" ../cmd/date -u -d "1970-01-01T00:00:00Z" '+%s' + atf_check -o "inline:69\n" ../cmd/date -u -d "1970-01-01T00:01:09Z" '+%s' + atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51Z" '+%s' } atf_init_test_cases() { @@ -83,4 +89,5 @@ atf_init_test_cases() { atf_add_test_case utc atf_add_test_case timestamp + atf_add_test_case isodate }