aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChloe Kudryavtsev <code@toast.bunkerlabs.net>2023-05-31 16:46:43 +0200
committerChloe Kudryavtsev <code@toast.bunkerlabs.net>2023-05-31 16:46:43 +0200
commitb880ff8ba94cf389c988dda55c3e9c13678531c2 (patch)
tree53aa391ddc5f3fecf423d1e646abd45efa3440e2 /src
parentmake GNU expose tm_gmtoff and tm_zone (diff)
add timegm
Diffstat (limited to 'src')
-rw-r--r--src/date.h2
-rw-r--r--src/polyfill.h9
-rw-r--r--src/tm.c24
3 files changed, 35 insertions, 0 deletions
diff --git a/src/date.h b/src/date.h
index d4b0680..bda32a0 100644
--- a/src/date.h
+++ b/src/date.h
@@ -27,4 +27,6 @@ struct tm *jd_opttm(Janet *argv, int32_t argc, int32_t n);
struct tm *jd_maketm(void);
JANET_CFUN(jd_mktime);
JANET_CFUN(jd_mktime_inplace);
+JANET_CFUN(jd_timegm);
+JANET_CFUN(jd_timegm_inplace);
JANET_CFUN(jd_strftime);
diff --git a/src/polyfill.h b/src/polyfill.h
index ecc3a0d..70ea4f5 100644
--- a/src/polyfill.h
+++ b/src/polyfill.h
@@ -1,5 +1,6 @@
#pragma once
#include <janet.h>
+#include <time.h>
#if JANET_VERSION_MAJOR < 2 && JANET_VERSION_MINOR < 28
#define POLYFILL_CBYTES
@@ -20,6 +21,14 @@ const char *janet_optcbytes(const Janet *argv, int32_t argc, int32_t n, const ch
#endif // !defined(JANET_NO_SOURCEMAPS)
#endif // JANET_VERSION_MAJOR < 2 && JANET_VERSION_MINOR < 28
+// timegm
+#ifdef _MSC_VER
+#define timegm _mkgmtime
+#else
+// since tm is a pointer, it should be ABI-compatible even if the true type is mismatched
+time_t timegm(struct tm *tm);
+#endif
+
// ===
// public domain code from
diff --git a/src/tm.c b/src/tm.c
index 9c2a91f..4d5d1d5 100644
--- a/src/tm.c
+++ b/src/tm.c
@@ -278,6 +278,28 @@ JANET_FN(jd_mktime_inplace,
return janet_wrap_abstract(time);
}
+JANET_FN(jd_timegm,
+ "",
+ "") {
+ janet_fixarity(argc, 1);
+ struct tm *tm = jd_gettm(argv, 0);
+ struct tm *nw = jd_maketm();
+ *nw = *tm;
+ time_t *time = jd_maketime();
+ *time = timegm(nw);
+ return janet_wrap_abstract(time);
+}
+
+JANET_FN(jd_timegm_inplace,
+ "",
+ "") {
+ janet_fixarity(argc, 1);
+ struct tm *tm = jd_gettm(argv, 0);
+ time_t *time = jd_maketime();
+ *time = timegm(tm);
+ return janet_wrap_abstract(time);
+}
+
// strftime
struct strftime_format {
const char *keyword;
@@ -322,6 +344,8 @@ const JanetRegExt jd_tm_cfuns[] = {
JANET_REG("tm", jd_tm),
JANET_REG("mktime", jd_mktime),
JANET_REG("mktime!", jd_mktime_inplace),
+ JANET_REG("timegm", jd_timegm),
+ JANET_REG("timegm!", jd_timegm_inplace),
JANET_REG("strftime", jd_strftime),
JANET_REG_END
};