Skip to content
Permalink
Browse files

Merge pull request #817 from sideshowcoder/time-comparison-and-succ

Time#at Time#succ Time#<=>
  • Loading branch information
alex committed Aug 12, 2013
2 parents e6f6cf1 + 78c7b74 commit 2e589633d6078cfce9feccbb19f49dbf382981a5
@@ -11,4 +11,26 @@ def tv_usec
def tv_sec
self.to_f.floor
end

def succ
Time.at(self.to_i + 1)
end

def <=>(other)
if other.kind_of?(Time)
self.to_f <=> other.to_f
else
compare = (other <=> self)
if compare.nil?
nil
elsif compare > 0
-1
elsif compare < 0
1
else
0
end
end
end

end
@@ -5,11 +5,9 @@ fails:Time.at passed Time returns a UTC time if the argument is UTC
fails:Time.at passed Time returns a non-UTC time if the argument is non-UTC
fails:Time.at passed non-Time, non-Numeric with an argument that responds to #to_int coerces using #to_int
fails:Time.at passed non-Time, non-Numeric with an argument that responds to #to_r coerces using #to_r
fails:Time.at passed [Integer, Numeric] returns a Time object representing the given number of seconds and Integer microseconds since 1970-01-01 00:00:00 UTC
fails:Time.at passed [Integer, Numeric] returns a Time object representing the given number of seconds and Float microseconds since 1970-01-01 00:00:00 UTC
fails:Time.at with a second argument that responds to #to_int coerces using #to_int
fails:Time.at with a second argument that responds to #to_r coerces using #to_r
fails:Time.at passed [Integer, nil] raises a TypeError
fails:Time.at passed [Integer, String] raises a TypeError
fails:Time.at passed [Time, Integer] returns a Time object equal to the specified time plus the number of microseconds
fails:Time.at passed [Time, Integer] raises a TypeError
@@ -1,10 +1,3 @@
fails:Time#<=> returns 1 if the first argument is a point in time after the second argument
fails:Time#<=> returns 0 if time is the same as other
fails:Time#<=> returns -1 if the first argument is a point in time before the second argument
fails:Time#<=> returns 1 if the first argument is a fraction of a microsecond after the second argument
fails:Time#<=> returns 0 if time is the same as other, including fractional microseconds
fails:Time#<=> returns -1 if the first argument is a fraction of a microsecond before the second argument
fails:Time#<=> given a non-Time argument returns nil if argument <=> self returns nil
fails:Time#<=> given a non-Time argument returns -1 if argument <=> self is greater than 0
fails:Time#<=> given a non-Time argument returns 1 if argument <=> self is not greater than 0 and is less than 0
fails:Time#<=> given a non-Time argument returns 0 if argument <=> self is neither greater than 0 nor less than 0

This file was deleted.

@@ -3,10 +3,12 @@
from topaz.module import ClassDef
from topaz.coerce import Coerce
from topaz.objects.objectobject import W_Object
from topaz.modules.comparable import Comparable


class W_TimeObject(W_Object):
classdef = ClassDef("Time", W_Object.classdef)
classdef.include_module(Comparable)

def __init__(self, space, klass):
W_Object.__init__(self, space, klass)
@@ -21,13 +23,17 @@ def method_now(self, space):
return space.send(self, "new")

@classdef.singleton_method("at")
def method_at(self, space, w_time):
def method_at(self, space, w_time, w_microtime=None):
if not (w_time.is_kind_of(space, space.w_numeric) or
w_time.is_kind_of(space, space.getclassfor(W_TimeObject))):
raise space.error(space.w_TypeError)
if w_microtime is not None:
microtime = Coerce.float(space, w_microtime) * 0.000001
else:
microtime = 0.0
timestamp = Coerce.float(space, w_time)
w_time = space.send(self, "new")
w_time._set_epoch_seconds(timestamp)
w_time._set_epoch_seconds(timestamp + microtime)
return w_time

@classdef.method("initialize")
@@ -38,6 +44,10 @@ def method_initialize(self, space):
def method_to_f(self, space):
return space.newfloat(self.epoch_seconds)

@classdef.method("to_i")
def method_to_i(self, space):
return space.newint(int(self.epoch_seconds))

@classdef.method("-")
def method_sub(self, space, w_other):
assert isinstance(w_other, W_TimeObject)

0 comments on commit 2e58963

Please sign in to comment.
You can’t perform that action at this time.