mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user