logo

utils

~/.local/bin tools and git-hooks
commit: a44fd69f1764951bf1cd07ae0a05a4c998e16039
parent: c05c6a99a7a9588d9c5b8d27130dde5b9ad202d6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 14 Oct 2018 03:02:55 +0200

src/date.c: Support -d

Diffstat:

Mmkfile2+-
Msrc/date.c41++++++++++++++++++++++++++++-------------
2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/mkfile b/mkfile @@ -1,6 +1,6 @@ MKSHELL=/bin/sh CC=cc -CFLAGS="-Wpedantic -Wall -Wextra -Wformat -O2 -std=c99 -Wno-unused -Werror=date-time" +CFLAGS="-Wpedantic -Wall -Wextra -Wformat -O2 -std=c99 -Wno-unused -Werror=date-time -D_POSIX_C_SOURCE=200809L" PREFIX="/usr/local" BINDIR=$PREFIX"/bin" DOCDIR=$PREFIX"/share/doc/lanodan-utils" diff --git a/src/date.c b/src/date.c @@ -3,30 +3,45 @@ #include <stdio.h> /* BUFSIZ, perror(), puts() */ #include <stdlib.h> /* exit() */ #include <time.h> /* time, localtime, tm, strftime */ +#include <unistd.h> /* getopt(), optarg, optind */ int main(int argc, char *argv[]) { char outstr[BUFSIZ]; - time_t now = time(NULL); - struct tm *tm = localtime(&now); + struct tm *tm; + time_t now; char *format = "%c"; + int uflag = 0, dflag = 0; + int c; setlocale(LC_ALL, ""); - for(int i = (argc - 1); i > 0; i--) - { - if((argv[i][0] == '-') && (argv[i][1] == 'u')) - { - tm = gmtime(&now); - } - - if(argv[i][0] == '+') - { - /* format = argv[1][1,]; */ - format = &argv[i][1]; + while ((c = getopt(argc, argv, ":ud:")) != -1) { + switch(c) { + case 'd': /* user-provided datetime */ + now = time(optarg); + dflag++; + break; + case 'u': /* Timezone is UTC */ + uflag++; + break; } } + if(!dflag) + now = time(NULL); + + if(uflag) + tm = gmtime(&now); + else + tm = localtime(&now); + + argc -= optind; + argv += optind; + + if (*argv && **argv == '+') + format = *argv + 1; + if(tm == NULL) { perror("localtime");