Commit 623100cc
623100cceea8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
feat(codegen): Time#utc + UTC-aware iso8601/strftime (#418)
Sibling to #414 (iso8601 / strftime). Time#utc was unresolved
on a Time receiver -- emitted a cannot resolve call to 'utc' on time (emitting 0) warning and the result fell back to int. The
canonical chain Time.now.utc.iso8601 (used by ActiveRecord's
fill_timestamps to produce UTC-suffixed timestamps) broke one
step earlier than the iso8601-on-Time-direct path that #414 had
just unblocked.
Runtime additions in lib/sp_runtime.h:
-
sp_Timegains anint8_t is_utcfield. C99 compound literals zero-init missing fields, so existing(sp_Time){sec, nsec}sites stay valid (default to local zone) and ABI compatibility is preserved. -
sp_time_utc(t)returns the same instant with is_utc set. Presentation-only flag -- the underlying epoch is the same whether or not it's UTC-flagged. Mirrors CRuby's internal "gmt" flag; both Time.now and Time.now.utc represent the same point-in-time, the difference is in formatting. -
sp_time_iso8601checks is_utc: when set, formats against gmtime and emits the trailing "Z" suffix CRuby uses for UTC iso8601 ("2026-05-10T14:35:32Z"). The local path stays at "%Y-%m-%dT%H:%M:%S" + portable mktime-computed offset from #414's CI follow-up, unchanged. -
sp_time_strftimeselects gmtime vs localtime by is_utc sot.utc.strftime("%H")runs against UTC hour-of-day.
Inference: infer_method_name_type returns "time" when mname is
"utc" and recv resolves to time. Same gating shape as #414's
iso8601/strftime arms.
Codegen: compile_object_method_expr's recv_type == "time" arm
dispatches utc to sp_time_utc.
Test time_utc.rb: covers the canonical Time.now.utc.iso8601 chain (output ends in "Z"), strftime against UTC (%H is a 2-digit hour), idempotent re-utc (no double-flip / shape change), and a regression check that local-zone iso8601 still works.
Closes #418
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 623100cceea8
- Parent
- 3c07fba9b1ad
lib/sp_runtime.hmodifiedView file
/* ---- Time runtime ---- *//* sp_Time keeps Time.now / Time.at as value-typed structs. d78149b's sub-second precision is preserved by inlining tv_sec + tv_nsec/1e9 at every Time#to_f / Time#- call site (see spinel_codegen.rb). */typedef struct { int64_t tv_sec; int32_t tv_nsec; } sp_Time; at every Time#to_f / Time#- call site (see spinel_codegen.rb). Issue #418: `is_utc` distinguishes UTC-coerced times (set by `Time#utc`) from local-zone times — a presentation-only flag, the underlying epoch is the same instant either way. iso8601 / strftime check the flag at format time. C99 compound literals zero-init missing fields, so existing `(sp_Time){sec, nsec}` sites stay valid (default to local zone). */typedef struct { int64_t tv_sec; int32_t tv_nsec; int8_t is_utc; } sp_Time;static inline sp_Time sp_time_now(void) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts);}/* Issue #414: Time#strftime — format the time per `fmt` using libc strftime against the local-time broken-down value. Returns a freshly-allocated string (sp_str_dup_external'd into the str heap so GC tracks it). The 256-byte buffer is enough for every format CRuby builds in (the longest is %c which produces ~25 bytes); custom formats that exceed it get truncated. */ strftime. The is_utc flag (#418) selects gmtime vs localtime so a `Time.now.utc.strftime("%H")` runs against the UTC broken-down value. Returns a freshly-allocated string (sp_str_dup_external'd into the str heap so GC tracks it). The 256-byte buffer is enough for every format CRuby builds in (the longest is %c which produces ~25 bytes); custom formats that exceed it get truncated. */static const char *sp_time_strftime(sp_Time t, const char *fmt) { char buf[256]; time_t s = (time_t)t.tv_sec; struct tm *lt = localtime(&s); struct tm *lt = t.is_utc ? gmtime(&s) : localtime(&s); if (lt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), fmt, lt); if (n == 0) return SPL("");/* 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. `mktime(gmtime(s)) - s` rather than relying on strftime's %z, because MSVCRT's %z emits the timezone *name* rather than ±HHMM, so a Windows-MinGW build of the same code produces 30+ char strings like "...Coordinated Universal Time" instead of CRuby's "...+09:00". 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. */ Phase 1 doesn't support. Issue #418: when is_utc is set, format against gmtime and emit the trailing "Z" suffix CRuby uses for UTC iso8601. */static const char *sp_time_iso8601(sp_Time t) { char buf[64]; time_t s = (time_t)t.tv_sec; if (t.is_utc) { struct tm *gt = gmtime(&s); if (gt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gt); if (n == 0) return SPL(""); buf[n] = 0; return sp_str_dup_external(buf); } struct tm *lt = localtime(&s); if (lt == NULL) return SPL(""); size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", lt); return sp_str_dup_external(buf);}/* Issue #418: Time#utc — same instant, presentation flag flipped to UTC. iso8601 / strftime check is_utc at format time. CRuby's Time internally tracks a similar "gmt" flag; the value-typed sp_Time stores it inline. */static inline sp_Time sp_time_utc(sp_Time t) { t.is_utc = 1; return t;}#define SP_GC_STACK_MAX 65536static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;/* GC root tracking. SP_GC_ROOT registers a stack-resident root with aspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/time_utc.rbaddedView file
Loading diff…
test/time_utc.rb.expectedaddedView file
Loading diff…