forked from rubinius/rubinius
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Time C++ class and primitives.
- Loading branch information
Brian Ford
committed
Aug 22, 2008
1 parent
1686604
commit 44d61f8
Showing
12 changed files
with
240 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
class Time | ||
def self.gettimeofday | ||
Ruby.primitive :gettimeofday | ||
raise PrimitiveFailure, "primitive failed" | ||
def self.allocate | ||
Ruby.primitive :time_allocate | ||
raise PrimitiveFailure, "Time.allocate primitive failed" | ||
end | ||
|
||
|
||
def gettimeofday | ||
Ruby.primitive :time_gettimeofday | ||
raise PrimitiveFailure, "Time#gettimeofday primitive failed" | ||
end | ||
|
||
def time_switch(sec, to_gmt) | ||
Ruby.primitive :time_switch | ||
raise PrimitiveFailure, "primitive failed" | ||
raise PrimitiveFailure, "Time#time_switch primitive failed" | ||
end | ||
|
||
def time_mktime(sec, min, hour, mday, mon, year, usec, isdst, from_gmt) | ||
Ruby.primitive :mktime | ||
Ruby.primitive :time_mktime | ||
raise ArgumentError, "time out of range" | ||
end | ||
def __strftime(tm, format) | ||
Ruby.primitive :strftime | ||
raise PrimitiveFailure, "primitive failed" | ||
|
||
def __strftime__(tm, format) | ||
Ruby.primitive :time_strftime | ||
raise PrimitiveFailure, "Time#__strftime__ primitive failed" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "builtin/array.hpp" | ||
#include "builtin/integer.hpp" | ||
#include "builtin/time.hpp" | ||
#include "objectmemory.hpp" | ||
|
||
#include <sys/time.h> | ||
#include <time.h> | ||
|
||
namespace rubinius { | ||
Time* Time::create(STATE) { | ||
Time* tm = (Time*)state->om->new_object(G(time_class), Time::fields); | ||
|
||
tm->gettimeofday(state); | ||
tm->time_switch(state, Qfalse); | ||
|
||
return tm; | ||
} | ||
|
||
Time* Time::gettimeofday(STATE) { | ||
struct timeval tv; | ||
|
||
/* don't fill in the 2nd argument here. getting the timezone here | ||
* this way is not portable and broken anyway. | ||
*/ | ||
::gettimeofday(&tv, NULL); | ||
|
||
/* update Time::TIMEVAL_FIELDS when changing order of fields */ | ||
Array* ary = Array::create(state, 2); | ||
ary->set(state, 0, Integer::from(state, tv.tv_sec)); | ||
ary->set(state, 1, Integer::from(state, tv.tv_usec)); | ||
|
||
SET(this, timeval, ary); | ||
|
||
return this; | ||
} | ||
|
||
Time* Time::time_switch(STATE, OBJECT gmt) { | ||
time_t seconds = ((Integer*)this->timeval->get(state, 0))->to_native(); | ||
struct tm *tm; | ||
|
||
if(gmt == Qtrue) { | ||
tm = gmtime(&seconds); | ||
} else { | ||
tm = localtime(&seconds); | ||
} | ||
|
||
/* update Time::TM_FIELDS when changing order of fields */ | ||
Array* ary = Array::create(state, 11); | ||
ary->set(state, 0, Integer::from(state, tm->tm_sec)); | ||
ary->set(state, 1, Integer::from(state, tm->tm_min)); | ||
ary->set(state, 2, Integer::from(state, tm->tm_hour)); | ||
ary->set(state, 3, Integer::from(state, tm->tm_mday)); | ||
ary->set(state, 4, Integer::from(state, tm->tm_mon)); | ||
ary->set(state, 5, Integer::from(state, tm->tm_year)); | ||
ary->set(state, 6, Integer::from(state, tm->tm_wday)); | ||
ary->set(state, 7, Integer::from(state, tm->tm_yday)); | ||
ary->set(state, 8, Integer::from(state, tm->tm_isdst)); | ||
|
||
#ifdef HAVE_STRUCT_TM_TM_GMTOFF | ||
ary->set(state, 9, Integer::from(state, tm->tm_gmtoff)); | ||
#else | ||
ary->set(state, 9, Qnil); | ||
#endif | ||
|
||
#ifdef HAVE_STRUCT_TM_TM_ZONE | ||
ary->set(state, 10, String::create(state, tm->tm_zone)); | ||
#else | ||
ary->set(state, 10, Qnil); | ||
#endif | ||
|
||
SET(this, tm, ary); | ||
SET(this, is_gmt, gmt); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef RBX_BUILTIN_TIME_HPP | ||
#define RBX_BUILTIN_TIME_HPP | ||
|
||
#include "builtin/object.hpp" | ||
#include "type_info.hpp" | ||
|
||
namespace rubinius { | ||
class Array; | ||
|
||
class Time : public Object { | ||
public: | ||
const static size_t fields = 3; | ||
const static object_type type = TimeType; | ||
|
||
Array* timeval; // slot | ||
Array* tm; // slot | ||
OBJECT is_gmt; // slot | ||
|
||
// Ruby.primitive :time_allocate | ||
static Time* create(STATE); | ||
|
||
// Ruby.primitive :time_gettimeofday | ||
Time* gettimeofday(STATE); | ||
|
||
// Ruby.primitive :time_witch | ||
Time* time_switch(STATE, OBJECT gmt); | ||
|
||
class Info : public TypeInfo { | ||
public: | ||
BASIC_TYPEINFO(TypeInfo) | ||
}; | ||
}; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.