1 #ifndef VALUE2TIMESPEC_H
2 #define VALUE2TIMESPEC_H
9 # define NUM2TIMET(n) NUM2LONG(n)
13 # define RFLOAT_VALUE(v) (RFLOAT(v)->value)
16 static struct timespec
*value2timespec(struct timespec
*ts
, VALUE num
)
21 ts
->tv_sec
= NUM2TIMET(num
);
25 double orig
= RFLOAT_VALUE(num
);
30 ts
->tv_nsec
= (long)(d
* 1e9
+ 0.5);
32 ts
->tv_nsec
= (long)(-d
* 1e9
+ 0.5);
33 if (ts
->tv_nsec
> 0) {
34 ts
->tv_nsec
= (long)1e9
- ts
->tv_nsec
;
38 ts
->tv_sec
= (time_t)f
;
40 rb_raise(rb_eRangeError
, "%f out of range", orig
);
44 VALUE tmp
= rb_inspect(num
);
45 const char *str
= StringValueCStr(tmp
);
46 rb_raise(rb_eTypeError
, "can't convert %s into timespec", str
);
48 rb_bug("rb_raise() failed, timespec failed");
53 # define TIMET2NUM(n) LONG2NUM(n)
56 static inline VALUE
timespec2num(struct timespec
*ts
)
59 return TIMET2NUM(ts
->tv_sec
);
61 return rb_float_new(ts
->tv_sec
+ ((double)ts
->tv_nsec
/ 1e9
));
64 #endif /* VALUE2TIMESPEC_H */