Commit 2a78c2da

2a78c2da2d2fMerged

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

Description

Merge pull request #565 from tomoyukiinoue/pr/time-accessors

feat(codegen,analyze,runtime): local Time.new, accessors, scalar inspect, comparison

Details

Commit
2a78c2da2d2f
Parent
06be9d56b1d2
Merge parent
e7a21d11862a

lib/sp_runtime.hmodifiedView file

@@ -154,17 +154,19 @@
154154 `is_utc` distinguishes UTC-coerced times (set by
155155 `Time#utc`) from local-zone times — a presentation-only flag,
156156 the underlying epoch is the same instant either way. iso8601 /
157- strftime check the flag at format time. C99 compound literals
158- zero-init missing fields, so existing `(sp_Time){sec, nsec}`
159- sites stay valid (default to local zone). */
157+ strftime check the flag at format time. Every `(sp_Time){...}`
158+ site initializes all three fields explicitly (is_utc=0 for the
159+ local-zone constructors); C99 would zero-init a missing field,
160+ but spelling it out keeps intent visible and -Wmissing-field-
161+ initializers clean. */
160162 typedef struct { int64_t tv_sec; int32_t tv_nsec; int8_t is_utc; } sp_Time;
161163 static inline sp_Time sp_time_now(void) {
162164 struct timespec ts;
163165 clock_gettime(CLOCK_REALTIME, &ts);
164- return (sp_Time){ ts.tv_sec, (int32_t)ts.tv_nsec };
166+ return (sp_Time){ ts.tv_sec, (int32_t)ts.tv_nsec, 0 };
165167 }
166168 static inline sp_Time sp_time_at_int(mrb_int sec) {
167- return (sp_Time){ (int64_t)sec, 0 };
169+ return (sp_Time){ (int64_t)sec, 0, 0 };
168170 }
169171 /* POSIX convention: keep tv_nsec in [0, 1e9). For negative epoch with a
170172 non-integer fractional part, decrement tv_sec and roll the fraction
@@ -176,7 +178,7 @@
176178 sec -= 1;
177179 frac += 1.0;
178180 }
179- return (sp_Time){ sec, (int32_t)(frac * 1e9) };
181+ return (sp_Time){ sec, (int32_t)(frac * 1e9), 0 };
180182 }
181183
182184
@@ -373,6 +375,108 @@
373375 return t;
374376 }
375377
378+/* ---- Time broken-down accessors / local Time.new / scalar inspect ----
379+ sp_time_vtm is the single zone resolver: is_utc selects gmtime vs
380+ localtime, and for local times the UTC offset is computed via
381+ mktime(gmtime(s))-s (same portable technique as sp_time_iso8601's
382+ #414 follow-up; avoids relying on strftime %z which MSVCRT renders
383+ as a name). The sp_Time struct is unchanged. */
384+static void sp_time_vtm(sp_Time t, struct tm *bd, int32_t *off, char *zbuf) {
385+ time_t s = (time_t)t.tv_sec;
386+ if (t.is_utc) {
387+ struct tm *g = gmtime(&s);
388+ if (g) { *bd = *g; } else { memset(bd, 0, sizeof(*bd)); }
389+ if (off) *off = 0;
390+ if (zbuf) { zbuf[0]='U'; zbuf[1]='T'; zbuf[2]='C'; zbuf[3]=0; }
391+ } else {
392+ struct tm *l = localtime(&s);
393+ if (l) { *bd = *l; } else { memset(bd, 0, sizeof(*bd)); }
394+ if (off) {
395+ struct tm gm = *gmtime(&s);
396+ gm.tm_isdst = -1;
397+ *off = (int32_t)(s - (time_t)mktime(&gm));
398+ }
399+ if (zbuf) {
400+ if (strftime(zbuf, 8, "%Z", bd) == 0) zbuf[0] = 0;
401+ }
402+ }
403+}
404+
405+/* Time.new(y[,mo[,d[,h[,mi[,s]]]]]) — local construction. mktime
406+ interprets the broken-down value in the host local zone and resolves
407+ DST itself (tm_isdst=-1), matching CRuby's local Time.new. timegm /
408+ days-from-civil are intentionally not used. is_utc=0, struct
409+ unchanged. The fixed-offset 7-arg form is a separate Issue. */
410+static sp_Time sp_time_new(mrb_int y, mrb_int mo, mrb_int d,
411+ mrb_int h, mrb_int mi, mrb_int s) {
412+ struct tm tm;
413+ memset(&tm, 0, sizeof(tm));
414+ tm.tm_year = (int)y - 1900;
415+ tm.tm_mon = (int)mo - 1;
416+ tm.tm_mday = (int)d;
417+ tm.tm_hour = (int)h;
418+ tm.tm_min = (int)mi;
419+ tm.tm_sec = (int)s;
420+ tm.tm_isdst = -1;
421+ time_t e = mktime(&tm);
422+ return (sp_Time){ (int64_t)e, 0, 0 };
423+}
424+
425+static mrb_int sp_time_year(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)(b.tm_year+1900);}
426+static mrb_int sp_time_mon(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)(b.tm_mon+1);}
427+static mrb_int sp_time_mday(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)b.tm_mday;}
428+static mrb_int sp_time_hour(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)b.tm_hour;}
429+static mrb_int sp_time_min(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)b.tm_min;}
430+static mrb_int sp_time_sec(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)b.tm_sec;}
431+static mrb_int sp_time_wday(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)b.tm_wday;}
432+static mrb_int sp_time_yday(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)(b.tm_yday+1);}
433+static mrb_int sp_time_isdst(sp_Time t){struct tm b;sp_time_vtm(t,&b,NULL,NULL);return (mrb_int)(b.tm_isdst>0?1:0);}
434+/* Time + Numeric / Time - Numeric. secs may be fractional (CRuby's
435+ Numeric includes Float), so split into whole seconds plus a
436+ sub-second carry and keep tv_nsec normalized to [0,1e9). is_utc is
437+ inherited from the receiver. */
438+static sp_Time sp_time_add(sp_Time t, mrb_float secs) {
439+ int64_t whole = (int64_t)secs;
440+ mrb_float frac = secs - (mrb_float)whole;
441+ int64_t ns = (int64_t)t.tv_nsec + (int64_t)(frac * 1e9);
442+ int64_t carry = ns / 1000000000;
443+ ns -= carry * 1000000000;
444+ if (ns < 0) { ns += 1000000000; carry -= 1; }
445+ return (sp_Time){ t.tv_sec + whole + carry, (int32_t)ns, t.is_utc };
446+}
447+static mrb_int sp_time_utc_offset(sp_Time t){int32_t o;struct tm b;sp_time_vtm(t,&b,&o,NULL);return (mrb_int)o;}
448+static const char *sp_time_zone(sp_Time t){char z[8];struct tm b;sp_time_vtm(t,&b,NULL,z);return sp_str_dup_external(z);}
449+
450+/* Scalar Time inspect. CRuby form: local "YYYY-MM-DD HH:MM:SS +0900",
451+ UTC "YYYY-MM-DD HH:MM:SS UTC". The poly-box path keeps its own
452+ sp_Time_inspect; this value-taking variant is for the scalar
453+ p/puts/to_s codegen path that previously fell into the integer
454+ printf cast and failed C compilation. */
455+static const char *sp_time_inspect_v(sp_Time t) {
456+ char buf[40];
457+ struct tm b;
458+ int32_t off;
459+ sp_time_vtm(t, &b, &off, NULL);
460+ size_t n = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &b);
461+ if (n == 0) {
462+ snprintf(buf, sizeof(buf), "Time(%lld)", (long long)t.tv_sec);
463+ return sp_str_dup_external(buf);
464+ }
465+ if (t.is_utc) {
466+ buf[n++]=' '; buf[n++]='U'; buf[n++]='T'; buf[n++]='C'; buf[n]=0;
467+ } else {
468+ char sign = off >= 0 ? '+' : '-';
469+ long a = off < 0 ? -(long)off : (long)off;
470+ int oh = (int)(a / 3600);
471+ int om = (int)((a / 60) % 60);
472+ buf[n++]=' '; buf[n++]=sign;
473+ buf[n++]=(char)('0'+oh/10); buf[n++]=(char)('0'+oh%10);
474+ buf[n++]=(char)('0'+om/10); buf[n++]=(char)('0'+om%10);
475+ buf[n]=0;
476+ }
477+ return sp_str_dup_external(buf);
478+}
479+
376480 #define SP_GC_STACK_MAX 65536
377481 static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;
378482 /* GC root tracking. SP_GC_ROOT registers a stack-resident root with a