Commit 6dcc3e73 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

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

Time rounding precision
parents 97a18ff4 47c61cf5
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
** See Copyright Notice in mruby.h ** See Copyright Notice in mruby.h
*/ */
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include "mruby.h" #include "mruby.h"
...@@ -208,12 +209,12 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) ...@@ -208,12 +209,12 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
out_of_range: out_of_range:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "%S out of Time range", mrb_float_value(mrb, sec)); 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) { while (tm->usec < 0) {
tm->sec--; tm->sec--;
tm->usec += 1000000; tm->usec += 1000000;
} }
while (tm->usec > 1000000) { while (tm->usec >= 1000000) {
tm->sec++; tm->sec++;
tm->usec -= 1000000; tm->usec -= 1000000;
} }
......
...@@ -203,3 +203,11 @@ assert('day of week methods') do ...@@ -203,3 +203,11 @@ assert('day of week methods') do
assert_false t.friday? assert_false t.friday?
assert_false t.saturday? assert_false t.saturday?
end end
assert('2000 times 500us make a second') do
t = Time.utc 2015
2000.times do
t += 0.0005
end
t.usec == 0
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment