End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Time#ceil
time.ceil([ndigits]) → new_time
time.c
View on GitHub ↗
static VALUE
time_ceil(int argc, VALUE *argv, VALUE time)
{
VALUE ndigits, v, den;
struct time_object *tobj;
if (!rb_check_arity(argc, 0, 1) || NIL_P(ndigits = argv[0]))
den = INT2FIX(1);
else
den = ndigits_denominator(ndigits);
GetTimeval(time, tobj);
v = w2v(rb_time_unmagnify(tobj->timew));
v = modv(v, den);
if (!rb_equal(v, INT2FIX(0))) {
v = subv(den, v);
}
return time_add(tobj, time, v, 1);
}
Ceils subsecond to a given precision in decimal digits (0 digits by default). It returns a new Time object. ndigits should be zero or a positive integer.
Example
t = Time.utc(2010,3,30, 5,43,25.0123456789r) t #=> 2010-03-30 05:43:25 123456789/10000000000 UTC t.ceil #=> 2010-03-30 05:43:26 UTC t.ceil(0) #=> 2010-03-30 05:43:26 UTC t.ceil(1) #=> 2010-03-30 05:43:25.1 UTC t.ceil(2) #=> 2010-03-30 05:43:25.02 UTC t.ceil(3) #=> 2010-03-30 05:43:25.013 UTC t.ceil(4) #=> 2010-03-30 05:43:25.0124 UTC t = Time.utc(1999,12,31, 23,59,59) (t + 0.4).ceil #=> 2000-01-01 00:00:00 UTC (t + 0.9).ceil #=> 2000-01-01 00:00:00 UTC (t + 1.4).ceil #=> 2000-01-01 00:00:01 UTC (t + 1.9).ceil #=> 2000-01-01 00:00:01 UTC t = Time.utc(1999,12,31, 23,59,59) (t + 0.123456789).ceil(4) #=> 1999-12-31 23:59:59.1235 UTC