Commit 318a93a3

318a93a30188Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified

Description

feat(codegen): Time#iso8601 + Time#strftime (#414)

Both methods used to emit cannot resolve call to '...' on time (emitting 0) warnings. Downstream .length/string-concat sites either cascaded more warnings or hit C-compile errors at any typed-string sink.

Runtime additions in lib/sp_runtime.h:

  • sp_time_strftime(t, fmt) delegates to libc strftime against the local-time broken-down value, copies into a 256-byte buffer, then sp_str_dup_external's into the str heap.

  • sp_time_iso8601(t) formats with libc as "%Y-%m-%dT%H:%M:%S%z" (the closest portable spelling of RFC 3339), then splices a colon into the offset's HHMM tail so the output matches CRuby's "+HH:MM" form. Sub-second precision is intentionally omitted -- CRuby drops it too unless the caller passes a precision arg, which Phase 1 doesn't support.

Inference: infer_method_name_type returns "string" when mname is "iso8601" / "strftime" AND recv resolves to "time". Gated on recv_type so user classes that happen to define methods of those names still flow through normal resolution.

Codegen: compile_object_method_expr's recv_type == "time" arm dispatches to the new helpers alongside the existing to_i / to_f arms.

Test time_iso8601_strftime.rb checks shape (length, separator positions) rather than exact strings, since the output depends on the system clock and local-time offset.

Closes #414

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
318a93a30188
Parent
25a2866f5602

lib/sp_runtime.hmodifiedView file

@@ -146,6 +146,7 @@
146146 return (sp_Time){ sec, (int32_t)(frac * 1e9) };
147147 }
148148
149+
149150 typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; } sp_gc_hdr;
150151 static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;
151152
@@ -240,6 +241,51 @@
240241 }
241242 }
242243
244+/* 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. */
250+static const char *sp_time_strftime(sp_Time t, const char *fmt) {
251+ char buf[256];
252+ time_t s = (time_t)t.tv_sec;
253+ struct tm *lt = localtime(&s);
254+ if (lt == NULL) return SPL("");
255+ size_t n = strftime(buf, sizeof(buf), fmt, lt);
256+ if (n == 0) return SPL("");
257+ buf[n] = 0;
258+ return sp_str_dup_external(buf);
259+}
260+
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. */
266+static const char *sp_time_iso8601(sp_Time t) {
267+ char buf[64];
268+ time_t s = (time_t)t.tv_sec;
269+ struct tm *lt = localtime(&s);
270+ 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+ }
286+ return sp_str_dup_external(buf);
287+}
288+
243289 #define SP_GC_STACK_MAX 65536
244290 static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;
245291 /* GC root tracking. SP_GC_ROOT registers a stack-resident root with a