#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);
  }
}
