config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 0f047b44b0972948e5111e239de384ee997fc729
parent e2b13aab75d18436a6e06e4ae2ee78ef41e8ae21
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Sun,  8 Oct 2023 08:47:39 -0700

Add timedec program

timedec is a simple program to convert an ASCII representation of
time to the seconds elapsed since the UNIX epoch. It's purpose is
to provide a standard interface to specifying time in other programs.

Diffstat:
Mlocal/src/src/singles/Makefile | 1+
Alocal/src/src/singles/timedec.c | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/local/src/src/singles/Makefile b/local/src/src/singles/Makefile @@ -1,5 +1,6 @@ names =\ datediff\ + timedec\ numsep\ urldecode\ urlencode\ diff --git a/local/src/src/singles/timedec.c b/local/src/src/singles/timedec.c @@ -0,0 +1,100 @@ +/* + * Copyright 2023 Jacob R. Edwards <jacob@jacobedwards.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +char * +die(char *msg) +{ + perror(msg); + exit(1); +} + +char * +xstrptime(char *buf, char *fmt, struct tm *tm) +{ + char *ep; + struct tm ref; + + memcpy(&ref, tm, sizeof(ref)); + if ((ep = strptime(buf, fmt, tm))) + return ep; + memcpy(tm, &ref, sizeof(ref)); + return NULL; +} + +char * +strptimes(char *buf, char **fmts, struct tm *tm) +{ + char *ep; + + for (; *fmts; ++fmts) + if ((ep = xstrptime(buf, *fmts, tm))) + return ep; + return NULL; +} + +time_t +decodedatetime(char *s) +{ + char *ep; + struct tm *tm; + time_t t; + + t = time(NULL); + tm = localtime(&t); + tm->tm_hour = 0; + tm->tm_min = 0; + tm->tm_sec = 0; + + ep = xstrptime(s, "%F", tm); + if (ep) { + if (ep[0] == '\0') + return mktime(tm); + else if (ep[0] != ' ') + return 0; + s = ep + 1; + } + + ep = strptimes(s, (char *[]){ "%T", "%R", NULL }, tm); + if (!ep || (ep && ep[0])) + return 0; + return mktime(tm); +} + +/* + * This program takes a time given in the strftime(3) format '%F' + * or '[%F ]%H:%M[:%S]' and outputs the time elapsed since Jan 1, + * 1970 00:00 in seconds. + * + * It is meant as to be a standard interface to specifying time. + */ +int +main(int argc, char *argv[]) +{ + char *ts; + time_t t; + + if (!argv[1]) + die("usage: timedec time"); + t = decodedatetime(argv[1]); + if (t == 0) + die("invalid time (must be of the form '%F' or '[%F ]%H:%M[:%S]')"); + return printf("%d\n", t) < 0; +}