time Utility for working with time/dates

This commit is contained in:
2025-10-16 14:15:55 +02:00
parent 7445689010
commit 553b893a9c
4 changed files with 88 additions and 0 deletions

38
user/time/now.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdint.h>
#include <stddef.h>
#include <ulib.h>
struct {
char *format;
} TM_NOW_CONFIG = {0};
static Arg ARGS[] = {
ARG("-fmt", ARG_STRING, &TM_NOW_CONFIG.format),
ARG_END(),
};
void tm_now(void) {
int32_t ret;
if ((ret = parse_args(args()+1, argslen()-1, ARGS)) < 0) {
uprintf("time: could not parse args: %d\n", ret);
return;
}
Time tmbuf;
time(&tmbuf);
if (TM_NOW_CONFIG.format == NULL) {
uprintf("%02u/%02u/%02u %02u:%02u:%02u\n",
tmbuf.day, tmbuf.month, tmbuf.year,
tmbuf.hour, tmbuf.minute, tmbuf.second
);
return;
}
if (string_strcmp(TM_NOW_CONFIG.format, "unix") == 0) {
timeunix_t unix = time_tounix(&tmbuf);
uprintf("%lu\n", unix);
} else {
uprintf("time: unknown format %s\n", TM_NOW_CONFIG.format);
}
}