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_Time gains an int8_t is_utc field. 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_iso8601 checks 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_strftime selects gmtime vs localtime by is_utc so t.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

@@ -123,8 +123,14 @@
123123 /* ---- Time runtime ---- */
124124 /* sp_Time keeps Time.now / Time.at as value-typed structs. d78149b's
125125 sub-second precision is preserved by inlining tv_sec + tv_nsec/1e9
126- at every Time#to_f / Time#- call site (see spinel_codegen.rb). */
127-typedef struct { int64_t tv_sec; int32_t tv_nsec; } sp_Time;
126+ at every Time#to_f / Time#- call site (see spinel_codegen.rb).
127+ Issue #418: `is_utc` distinguishes UTC-coerced times (set by
128+ `Time#utc`) from local-zone times — a presentation-only flag,
129+ the underlying epoch is the same instant either way. iso8601 /
130+ strftime check the flag at format time. C99 compound literals
131+ zero-init missing fields, so existing `(sp_Time){sec, nsec}`
132+ sites stay valid (default to local zone). */
133+typedef struct { int64_t tv_sec; int32_t tv_nsec; int8_t is_utc; } sp_Time;
128134 static inline sp_Time sp_time_now(void) {
129135 struct timespec ts;
130136 clock_gettime(CLOCK_REALTIME, &ts);
@@ -242,15 +248,16 @@
242248 }
243249
244250 /* Issue #414: Time#strftime — format the time per `fmt` using libc
245- strftime against the local-time broken-down value. Returns a
246- freshly-allocated string (sp_str_dup_external'd into the str heap
247- so GC tracks it). The 256-byte buffer is enough for every format
248- CRuby builds in (the longest is %c which produces ~25 bytes);
249- custom formats that exceed it get truncated. */
251+ strftime. The is_utc flag (#418) selects gmtime vs localtime so a
252+ `Time.now.utc.strftime("%H")` runs against the UTC broken-down
253+ value. Returns a freshly-allocated string (sp_str_dup_external'd
254+ into the str heap so GC tracks it). The 256-byte buffer is enough
255+ for every format CRuby builds in (the longest is %c which produces
256+ ~25 bytes); custom formats that exceed it get truncated. */
250257 static const char *sp_time_strftime(sp_Time t, const char *fmt) {
251258 char buf[256];
252259 time_t s = (time_t)t.tv_sec;
253- struct tm *lt = localtime(&s);
260+ struct tm *lt = t.is_utc ? gmtime(&s) : localtime(&s);
254261 if (lt == NULL) return SPL("");
255262 size_t n = strftime(buf, sizeof(buf), fmt, lt);
256263 if (n == 0) return SPL("");
@@ -260,19 +267,28 @@
260267
261268 /* Issue #414: Time#iso8601 — RFC 3339 style. Format the date+time
262269 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+ `mktime(gmtime(s)) - s` rather than relying on strftime's %z,
271+ because MSVCRT's %z emits the timezone *name* rather than ±HHMM,
272+ so a Windows-MinGW build of the same code produces 30+ char
273+ strings like "...Coordinated Universal Time" instead of CRuby's
274+ "...+09:00". Computing the offset from the gmtime/mktime
275+ difference gives the same answer on every libc we target.
270276 Sub-second precision is intentionally omitted: CRuby's iso8601
271277 also drops it unless the caller passes a precision arg, which
272- Phase 1 doesn't support. */
278+ Phase 1 doesn't support.
279+ Issue #418: when is_utc is set, format against gmtime and emit the
280+ trailing "Z" suffix CRuby uses for UTC iso8601. */
273281 static const char *sp_time_iso8601(sp_Time t) {
274282 char buf[64];
275283 time_t s = (time_t)t.tv_sec;
284+ if (t.is_utc) {
285+ struct tm *gt = gmtime(&s);
286+ if (gt == NULL) return SPL("");
287+ size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gt);
288+ if (n == 0) return SPL("");
289+ buf[n] = 0;
290+ return sp_str_dup_external(buf);
291+ }
276292 struct tm *lt = localtime(&s);
277293 if (lt == NULL) return SPL("");
278294 size_t n = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", lt);
@@ -299,6 +315,15 @@
299315 return sp_str_dup_external(buf);
300316 }
301317
318+/* Issue #418: Time#utc — same instant, presentation flag flipped to
319+ UTC. iso8601 / strftime check is_utc at format time. CRuby's Time
320+ internally tracks a similar "gmt" flag; the value-typed sp_Time
321+ stores it inline. */
322+static inline sp_Time sp_time_utc(sp_Time t) {
323+ t.is_utc = 1;
324+ return t;
325+}
326+
302327 #define SP_GC_STACK_MAX 65536
303328 static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;
304329 /* GC root tracking. SP_GC_ROOT registers a stack-resident root with a