diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-06-16 16:36:34 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-06-16 16:36:34 +0900 |
| commit | 6dcc3e7383907efc07e6d7e1abc968deecdb914d (patch) | |
| tree | e14dd549f9f7a4f9cccbebb07016ea6c3989f020 | |
| parent | 97a18ff4d9db48a72b3e63f2d6fd7c7958e98c97 (diff) | |
| parent | 47c61cf5027ed46a51ef0a261c3192700700a350 (diff) | |
| download | mruby-6dcc3e7383907efc07e6d7e1abc968deecdb914d.tar.gz mruby-6dcc3e7383907efc07e6d7e1abc968deecdb914d.zip | |
Merge pull request #2835 from kext/time-rounding-precision
Time rounding precision
| -rw-r--r-- | mrbgems/mruby-time/src/time.c | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-time/test/time.rb | 8 |
2 files changed, 11 insertions, 2 deletions
diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index b377f3e33..da3451d22 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -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; } diff --git a/mrbgems/mruby-time/test/time.rb b/mrbgems/mruby-time/test/time.rb index ba9b48fab..759e2881d 100644 --- a/mrbgems/mruby-time/test/time.rb +++ b/mrbgems/mruby-time/test/time.rb @@ -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 |
