logo

utils

~/.local/bin tools and git-hooks
commit: b7afc99c488c8c2d4c78fad78b38146b523f3c6e
parent: a85da21135d7879a2b9d369d5351def4c7a023e6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  1 Mar 2018 18:10:50 +0100

src/date.c: init with POSIX arguments/options

But screw POSIX locale (whatever that is) that should use this western-legacy format "%a %b %e %H:%M:%S %Z %Y"

Diffstat:

Asrc/date.c37+++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+), 0 deletions(-)

diff --git a/src/date.c b/src/date.c @@ -0,0 +1,37 @@ +/* Copyright CC-BY-SA-4.0 2017-2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me> */ +#include <locale.h> /* setlocale() */ +#include <stdio.h> /* BUFSIZ, perror(), puts() */ +#include <stdlib.h> /* exit() */ +#include <time.h> /* time, localtime, tm, strftime */ + +int main(int argc, char *argv[]) { + char outstr[BUFSIZ]; + time_t now = time(NULL); + struct tm *tm = localtime(&now); + char *format = "%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]; + } + } + + if (tm == NULL) { + perror("localtime"); + exit(EXIT_FAILURE); + } + + if (strftime(outstr, sizeof(outstr), format, tm) == 0) { + perror("strftime returned 0"); + exit(EXIT_FAILURE); + } + + return puts(outstr); +}