diff options
| author | 2023-05-31 16:46:43 +0200 | |
|---|---|---|
| committer | 2023-05-31 16:46:43 +0200 | |
| commit | b880ff8ba94cf389c988dda55c3e9c13678531c2 (patch) | |
| tree | 53aa391ddc5f3fecf423d1e646abd45efa3440e2 | |
| parent | make GNU expose tm_gmtoff and tm_zone (diff) | |
add timegm
| -rw-r--r-- | src/date.h | 2 | ||||
| -rw-r--r-- | src/polyfill.h | 9 | ||||
| -rw-r--r-- | src/tm.c | 24 |
3 files changed, 35 insertions, 0 deletions
@@ -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 @@ -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 }; |
