Merge pull request #2835 from kext/time-rounding-precision

Time rounding precision
This commit is contained in:
Yukihiro "Matz" Matsumoto
2015-06-16 16:36:34 +09:00
2 changed files with 11 additions and 2 deletions
+3 -2
View File
@@ -4,6 +4,7 @@
** See Copyright Notice in mruby.h
*/
#include <math.h>
#include <stdio.h>
#include <time.h>
#include "mruby.h"
@@ -208,12 +209,12 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
out_of_range:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec));
}
tm->usec = (time_t)((sec - tm->sec) * 1.0e6 + usec);
tm->usec = (time_t)llround((sec - tm->sec) * 1.0e6 + usec);
while (tm->usec < 0) {
tm->sec--;
tm->usec += 1000000;
}
while (tm->usec > 1000000) {
while (tm->usec >= 1000000) {
tm->sec++;
tm->usec -= 1000000;
}
+8
View File
@@ -203,3 +203,11 @@ assert('day of week methods') do
assert_false t.friday?
assert_false t.saturday?
end
assert('2000 times 500us make a second') do
t = Time.utc 2015
2000.times do
t += 0.0005
end
t.usec == 0
end