Skip to content
Permalink
Browse files

Merge pull request #793 from jstepien/time-at

Add Time.at
  • Loading branch information
alex committed Jul 12, 2013
2 parents c5120b1 + c38df85 commit 90b24be46e3bf9348f873593133e6e5c3ed06bdc
@@ -28,4 +28,5 @@
load_bootstrap.call("string.rb")
load_bootstrap.call("symbol.rb")
load_bootstrap.call("thread.rb")
load_bootstrap.call("time.rb")
load_bootstrap.call("top_self.rb")
@@ -0,0 +1,14 @@
class Time
def usec
float = self.to_f
(float - float.floor) * 1e6
end

def tv_usec
self.usec.floor
end

def tv_sec
self.to_f.floor
end
end
@@ -1,14 +1,8 @@
fails:Time.at passed Numeric returns a Time object representing the given number of Integer seconds since 1970-01-01 00:00:00 UTC
fails:Time.at passed Numeric returns a Time object representing the given number of Float seconds since 1970-01-01 00:00:00 UTC
fails:Time.at passed Numeric returns a non-UTC Time
fails:Time.at passed Numeric returns a subclass instance on a Time subclass
fails:Time.at passed Time creates a new time object with the value given by time
fails:Time.at passed Time creates a dup time object with the value given by time
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 Time returns a subclass instance
fails:Time.at passed non-Time, non-Numeric raises a TypeError with a String argument
fails:Time.at passed non-Time, non-Numeric raises a TypeError with a nil argument
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
@@ -1,5 +1,4 @@
fails:Time#eql? returns true if self and other have the same whole number of seconds
fails:Time#eql? returns false if self and other have differing whole numbers of seconds
fails:Time#eql? returns true if self and other have the same number of microseconds
fails:Time#eql? returns false if self and other have differing numbers of microseconds
fails:Time#eql? returns false if self and other have differing fractional microseconds

This file was deleted.

@@ -1,5 +1,3 @@
fails:Time#usec returns 0 for a Time constructed with a whole number of seconds
fails:Time#usec returns the microseconds part of a Time constructed with a Float number of seconds
fails:Time#usec returns the microseconds part of a Time constructed with an Integer number of microseconds
fails:Time#usec returns the microseconds part of a Time constructed with an Float number of microseconds > 1
fails:Time#usec returns 0 for a Time constructed with an Float number of microseconds < 1
@@ -1,6 +1,7 @@
import time

from topaz.module import ClassDef
from topaz.coerce import Coerce
from topaz.objects.objectobject import W_Object


@@ -19,6 +20,16 @@ def method_allocate(self, space):
def method_now(self, space):
return space.send(self, "new")

@classdef.singleton_method("at")
def method_at(self, space, w_time):
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)
timestamp = Coerce.float(space, w_time)
time = space.send(self, "new")
time._set_epoch_seconds(timestamp)
return time

@classdef.method("initialize")
def method_initialize(self, space):
self.epoch_seconds = time.time()
@@ -31,3 +42,6 @@ def method_to_f(self, space):
def method_sub(self, space, w_other):
assert isinstance(w_other, W_TimeObject)
return space.newfloat(self.epoch_seconds - w_other.epoch_seconds)

def _set_epoch_seconds(self, timestamp):
self.epoch_seconds = timestamp

0 comments on commit 90b24be

Please sign in to comment.