summaryrefslogtreecommitdiff
path: root/time.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-04-06 08:02:56 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-04-06 08:02:56 +0000
commitefa746c6bd61d6f327c6c1f0742284d768704d47 (patch)
treedc8fcd3678b94dce418e0a170b35d8073a21ccd3 /time.c
parent5923a2c0e770515a9c7144c50e09a32350695b00 (diff)
* configure.in: check the size of time_t.
* time.c (time_add): new function. (time_plus): use time_add. (time_minus): use time_add. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6110 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'time.c')
-rw-r--r--time.c111
1 files changed, 54 insertions, 57 deletions
diff --git a/time.c b/time.c
index b6b16a1..a83ce74 100644
--- a/time.c
+++ b/time.c
@@ -1254,6 +1254,57 @@ time_to_s(time)
return rb_str_new(buf, len);
}
+#if SIZEOF_TIME_T == SIZEOF_LONG
+typedef unsigned long unsigned_time_t;
+#elif SIZEOF_TIME_T == SIZEOF_LONG_LONG
+typedef unsigned long long unsigned_time_t;
+#else
+# error cannot find integer type which size is same as time_t.
+#endif
+
+static VALUE
+time_add(tobj, offset, sign)
+ struct time_object *tobj;
+ VALUE offset;
+ int sign;
+{
+ double v = NUM2DBL(offset);
+ double f, d;
+ unsigned_time_t sec_off;
+ time_t usec_off, sec, usec;
+ VALUE result;
+
+ if (v < 0) {
+ v = -v;
+ sign = -sign;
+ }
+ d = modf(v, &f);
+ sec_off = (unsigned_time_t)f;
+ if (f != (double)sec_off)
+ rb_raise(rb_eRangeError, "time %s %f out of Time range",
+ sign < 0 ? "-" : "+", v);
+ usec_off = (time_t)(d*1e6);
+
+ if (sign < 0) {
+ sec = tobj->tv.tv_sec - sec_off;
+ usec = tobj->tv.tv_usec - usec_off;
+ if (sec > tobj->tv.tv_sec)
+ rb_raise(rb_eRangeError, "time - %f out of Time range", v);
+ }
+ else {
+ sec = tobj->tv.tv_sec + sec_off;
+ usec = tobj->tv.tv_usec + usec_off;
+ if (sec < tobj->tv.tv_sec)
+ rb_raise(rb_eRangeError, "time + %f out of Time range", v);
+ }
+ result = rb_time_new(sec, usec);
+ if (tobj->gmt) {
+ GetTimeval(result, tobj);
+ tobj->gmt = 1;
+ }
+ return result;
+}
+
/*
* call-seq:
* time + numeric => time
@@ -1270,40 +1321,12 @@ time_plus(time1, time2)
VALUE time1, time2;
{
struct time_object *tobj;
- time_t sec, usec;
- double f, d, v;
-
GetTimeval(time1, tobj);
if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
rb_raise(rb_eTypeError, "time + time?");
}
- v = NUM2DBL(time2);
- d = modf(v, &f);
- sec = (time_t)f;
- if (f != (double)sec) {
- rb_raise(rb_eRangeError, "time + %f out of Time range", v);
- }
-#ifndef NEGATIVE_TIME_T
- if (f < 0 && -f >= tobj->tv.tv_sec) {
- rb_raise(rb_eArgError, "time must be positive");
- }
-#endif
- usec = tobj->tv.tv_usec + (time_t)(d*1e6);
- sec = tobj->tv.tv_sec + (time_t)f;
-
-#ifdef NEGATIVE_TIME_T
- if ((tobj->tv.tv_sec >= 0 && f >= 0 && sec < 0) ||
- (tobj->tv.tv_sec <= 0 && f <= 0 && sec > 0)) {
- rb_raise(rb_eRangeError, "time + %f out of Time range", v);
- }
-#endif
- time2 = rb_time_new(sec, usec);
- if (tobj->gmt) {
- GetTimeval(time2, tobj);
- tobj->gmt = 1;
- }
- return time2;
+ return time_add(tobj, time2, 1);
}
/*
@@ -1326,12 +1349,11 @@ time_minus(time1, time2)
VALUE time1, time2;
{
struct time_object *tobj;
- time_t sec, usec;
- double f, d, v;
GetTimeval(time1, tobj);
if (TYPE(time2) == T_DATA && RDATA(time2)->dfree == time_free) {
struct time_object *tobj2;
+ double f;
GetTimeval(time2, tobj2);
f = (double)tobj->tv.tv_sec - (double)tobj2->tv.tv_sec;
@@ -1340,32 +1362,7 @@ time_minus(time1, time2)
return rb_float_new(f);
}
- v = NUM2DBL(time2);
- d = modf(v, &f);
- sec = (time_t)f;
- if (f != (double)sec) {
- rb_raise(rb_eRangeError, "time - %f out of Time range", v);
- }
-#ifndef NEGATIVE_TIME_T
- if (f > 0 && f >= tobj->tv.tv_sec) {
- rb_raise(rb_eArgError, "time must be positive");
- }
-#endif
- usec = tobj->tv.tv_usec - (time_t)(d*1e6);
- sec = tobj->tv.tv_sec - (time_t)f;
-#ifdef NEGATIVE_TIME_T
- if ((tobj->tv.tv_sec <= 0 && f >= 0 && sec > 0) ||
- (tobj->tv.tv_sec >= 0 && f <= 0 && sec < 0)) {
- rb_raise(rb_eRangeError, "time - %f out of Time range", v);
- }
-#endif
-
- time2 = rb_time_new(sec, usec);
- if (tobj->gmt) {
- GetTimeval(time2, tobj);
- tobj->gmt = 1;
- }
- return time2;
+ return time_add(tobj, time2, -1);
}
/*