aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChloe Kudryavtsev <code@toast.bunkerlabs.net>2023-06-17 22:20:11 +0200
committerChloe Kudryavtsev <code@toast.bunkerlabs.net>2023-06-17 22:20:11 +0200
commit36696695692ae816e8a3d442b7647e3aba1feba5 (patch)
tree4a06bc97e2e1091657e8884022407ee8ecea3add /src
parentminor native sources cleanup (diff)
allow constructing your own date/tm values
Diffstat (limited to 'src')
-rw-r--r--src/time.c10
-rw-r--r--src/tm.c15
2 files changed, 16 insertions, 9 deletions
diff --git a/src/time.c b/src/time.c
index f9bda70..eee419e 100644
--- a/src/time.c
+++ b/src/time.c
@@ -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 = {
diff --git a/src/tm.c b/src/tm.c
index f04e9a9..a029627 100644
--- a/src/tm.c
+++ b/src/tm.c
@@ -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];