summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authorMichael Leibowitz <michael.leibowitz@intel.com>2013-08-15 15:02:54 -0700
committerMichael Leibowitz <michael.leibowitz@intel.com>2013-08-15 15:02:54 -0700
commit6b0e2a11aeccf2ebc707c22548a0c15c8c2bae06 (patch)
treed9d59f4b502927634d32832e963db76629ff5a2e /time.c
parent3b35de2a90e26b99e2a6d4f61dc56d6ce7ded748 (diff)
downloadruby-6b0e2a11aeccf2ebc707c22548a0c15c8c2bae06.tar.gz
ruby-6b0e2a11aeccf2ebc707c22548a0c15c8c2bae06.tar.bz2
ruby-6b0e2a11aeccf2ebc707c22548a0c15c8c2bae06.zip
Imported Upstream version 1.9.3.p448upstream/1.9.3.p448upstream
Diffstat (limited to 'time.c')
-rw-r--r--time.c52
1 files changed, 43 insertions, 9 deletions
diff --git a/time.c b/time.c
index a47ce23..a8e209d 100644
--- a/time.c
+++ b/time.c
@@ -2,7 +2,7 @@
time.c -
- $Author: ayumin $
+ $Author: usa $
created at: Tue Dec 28 14:31:59 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
@@ -27,6 +27,10 @@
#include <strings.h>
#endif
+#if defined(HAVE_SYS_TIME_H)
+#include <sys/time.h>
+#endif
+
#include "timev.h"
static ID id_divmod, id_mul, id_submicro, id_nano_num, id_nano_den, id_offset;
@@ -679,7 +683,9 @@ num_exact(VALUE v)
default:
if ((tmp = rb_check_funcall(v, rb_intern("to_r"), 0, NULL)) != Qundef) {
- if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
+ /* test to_int method availability to reject non-Numeric
+ * objects such as String, Time, etc which have to_r method. */
+ if (!rb_respond_to(v, rb_intern("to_int"))) goto typeerror;
v = tmp;
break;
}
@@ -1803,10 +1809,11 @@ struct time_object {
int tm_got;
};
-#define GetTimeval(obj, tobj) \
- TypedData_Get_Struct((obj), struct time_object, &time_data_type, (tobj))
+#define GetTimeval(obj, tobj) ((tobj) = get_timeval(obj))
+#define GetNewTimeval(obj, tobj) ((tobj) = get_new_timeval(obj))
#define IsTimeval(obj) rb_typeddata_is_kind_of((obj), &time_data_type)
+#define TIME_INIT_P(tobj) ((tobj)->gmt != -1)
#define TIME_UTC_P(tobj) ((tobj)->gmt == 1)
#define TIME_SET_UTC(tobj) ((tobj)->gmt = 1)
@@ -1869,12 +1876,35 @@ time_s_alloc(VALUE klass)
struct time_object *tobj;
obj = TypedData_Make_Struct(klass, struct time_object, &time_data_type, tobj);
+ tobj->gmt = -1;
tobj->tm_got=0;
tobj->timew = WINT2FIXWV(0);
return obj;
}
+static struct time_object *
+get_timeval(VALUE obj)
+{
+ struct time_object *tobj;
+ TypedData_Get_Struct(obj, struct time_object, &time_data_type, tobj);
+ if (!TIME_INIT_P(tobj)) {
+ rb_raise(rb_eTypeError, "uninitialized %"PRIiVALUE, CLASS_OF(obj));
+ }
+ return tobj;
+}
+
+static struct time_object *
+get_new_timeval(VALUE obj)
+{
+ struct time_object *tobj;
+ TypedData_Get_Struct(obj, struct time_object, &time_data_type, tobj);
+ if (TIME_INIT_P(tobj)) {
+ rb_raise(rb_eTypeError, "already initialized %"PRIiVALUE, CLASS_OF(obj));
+ }
+ return tobj;
+}
+
static void
time_modify(VALUE time)
{
@@ -1941,7 +1971,8 @@ time_init_0(VALUE time)
struct timespec ts;
time_modify(time);
- GetTimeval(time, tobj);
+ GetNewTimeval(time, tobj);
+ tobj->gmt = 0;
tobj->tm_got=0;
tobj->timew = WINT2FIXWV(0);
#ifdef HAVE_CLOCK_GETTIME
@@ -2182,7 +2213,8 @@ time_init_1(int argc, VALUE *argv, VALUE time)
validate_vtm(&vtm);
time_modify(time);
- GetTimeval(time, tobj);
+ GetNewTimeval(time, tobj);
+ tobj->gmt = 0;
tobj->tm_got=0;
tobj->timew = WINT2FIXWV(0);
@@ -2298,7 +2330,8 @@ time_new_timew(VALUE klass, wideval_t timew)
VALUE time = time_s_alloc(klass);
struct time_object *tobj;
- GetTimeval(time, tobj);
+ tobj = DATA_PTR(time); /* skip type check */
+ tobj->gmt = 0;
tobj->timew = timew;
return time;
@@ -3424,7 +3457,7 @@ time_init_copy(VALUE copy, VALUE time)
if (copy == time) return copy;
time_modify(copy);
GetTimeval(time, tobj);
- GetTimeval(copy, tcopy);
+ GetNewTimeval(copy, tcopy);
MEMCPY(tcopy, tobj, struct time_object, 1);
return copy;
@@ -4801,7 +4834,8 @@ end_submicro: ;
timew = timegmw(&vtm);
}
- GetTimeval(time, tobj);
+ GetNewTimeval(time, tobj);
+ tobj->gmt = 0;
tobj->tm_got = 0;
tobj->timew = timew;
if (gmt) {