mruby-time/time.c (mrb_to_time_t): use floor instead of llround

Since we should generate `t=(sec+usec)` from a floating point number
`sec` should be `floor(f)` especially for negative numbers, if the place
for `usec` is specified. In contrast, we can `round` the below point
digits if we are going to ignore them. But we now use `round` instead of
`llround` which directly convert `double` to `long long`, to add
boundary check for conversion.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2023-03-07 22:14:41 +09:00
parent 2f0265d078
commit da8c23524f
+7 -3
View File
@@ -247,11 +247,15 @@ mrb_to_time_t(mrb_state *mrb, mrb_value obj, time_t *usec)
}
if (usec) {
t = (time_t)f;
*usec = (time_t)llround((f - t) * 1.0e+6);
double tt = floor(f);
if (!isfinite(tt)) goto out_of_range;
t = (time_t)tt;
*usec = (time_t)trunc((f - tt) * 1.0e+6);
}
else {
t = (time_t)llround(f);
double tt = round(f);
if (!isfinite(tt)) goto out_of_range;
t = (time_t)tt;
}
}
break;