Commit b1ab1b9f
b1ab1b9f9f3aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(runtime): portable Time#iso8601 offset (#414 CI follow-up)
The Windows MinGW build of the #414 test was failing on CI. Root
cause: sp_time_iso8601 used strftime("%z", lt) to format the
UTC offset. POSIX defines %z as ±HHMM (the format the test
asserted shape against), but MSVCRT's strftime emits the timezone
name instead -- something like "Coordinated Universal Time" or
"Pacific Standard Time", locale-sensitive and 20+ characters
long. The post-splice output blew past the test's length range
and failed.
Fix: stop relying on %z. Compute the offset manually via
mktime(gmtime(s)) - s (the gmtime broken-down value
re-interpreted as if it were local-time gives the offset in
seconds), then format ±HH:MM ourselves. Same answer on glibc /
MinGW / macOS / FreeBSD; no platform-specific %z handling.
Also tighten the test's iso8601 assertion: with the offset format now stable, length is exactly 25 ("YYYY-MM-DDTHH:MM:SS[+-]HH:MM") and per-position separator characters are well-defined. The previous shape check accepted broader-but-still-wrong outputs because it only looked at one position.
Test runs locally on Linux and the bootstrap fixpoint is stable. The Windows MinGW path is the regression this fix targets.
Refs #414
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b1ab1b9f9f3a
- Parent
- 8a1f3ae7a271
lib/sp_runtime.hmodifiedView file
return sp_str_dup_external(buf);}/* Issue #414: Time#iso8601 — RFC 3339 style "%Y-%m-%dT%H:%M:%S%:z". strftime's %z gives the offset as ±HHMM, so we splice in the colon manually to match CRuby's iso8601 output. Sub-second precision is intentionally omitted: CRuby's iso8601 also drops it unless the caller passes a precision arg, which Phase 1 doesn't support. *//* Issue #414: Time#iso8601 — RFC 3339 style. Format the date+time prefix with strftime, then compute the UTC offset manually via `mktime(gmtime(s)) - s` rather than relying on strftime's %z. The original implementation used %z, which is POSIX-defined as ±HHMM but Windows MSVCRT instead emits the timezone *name* (e.g. "Coordinated Universal Time") — long, locale-sensitive, and unsuitable for an iso8601 string. Computing the offset from the gmtime/mktime difference gives the same answer on every libc we target. Sub-second precision is intentionally omitted: CRuby's iso8601 also drops it unless the caller passes a precision arg, which Phase 1 doesn't support. */static const char *sp_time_iso8601(sp_Time t) { char buf[64]; time_t s = (time_t)t.tv_sec; struct tm *lt = localtime(&s); if (lt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", lt); if (n == 0) return SPL(""); /* Insert a colon between the offset's hour and minute halves (e.g. "-0400" -> "-04:00"). strftime %z always emits 5 chars at the tail; if it's missing for some libc, leave the buffer alone. */ if (n >= 5 && (buf[n - 5] == '+' || buf[n - 5] == '-')) { char m1 = buf[n - 2], m2 = buf[n - 1]; buf[n - 2] = ':'; buf[n - 1] = m1; buf[n] = m2; buf[n + 1] = 0; n++; } else { buf[n] = 0; } size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", lt); if (n == 0 || n + 6 >= sizeof(buf)) return SPL(""); /* Compute UTC offset portably: re-interpret gmtime's broken-down value as if it were local-time via mktime, then the difference from the original epoch is the offset in seconds. mktime mutates tm_isdst, so use a fresh copy. */ struct tm gm = *gmtime(&s); gm.tm_isdst = -1; time_t gm_as_if_local = mktime(&gm); long offset_sec = (long)(s - gm_as_if_local); char sign = offset_sec >= 0 ? '+' : '-'; long abs_off = offset_sec < 0 ? -offset_sec : offset_sec; int oh = (int)(abs_off / 3600); int om = (int)((abs_off / 60) % 60); buf[n++] = sign; buf[n++] = (char)('0' + (oh / 10)); buf[n++] = (char)('0' + (oh % 10)); buf[n++] = ':'; buf[n++] = (char)('0' + (om / 10)); buf[n++] = (char)('0' + (om % 10)); buf[n] = 0; return sp_str_dup_external(buf);}test/time_iso8601_strftime.rbmodifiedView file
Loading diff…