diff options
| author | 2023-06-17 22:20:11 +0200 | |
|---|---|---|
| committer | 2023-06-17 22:20:11 +0200 | |
| commit | 36696695692ae816e8a3d442b7647e3aba1feba5 (patch) | |
| tree | 4a06bc97e2e1091657e8884022407ee8ecea3add /src | |
| parent | minor native sources cleanup (diff) | |
allow constructing your own date/tm values
Diffstat (limited to 'src')
| -rw-r--r-- | src/time.c | 10 | ||||
| -rw-r--r-- | src/tm.c | 15 |
2 files changed, 16 insertions, 9 deletions
@@ -22,10 +22,14 @@ static int jd_time_get(void *p, Janet key, Janet *out) { return janet_getmethod(janet_unwrap_keyword(key), jd_time_methods, out); } -// time_t is always a UTC-representation +// time_t is an arithmetic type, which means some width of int or float +// instead of trying to guess the type, +// we give the number of seconds from the zero-value of time_t +// on UNIX platforms, this is the number of seconds since UNIX epoch (1970) +// ultimately, the user knows what platform they're on, making the output useful static void jd_time_tostring(void *p, JanetBuffer *buffer) { - // print ISO 8601 - strftime_buffer("%F %T%z", gmtime(p), buffer); + double dt = difftime(*(time_t*)p, 0); + janet_formatb(buffer, "%f", dt); } static const JanetAbstractType jd_time_t = { @@ -215,15 +215,18 @@ struct tm *jd_opttm(Janet *argv, int32_t argc, int32_t n) { JANET_FN(jd_tm, "(tm {:sec 0 ...})", - "Construct a date/tm object from a compatible dictionary.") { - janet_fixarity(argc, 1); - JanetDictView view = janet_getdictionary(argv, 0); + "Construct a date/tm object from a compatible dictionary.\n" + "Note that you *must* immediate pass this into timegm or mktime.") { + janet_arity(argc, 0, 1); struct tm *out = jd_maketm(); + // default values memset(out, 0, sizeof(struct tm)); -#ifdef TM_GMTOFF - out->TM_GMTOFF = 0; -#endif + out->tm_mday = 1; // range is 1-31 + + if (argc == 0 || janet_checktype(argv[0], JANET_NIL)) return janet_wrap_abstract(out); + + JanetDictView view = janet_getdictionary(argv, 0); for (int32_t i = 0; i < view.cap; i++) { const JanetKV e = view.kvs[i]; |
