Skip to content

Commit ed92645

Browse files
committed
Add spec for rb_timespec_now
* See #175.
1 parent 4cb0765 commit ed92645

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

optional/capi/ext/rubyspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@
602602
#define HAVE_RB_TIME_TIMEVAL 1
603603
#define HAVE_RB_TIME_TIMESPEC 1
604604
#ifdef RUBY_VERSION_IS_2_3
605+
#define HAVE_RB_TIMESPEC_NOW 1
605606
#define HAVE_RB_TIME_TIMESPEC_NEW 1
606607
#endif
607608

optional/capi/ext/time_spec.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ static VALUE time_spec_rb_time_timespec_new(VALUE self, VALUE sec, VALUE nsec, V
6565
}
6666
#endif
6767

68+
#ifdef HAVE_RB_TIMESPEC_NOW
69+
static VALUE time_spec_rb_time_from_timspec_now(VALUE self, VALUE offset) {
70+
struct timespec ts;
71+
rb_timespec_now(&ts);
72+
73+
return rb_time_timespec_new(&ts, NUM2INT(offset));
74+
}
75+
#endif
76+
6877
#ifdef HAVE_TIMET2NUM
6978
static VALUE time_spec_TIMET2NUM(VALUE self) {
7079
time_t t = 10;
@@ -107,6 +116,10 @@ void Init_time_spec(void) {
107116
#ifdef HAVE_RB_TIME_TIMESPEC_NEW
108117
rb_define_method(cls, "rb_time_timespec_new", time_spec_rb_time_timespec_new, 3);
109118
#endif
119+
120+
#ifdef HAVE_RB_TIMESPEC_NOW
121+
rb_define_method(cls, "rb_time_from_timespec", time_spec_rb_time_from_timspec_now, 1);
122+
#endif
110123
}
111124

112125
#ifdef __cplusplus

optional/capi/time_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,14 @@
283283
lambda { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError)
284284
end
285285
end
286+
287+
describe "rb_timespec_now" do
288+
it "fills a struct timespec with the current time" do
289+
now = Time.now
290+
time = @s.rb_time_from_timespec(now.utc_offset)
291+
time.should be_an_instance_of(Time)
292+
(time - now).should be_close(0, 10)
293+
end
294+
end
286295
end
287296
end

0 commit comments

Comments
 (0)