instance method
Time#<=>
(::Time other) -> ::Integer
(untyped other) -> ::Integer?
self <=> other_time → -1, 0, +1, or nil
time.c
View on GitHub ↗
static VALUE
time_cmp(VALUE time1, VALUE time2)
{
struct time_object *tobj1, *tobj2;
int n;
GetTimeval(time1, tobj1);
if (IsTimeval(time2)) {
GetTimeval(time2, tobj2);
n = wcmp(tobj1->timew, tobj2->timew);
}
else {
return rb_invcmp(time1, time2);
}
if (n == 0) return INT2FIX(0);
if (n > 0) return INT2FIX(1);
return INT2FIX(-1);
}
Compares self with other_time; returns:
-
-1, ifselfis less thanother_time. -
0, ifselfis equal toother_time. -
1, ifselfis greater thenother_time. -
nil, ifselfandother_timeare incomparable.
Examples:
Example
t = Time.now # => 2007-11-19 08:12:12 -0600 t2 = t + 2592000 # => 2007-12-19 08:12:12 -0600 t <=> t2 # => -1 t2 <=> t # => 1 t = Time.now # => 2007-11-19 08:13:38 -0600 t2 = t + 0.1 # => 2007-11-19 08:13:38 -0600 t.nsec # => 98222999 t2.nsec # => 198222999 t <=> t2 # => -1 t2 <=> t # => 1 t <=> t # => 0