Skip to content
Myr

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

2 files+69 added−0 removed

modified:src/builtin.c+67 added
1281 unmodified lines
1282 return jv_invalid_with_msg(jv_string("gmtime not implemented on this platform"));1283}1284#endif12851286#ifdef HAVE_LOCALTIME_R1287static 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_GMTIME1302static 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#else1317static 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#endif13221323#ifdef HAVE_STRFTIME1324static 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 lines
1347 return jv_invalid_with_msg(jv_string("strftime/1 not implemented on this platform"));1348}1349#endif13501351#ifdef HAVE_STRFTIME1352static 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#else1372static 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#endif13781379#ifdef HAVE_GETTIMEOFDAY1380static jv f_now(jq_state *jq, jv a) {1381 jv_free(a);1382 struct timeval tv;
94 unmodified lines
1477 {(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 lines
modified:configure.ac+2 added
125 unmodified lines
126AC_FIND_FUNC([strftime], [c], [#include <time.h>], [0, 0, 0, 0])127AC_FIND_FUNC([timegm], [c], [#include <time.h>], [0])128AC_FIND_FUNC([gmtime_r], [c], [#include <time.h>], [0, 0])129AC_FIND_FUNC([gmtime], [c], [#include <time.h>], [0])130AC_FIND_FUNC([localtime_r], [c], [#include <time.h>], [0, 0])131AC_FIND_FUNC([localtime], [c], [#include <time.h>], [0])132AC_FIND_FUNC([gettimeofday], [c], [#include <time.h>], [0, 0])133AC_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>]])135AC_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