time Utility for working with time/dates
This commit is contained in:
2
user/time/.gitignore
vendored
Normal file
2
user/time/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.o
|
||||
time
|
||||
24
user/time/Makefile
Normal file
24
user/time/Makefile
Normal file
@ -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)
|
||||
24
user/time/main.c
Normal file
24
user/time/main.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <ulib.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
38
user/time/now.c
Normal file
38
user/time/now.c
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user