mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #2835 from kext/time-rounding-precision
Time rounding precision
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user