logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: e2571aa456e633dbb93dd320a001e18eb62b097c
parent 0bcbc6ade5461f1f1bd86d143f21fd87f42003b8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 29 Sep 2022 00:14:52 +0200

cmd/date: Add -R option

Diffstat:

Mcmd/date.c7+++++--
Mtest-cmd/date12++++++++++--
2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/cmd/date.c b/cmd/date.c @@ -33,7 +33,7 @@ custom_datetime(time_t *now, char *optarg) void usage() { - fprintf(stderr, "date [-u][-d datetime] [+format]\n"); + fprintf(stderr, "date [-uR][-d datetime] [+format]\n"); } int @@ -55,7 +55,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } - while((c = getopt(argc, argv, ":d:u")) != -1) + while((c = getopt(argc, argv, ":d:uR")) != -1) { switch(c) { @@ -68,6 +68,9 @@ main(int argc, char *argv[]) case 'u': /* UTC timezone */ uflag++; break; + case 'R': /* Email (RFC 5322) format */ + format = "%a, %d %b %Y %H:%M:%S %z"; + break; case ':': fprintf(stderr, "date: Error: Missing operand for option: '-%c'\n", optopt); usage(); diff --git a/test-cmd/date b/test-cmd/date @@ -9,17 +9,24 @@ noargs_body() { atf_test_case badarg badarg_body() { - atf_check -s 'exit:1' -e "inline:date: Error: Unrecognised option: '-x'\ndate [-u][-d datetime] [+format]\n" ../cmd/date -x + atf_check -s 'exit:1' -e "inline:date: Error: Unrecognised option: '-x'\ndate [-uR][-d datetime] [+format]\n" ../cmd/date -x } atf_test_case epoch epoch_body() { atf_check -o "match:^[0-9]+$" ../cmd/date '+%s' + atf_check -o "inline:1155544496\n" ../cmd/date -uR -d @1155544496 '+%s' } atf_test_case rfc3339 rfc3339_body() { atf_check -o "match:^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{4}$" ../cmd/date '+%FT%T%z' + atf_check -o "match:^2006\-08\-14T08:34:56[+\-]00:?00$" ../cmd/date -uR -d @1155544496 '+%FT%T%z' +} + +atf_test_case rfc5322 +rfc5322_body() { + atf_check -o "match:^Mon, 14 Aug 2006 08:34:56 [+\-]00:?00$" ../cmd/date -uR -d @1155544496 } atf_test_case empty @@ -52,7 +59,7 @@ timestamp_body() { atf_check -o "inline:1970-01-01T00:01:09\n" ../cmd/date -u -d @69 '+%FT%T' 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 [-u][-d datetime] [+format]\n" ../cmd/date -u -d + 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 @@ -72,6 +79,7 @@ atf_init_test_cases() { atf_add_test_case epoch atf_add_test_case rfc3339 + atf_add_test_case rfc5322 atf_add_test_case utc atf_add_test_case timestamp