Skip to content
Myr

Add ISO 8601-specific strptime

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Implement `strptime` for systems lacking it, but only supporting the ISO 8601 format string.

authored by Jason Hood · committed by Nico Williams · parent 87594f8 · Browse files · 1 file+25 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−9 removed

  • other:+25 added−9 removed

1 file+25 added−9 removed

modified:src/builtin.c+25 added-9 removed
1306 unmodified lines
1307 assert(yday == tm->tm_yday || tm->tm_yday == 367);1308 tm->tm_yday = yday;1309}13101311#ifdef HAVE_STRPTIME1311#ifndef HAVE_STRPTIME1312static char *strptime(const char *s, const char *format, struct tm *tm) {1313 if (strcmp(format, "%Y-%m-%dT%H:%M:%SZ"))1314 return NULL;13151316 int count, end;1317 count = sscanf(s, "%d-%d-%dT%d:%d:%d%n",1318 &tm->tm_year, &tm->tm_mon, &tm->tm_mday,1319 &tm->tm_hour, &tm->tm_min, &tm->tm_sec,1320 &end );1321 if (count == 6 && s[end] == 'Z') {1322 tm->tm_year -= 1900;1323 tm->tm_mon--;1324 return (char*)s + end + 1;1325 }1326 return NULL;1327}1328#endif13291330static jv f_strptime(jq_state *jq, jv a, jv b) {1331 if (jv_get_kind(a) != JV_KIND_STRING || jv_get_kind(b) != JV_KIND_STRING) {1332 return ret_error2(a, b, jv_string("strptime/1 requires string inputs and arguments"));1333 }
3 unmodified lines
1337 tm.tm_wday = 8; // sentinel1338 tm.tm_yday = 367; // sentinel1339 const char *input = jv_string_value(a);1340 const char *fmt = jv_string_value(b);1323 const char *end = strptime(input, fmt, &tm);13411342#ifndef HAVE_STRPTIME1343 if (strcmp(fmt, "%Y-%m-%dT%H:%M:%SZ")) {1344 return ret_error2(a, b, jv_string("strptime/1 only supports ISO 8601 on this platform"));1345 }1346#endif1347 const char *end = strptime(input, fmt, &tm);1348 if (end == NULL || (*end != '\0' && !isspace(*end))) {1349 return ret_error2(a, b, jv_string_fmt("date \"%s\" does not match format \"%s\"", input, fmt));1350 }1351 jv_free(b);
28 unmodified lines
1380 r = jv_array_append(r, jv_string(end));1381 jv_free(a); // must come after `*end` because `end` is a pointer into `a`'s string1382 return r;1383}1361#else1362static jv f_strptime(jq_state *jq, jv a, jv b) {1363 jv_free(a);1364 jv_free(b);1365 return jv_invalid_with_msg(jv_string("strptime/1 not implemented on this platform"));1366}1367#endif13841385#define TO_TM_FIELD(t, j, i) \1386 do { \1387 jv n = jv_array_get(jv_copy(j), (i)); \
440 unmodified lines