From 553b893a9ca17a5ae8687779321380c6514a814f Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Thu, 16 Oct 2025 14:15:55 +0200 Subject: [PATCH] time Utility for working with time/dates --- user/time/.gitignore | 2 ++ user/time/Makefile | 24 ++++++++++++++++++++++++ user/time/main.c | 24 ++++++++++++++++++++++++ user/time/now.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 user/time/.gitignore create mode 100644 user/time/Makefile create mode 100644 user/time/main.c create mode 100644 user/time/now.c diff --git a/user/time/.gitignore b/user/time/.gitignore new file mode 100644 index 0000000..a4c22a9 --- /dev/null +++ b/user/time/.gitignore @@ -0,0 +1,2 @@ +*.o +time diff --git a/user/time/Makefile b/user/time/Makefile new file mode 100644 index 0000000..f83b430 --- /dev/null +++ b/user/time/Makefile @@ -0,0 +1,24 @@ +include $(ROOT)/mk/grabsrc.mk +include ../Makefile.inc + +.PHONY: all clean + +TARGET := time + +LDFLAGS += -L$(ROOT)/ulib -l:libulib.a + +SRCFILES := $(call GRABSRC, .) +CFILES := $(call GET_CFILES, $(SRCFILES)) +OBJ := $(call GET_OBJ, $(SRCFILES)) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(LD) $^ $(LDFLAGS) -o $@ + echo $$(realpath $(TARGET)) >> $(FILES) + +clean: + rm -f $(OBJ) $(TARGET) diff --git a/user/time/main.c b/user/time/main.c new file mode 100644 index 0000000..3c80e80 --- /dev/null +++ b/user/time/main.c @@ -0,0 +1,24 @@ +#include +#include +#include + +#define CMDS(X) \ + X(now) + +void main(void) { + if (argslen() == 0) { + return; + } + + char *cmd = args()[0]; + + #define X(name) if (string_strcmp(cmd, #name) == 0) { \ + extern void tm_ ## name(void); \ + tm_ ## name(); \ + return; \ + } + CMDS(X) + #undef X + + uprintf("time: unknown command %s\n", cmd); +} diff --git a/user/time/now.c b/user/time/now.c new file mode 100644 index 0000000..9ae0cfb --- /dev/null +++ b/user/time/now.c @@ -0,0 +1,38 @@ +#include +#include +#include + +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); + } +}