time.c: optimize getutc and getlocal to skip unnecessary datetime updates

skip time_update_datetime() call when timezone conversion is not needed,
eliminating expensive gmtime_r/localtime_r system calls for redundant
conversions like time.utc.getutc or time.local.getlocal.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-06 09:07:45 +09:00
parent 731926cd72
commit 9c4e70ed01
+8 -4
View File
@@ -1107,8 +1107,10 @@ time_getutc(mrb_state *mrb, mrb_value self)
struct mrb_time *tm = time_get_ptr(mrb, self);
struct mrb_time *tm2 = (struct mrb_time*)mrb_malloc(mrb, sizeof(*tm));
*tm2 = *tm;
tm2->timezone = MRB_TIMEZONE_UTC;
time_update_datetime(mrb, tm2, TRUE);
if (tm2->timezone != MRB_TIMEZONE_UTC) {
tm2->timezone = MRB_TIMEZONE_UTC;
time_update_datetime(mrb, tm2, TRUE);
}
return time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}
@@ -1129,8 +1131,10 @@ time_getlocal(mrb_state *mrb, mrb_value self)
struct mrb_time *tm = time_get_ptr(mrb, self);
struct mrb_time *tm2 = (struct mrb_time*)mrb_malloc(mrb, sizeof(*tm));
*tm2 = *tm;
tm2->timezone = MRB_TIMEZONE_LOCAL;
time_update_datetime(mrb, tm2, TRUE);
if (tm2->timezone != MRB_TIMEZONE_LOCAL) {
tm2->timezone = MRB_TIMEZONE_LOCAL;
time_update_datetime(mrb, tm2, TRUE);
}
return time_wrap(mrb, mrb_obj_class(mrb, self), tm2);
}