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

@@ -258,31 +258,44 @@
258258 return sp_str_dup_external(buf);
259259 }
260260
261-/* Issue #414: Time#iso8601 — RFC 3339 style "%Y-%m-%dT%H:%M:%S%:z".
262- strftime's %z gives the offset as ±HHMM, so we splice in the colon
263- manually to match CRuby's iso8601 output. Sub-second precision is
264- intentionally omitted: CRuby's iso8601 also drops it unless the
265- caller passes a precision arg, which Phase 1 doesn't support. */
261+/* Issue #414: Time#iso8601 — RFC 3339 style. Format the date+time
262+ prefix with strftime, then compute the UTC offset manually via
263+ `mktime(gmtime(s)) - s` rather than relying on strftime's %z.
264+ The original implementation used %z, which is POSIX-defined as
265+ ±HHMM but Windows MSVCRT instead emits the timezone *name*
266+ (e.g. "Coordinated Universal Time") — long, locale-sensitive,
267+ and unsuitable for an iso8601 string. Computing the offset
268+ from the gmtime/mktime difference gives the same answer on
269+ every libc we target.
270+ Sub-second precision is intentionally omitted: CRuby's iso8601
271+ also drops it unless the caller passes a precision arg, which
272+ Phase 1 doesn't support. */
266273 static const char *sp_time_iso8601(sp_Time t) {
267274 char buf[64];
268275 time_t s = (time_t)t.tv_sec;
269276 struct tm *lt = localtime(&s);
270277 if (lt == NULL) return SPL("");
271- size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", lt);
272- if (n == 0) return SPL("");
273- /* Insert a colon between the offset's hour and minute halves
274- (e.g. "-0400" -> "-04:00"). strftime %z always emits 5 chars at
275- the tail; if it's missing for some libc, leave the buffer alone. */
276- if (n >= 5 && (buf[n - 5] == '+' || buf[n - 5] == '-')) {
277- char m1 = buf[n - 2], m2 = buf[n - 1];
278- buf[n - 2] = ':';
279- buf[n - 1] = m1;
280- buf[n] = m2;
281- buf[n + 1] = 0;
282- n++;
283- } else {
284- buf[n] = 0;
285- }
278+ size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", lt);
279+ if (n == 0 || n + 6 >= sizeof(buf)) return SPL("");
280+ /* Compute UTC offset portably: re-interpret gmtime's broken-down
281+ value as if it were local-time via mktime, then the difference
282+ from the original epoch is the offset in seconds. mktime mutates
283+ tm_isdst, so use a fresh copy. */
284+ struct tm gm = *gmtime(&s);
285+ gm.tm_isdst = -1;
286+ time_t gm_as_if_local = mktime(&gm);
287+ long offset_sec = (long)(s - gm_as_if_local);
288+ char sign = offset_sec >= 0 ? '+' : '-';
289+ long abs_off = offset_sec < 0 ? -offset_sec : offset_sec;
290+ int oh = (int)(abs_off / 3600);
291+ int om = (int)((abs_off / 60) % 60);
292+ buf[n++] = sign;
293+ buf[n++] = (char)('0' + (oh / 10));
294+ buf[n++] = (char)('0' + (oh % 10));
295+ buf[n++] = ':';
296+ buf[n++] = (char)('0' + (om / 10));
297+ buf[n++] = (char)('0' + (om % 10));
298+ buf[n] = 0;
286299 return sp_str_dup_external(buf);
287300 }
288301