Add `localtime` and `strflocaltime` (fix #1349)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nicolas Williams · parent 607a9e3 · Browse files · 2 files·+69 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+69 added−0 removed
- other:+69 added−0 removed
Provenance
- Commit
06f2060- Tree
23eed72- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
607a9e3
2 files changed+69 added−0 removed
modified:src/builtin.c+67 added
1281 unmodified lines1282 return jv_invalid_with_msg(jv_string("gmtime not implemented on this platform"));1283 }1284 #endif1285 1286+#ifdef HAVE_LOCALTIME_R1287+static jv f_localtime(jq_state *jq, jv a) {1288+ if (jv_get_kind(a) != JV_KIND_NUMBER)1289+ return jv_invalid_with_msg(jv_string("localtime() requires numeric inputs"));1290+ struct tm tm, *tmp;1291+ memset(&tm, 0, sizeof(tm));1292+ double fsecs = jv_number_value(a);1293+ time_t secs = fsecs;1294+ jv_free(a);1295+ tmp = localtime_r(&secs, &tm);1296+ if (tmp == NULL)1297+ return jv_invalid_with_msg(jv_string("errror converting number of seconds since epoch to datetime"));1298+ a = tm2jv(tmp);1299+ return jv_array_set(a, 5, jv_number(jv_number_value(jv_array_get(jv_copy(a), 5)) + (fsecs - floor(fsecs))));1300+}1301+#elif defined HAVE_GMTIME1302+static jv f_localtime(jq_state *jq, jv a) {1303+ if (jv_get_kind(a) != JV_KIND_NUMBER)1304+ return jv_invalid_with_msg(jv_string("localtime requires numeric inputs"));1305+ struct tm tm, *tmp;1306+ memset(&tm, 0, sizeof(tm));1307+ double fsecs = jv_number_value(a);1308+ time_t secs = fsecs;1309+ jv_free(a);1310+ tmp = localtime(&secs);1311+ if (tmp == NULL)1312+ return jv_invalid_with_msg(jv_string("errror converting number of seconds since epoch to datetime"));1313+ a = tm2jv(tmp);1314+ return jv_array_set(a, 5, jv_number(jv_number_value(jv_array_get(jv_copy(a), 5)) + (fsecs - floor(fsecs))));1315+}1316+#else1317+static jv f_localtime(jq_state *jq, jv a) {1318+ jv_free(a);1319+ return jv_invalid_with_msg(jv_string("localtime not implemented on this platform"));1320+}1321+#endif1322+1323 #ifdef HAVE_STRFTIME1324 static jv f_strftime(jq_state *jq, jv a, jv b) {1325 if (jv_get_kind(a) == JV_KIND_NUMBER) {1326 a = f_gmtime(jq, a);20 unmodified lines1347 return jv_invalid_with_msg(jv_string("strftime/1 not implemented on this platform"));1348 }1349 #endif1350 1351+#ifdef HAVE_STRFTIME1352+static jv f_strflocaltime(jq_state *jq, jv a, jv b) {1353+ if (jv_get_kind(a) == JV_KIND_NUMBER) {1354+ a = f_localtime(jq, a);1355+ } else if (jv_get_kind(a) != JV_KIND_ARRAY) {1356+ return jv_invalid_with_msg(jv_string("strflocaltime/1 requires parsed datetime inputs"));1357+ }1358+ struct tm tm;1359+ if (!jv2tm(a, &tm))1360+ return jv_invalid_with_msg(jv_string("strflocaltime/1 requires parsed datetime inputs")); \1361+ const char *fmt = jv_string_value(b);1362+ size_t alloced = strlen(fmt) + 100;1363+ char *buf = alloca(alloced);1364+ size_t n = strftime(buf, alloced, fmt, &tm);1365+ jv_free(b);1366+ /* POSIX doesn't provide errno values for strftime() failures; weird */1367+ if (n == 0 || n > alloced)1368+ return jv_invalid_with_msg(jv_string("strflocaltime/1: unknown system failure"));1369+ return jv_string(buf);1370+}1371+#else1372+static jv f_strflocaltime(jq_state *jq, jv a) {1373+ jv_free(a);1374+ jv_free(b);1375+ return jv_invalid_with_msg(jv_string("strflocaltime/1 not implemented on this platform"));1376+}1377+#endif1378+1379 #ifdef HAVE_GETTIMEOFDAY1380 static jv f_now(jq_state *jq, jv a) {1381 jv_free(a);1382 struct timeval tv;94 unmodified lines1477 {(cfunction_ptr)f_debug, "debug", 1},1478 {(cfunction_ptr)f_stderr, "stderr", 1},1479 {(cfunction_ptr)f_strptime, "strptime", 2},1480 {(cfunction_ptr)f_strftime, "strftime", 2},1481+ {(cfunction_ptr)f_strflocaltime, "strflocaltime", 2},1482 {(cfunction_ptr)f_mktime, "mktime", 1},1483 {(cfunction_ptr)f_gmtime, "gmtime", 1},1484+ {(cfunction_ptr)f_localtime, "localtime", 1},1485 {(cfunction_ptr)f_now, "now", 1},1486 {(cfunction_ptr)f_current_filename, "input_filename", 1},1487 {(cfunction_ptr)f_current_line, "input_line_number", 1},1488 };125 unmodified linesmodified:configure.ac+2 added
125 unmodified lines126 AC_FIND_FUNC([strftime], [c], [#include <time.h>], [0, 0, 0, 0])127 AC_FIND_FUNC([timegm], [c], [#include <time.h>], [0])128 AC_FIND_FUNC([gmtime_r], [c], [#include <time.h>], [0, 0])129 AC_FIND_FUNC([gmtime], [c], [#include <time.h>], [0])130+AC_FIND_FUNC([localtime_r], [c], [#include <time.h>], [0, 0])131+AC_FIND_FUNC([localtime], [c], [#include <time.h>], [0])132 AC_FIND_FUNC([gettimeofday], [c], [#include <time.h>], [0, 0])133 AC_CHECK_MEMBER([struct tm.tm_gmtoff], [AC_DEFINE([HAVE_TM_TM_GMT_OFF],1,[Define to 1 if the system has the tm_gmt_off field in struct tm])],134 [], [[#include <time.h>]])135 AC_CHECK_MEMBER([struct tm.__tm_gmtoff], [AC_DEFINE([HAVE_TM___TM_GMT_OFF],1,[Define to 1 if the system has the __tm_gmt_off field in struct tm])],143 unmodified lines