diff --git a/src/core/os.c b/src/core/os.c index 3ac2ebc4..0555ce9e 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -272,6 +272,30 @@ static Janet os_cwd(int32_t argc, Janet *argv) { return janet_cstringv(ptr); } +static Janet os_date(int32_t argc, Janet *argv) { + janet_arity(argc, 0, 1); + (void) argv; + time_t t; + struct tm *t_info; + if (argc) { + t = (time_t) janet_getinteger64(argv, 0); + } else { + time(&t); + } + t_info = localtime(&t); + JanetKV *st = janet_struct_begin(9); + janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t_info->tm_sec)); + janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t_info->tm_min)); + janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t_info->tm_hour)); + janet_struct_put(st, janet_ckeywordv("month-day"), janet_wrap_number(t_info->tm_mday)); + janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t_info->tm_mon)); + janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t_info->tm_year)); + janet_struct_put(st, janet_ckeywordv("week-day"), janet_wrap_number(t_info->tm_wday)); + janet_struct_put(st, janet_ckeywordv("year-day"), janet_wrap_number(t_info->tm_yday)); + janet_struct_put(st, janet_ckeywordv("dst"), janet_wrap_boolean(t_info->tm_isdst)); + return janet_wrap_struct(janet_struct_end(st)); +} + static const JanetReg cfuns[] = { { "os/which", os_which, @@ -318,7 +342,7 @@ static const JanetReg cfuns[] = { "os/clock", os_clock, JDOC("(os/clock)\n\n" "Return the number of seconds since some fixed point in time. The clock " - "is guaranteed to be non decreased in real time.") + "is guaranteed to be non decreasing in real time.") }, { "os/sleep", os_sleep, @@ -331,6 +355,21 @@ static const JanetReg cfuns[] = { JDOC("(os/cwd)\n\n" "Returns the current working directory.") }, + { + "os/date", os_date, + JDOC("(os/date [,time])\n\n" + "Returns the given time as a date struct, or the current time if no time is given. " + "Returns a struct with following key values.\n\n" + "\t:seconds - number of seconds [0-61]\n" + "\t:minutes - number of minutes [0-59]\n" + "\t:seconds - number of hours [0-23]\n" + "\t:month-day - day of month [1-31]\n" + "\t:month - month of year [0, 11]\n" + "\t:year - years since 1900\n" + "\t:week-day - day of the week [0-6]\n" + "\t:year-day - day of the year [0-365]\n" + "\t:dst - If Day Light Savings is in effect") + }, {NULL, NULL, NULL} };