diff options
| author | 2023-05-12 14:53:20 -0400 | |
|---|---|---|
| committer | 2023-05-12 14:53:20 -0400 | |
| commit | 78e775a1b89c827ad2e96b73a723c7caf83f5a82 (patch) | |
| tree | 11baea453275a2c9d1d22a64bcbdb9344be0cfb7 /src/time.c | |
| parent | update many things (diff) | |
rewrite
Split it into multiple things.
The new tests will actually reflect how you should use the native
library.
Diffstat (limited to 'src/time.c')
| -rw-r--r-- | src/time.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..64096c7 --- /dev/null +++ b/src/time.c @@ -0,0 +1,100 @@ +#include "date.h" +#include "janet.h" + +// wrappers around time_t + +static JanetMethod jd_time_methods[] = { + {"gmtime", jd_gmtime}, + {"localtime", jd_localtime}, + {"todict", jd_dict_time}, + {NULL, NULL}, +}; + +static int jd_time_compare(void *lhs, void *rhs) { + time_t lhv = (*(time_t*)lhs); + time_t rhv = (*(time_t*)rhs); + return difftime(lhv, rhv); +} + +static int jd_time_get(void *p, Janet key, Janet *out) { + (void) p; + if (!janet_checktype(key, JANET_KEYWORD)) { + return 0; + } + return janet_getmethod(janet_unwrap_keyword(key), jd_time_methods, out); +} + +// time_t is always a UTC-representation +static void jd_time_tostring(void *p, JanetBuffer *buffer) { + strftime_buffer("%F %T.000 UTC", localtime(p), buffer); +} + +static const JanetAbstractType jd_time_t = { + "time", + NULL, + NULL, + jd_time_get, + NULL, + NULL, + NULL, + jd_time_tostring, + jd_time_compare, + JANET_ATEND_COMPARE +}; + +time_t *jd_gettime(Janet *argv, int32_t n) { + return (time_t*)janet_getabstract(argv, n, &jd_time_t); +} + +time_t *jd_maketime(void) { + return janet_abstract(&jd_time_t, sizeof(time_t)); +} + +JANET_FN(jd_dict_time, + "(dict->time {...})", + "") { + janet_fixarity(argc, 1); + JanetDictView dict = janet_getdictionary(argv, 0); + struct tm *tm = jd_tm_from_dict(dict); + return janet_wrap_abstract(tm); +} + +JANET_FN(jd_gmtime, + "(gmtime (time))", + "") { + janet_fixarity(argc, 1); + time_t *time = jd_gettime(argv, 0); + struct tm *tm = gmtime(time); + struct tm *out = jd_maketm(); + *out = *tm; + return janet_wrap_abstract(out); +} + +JANET_FN(jd_localtime, + "(localtime (time))", + "WARNING: do not use this unless it's for final display.") { + janet_fixarity(argc, 1); + time_t *time = jd_gettime(argv, 0); + struct tm *tm = localtime(time); + struct tm *out = jd_maketm(); + *out = *tm; + return janet_wrap_abstract(out); +} + +JANET_FN(jd_time, + "(time)", + "") { + (void) argv; + janet_fixarity(argc, 0); + time_t *out = jd_maketime(); + time(out); + return janet_wrap_abstract(out); +} + +const JanetRegExt jd_time_cfuns[] = { + JANET_REG("dict->time", jd_dict_time), + JANET_REG("gmtime", jd_gmtime), + JANET_REG("localtime", jd_localtime), + JANET_REG("time", jd_time), + JANET_REG_END +}; |
