Skip to content

Commit e976989

Browse files
ahmadsheriferegon
authored andcommitted
Add specs for rb_time_timespec_new
1 parent 1b4d6a2 commit e976989

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

optional/capi/ext/rubyspec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@
596596
#define HAVE_RB_TIME_INTERVAL 1
597597
#define HAVE_RB_TIME_TIMEVAL 1
598598
#define HAVE_RB_TIME_TIMESPEC 1
599+
#define HAVE_RB_TIME_TIMESPEC_NEW 1
599600

600601
/* Util */
601602
#define HAVE_RB_SCAN_ARGS 1

optional/capi/ext/time_spec.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ static VALUE time_spec_rb_time_timespec(VALUE self, VALUE time) {
5555
}
5656
#endif
5757

58+
#ifdef HAVE_RB_TIME_TIMESPEC_NEW
59+
static VALUE time_spec_rb_time_timespec_new(VALUE self, VALUE sec, VALUE nsec, VALUE offset) {
60+
struct timespec ts;
61+
ts.tv_sec = NUM2TIMET(sec);
62+
ts.tv_nsec = NUM2LONG(nsec);
63+
64+
return rb_time_timespec_new(&ts, NUM2INT(offset));
65+
}
66+
#endif
67+
5868
#ifdef HAVE_TIMET2NUM
5969
static VALUE time_spec_TIMET2NUM(VALUE self) {
6070
time_t t = 10;
@@ -93,6 +103,10 @@ void Init_time_spec() {
93103
#ifdef HAVE_RB_TIME_TIMESPEC
94104
rb_define_method(cls, "rb_time_timespec", time_spec_rb_time_timespec, 1);
95105
#endif
106+
107+
#ifdef HAVE_RB_TIME_TIMESPEC_NEW
108+
rb_define_method(cls, "rb_time_timespec_new", time_spec_rb_time_timespec_new, 3);
109+
#endif
96110
}
97111

98112
#ifdef __cplusplus

optional/capi/time_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,34 @@
251251
nsec.should == t.nsec
252252
end
253253
end
254+
255+
describe "rb_time_timespec_new" do
256+
it "returns a time object with the given timespec and UTC offset" do
257+
@s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400)
258+
end
259+
260+
describe "when offset given is within range of -86400 and 86400 (exclusive)" do
261+
it "sets time's is_gmt to false" do
262+
@s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false
263+
end
264+
265+
it "sets time's offset to the offset given" do
266+
@s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399
267+
end
268+
end
269+
270+
it "returns time object in UTC if offset given equals INT_MAX - 1" do
271+
@s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true
272+
end
273+
274+
it "returns time object in localtime if offset given equals INT_MAX" do
275+
@s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime
276+
@s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).gmtoff.should == Time.now.gmtoff
277+
end
278+
279+
it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do
280+
lambda { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError)
281+
lambda { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError)
282+
end
283+
end
254284
end

0 commit comments

Comments
 (0)