1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
require_relative 'spec_helper'
load_extension("time")
describe "CApiTimeSpecs" do
before :each do
@s = CApiTimeSpecs.new
end
describe "rb_time_new" do
it "creates a Time from the sec and usec" do
usec = CAPI_SIZEOF_LONG == 8 ? 4611686018427387903 : 1413123123
@s.rb_time_new(1232141421, usec).should == Time.at(1232141421, usec)
end
end
describe "TIMET2NUM" do
it "returns an Integer" do
@s.TIMET2NUM.should be_kind_of(Integer)
end
end
describe "rb_time_nano_new" do
it "creates a Time from the sec and usec" do
time = @s.rb_time_nano_new(1232141421, 1413123123)
time.to_i.should == 1232141422
time.nsec.should == 413123123
end
end
describe "rb_time_num_new" do
it "creates a Time in the local zone with only a timestamp" do
with_timezone("Europe/Amsterdam") do
time = @s.rb_time_num_new(1232141421, nil)
time.should be_an_instance_of(Time)
time.to_i.should == 1232141421
platform_is_not :windows do
time.gmt_offset.should == 3600
end
end
end
it "creates a Time with the given offset" do
with_timezone("Europe/Amsterdam") do
time = @s.rb_time_num_new(1232141421, 7200)
time.should be_an_instance_of(Time)
time.to_i.should == 1232141421
time.gmt_offset.should == 7200
end
end
it "creates a Time with a Float timestamp" do
with_timezone("Europe/Amsterdam") do
time = @s.rb_time_num_new(1.5, 7200)
time.should be_an_instance_of(Time)
time.to_i.should == 1
time.nsec.should == 500000000
time.gmt_offset.should == 7200
end
end
it "creates a Time with a Rational timestamp" do
with_timezone("Europe/Amsterdam") do
time = @s.rb_time_num_new(Rational(3, 2), 7200)
time.should be_an_instance_of(Time)
time.to_i.should == 1
time.nsec.should == 500000000
time.gmt_offset.should == 7200
end
end
end
describe "rb_time_interval" do
it "creates a timeval interval for a Fixnum" do
sec, usec = @s.rb_time_interval(1232141421)
sec.should be_kind_of(Integer)
sec.should == 1232141421
usec.should be_kind_of(Integer)
usec.should == 0
end
it "creates a timeval interval for a Float" do
sec, usec = @s.rb_time_interval(1.5)
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval interval for a Rational" do
sec, usec = @s.rb_time_interval(Rational(3, 2))
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "throws an argument error for a negative value" do
-> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError)
-> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError)
-> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError)
end
end
describe "rb_time_interval" do
it "creates a timeval interval for a Fixnum" do
sec, usec = @s.rb_time_interval(1232141421)
sec.should be_kind_of(Integer)
sec.should == 1232141421
usec.should be_kind_of(Integer)
usec.should == 0
end
it "creates a timeval interval for a Float" do
sec, usec = @s.rb_time_interval(1.5)
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval interval for a Rational" do
sec, usec = @s.rb_time_interval(Rational(3, 2))
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "throws an argument error for a negative value" do
-> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError)
-> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError)
-> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError)
end
it "throws an argument error when given a Time instance" do
-> { @s.rb_time_interval(Time.now) }.should raise_error(TypeError)
end
end
describe "rb_time_timeval" do
it "creates a timeval for a Fixnum" do
sec, usec = @s.rb_time_timeval(1232141421)
sec.should be_kind_of(Integer)
sec.should == 1232141421
usec.should be_kind_of(Integer)
usec.should == 0
end
it "creates a timeval for a Float" do
sec, usec = @s.rb_time_timeval(1.5)
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval for a Rational" do
sec, usec = @s.rb_time_timeval(Rational(3, 2))
sec.should be_kind_of(Integer)
sec.should == 1
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval for a negative Fixnum" do
sec, usec = @s.rb_time_timeval(-1232141421)
sec.should be_kind_of(Integer)
sec.should == -1232141421
usec.should be_kind_of(Integer)
usec.should == 0
end
it "creates a timeval for a negative Float" do
sec, usec = @s.rb_time_timeval(-1.5)
sec.should be_kind_of(Integer)
sec.should == -2
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval for a negative Rational" do
sec, usec = @s.rb_time_timeval(Rational(-3, 2))
sec.should be_kind_of(Integer)
sec.should == -2
usec.should be_kind_of(Integer)
usec.should == 500000
end
it "creates a timeval from a Time object" do
t = Time.now
sec, usec = @s.rb_time_timeval(t)
sec.should == t.to_i
usec.should == t.nsec.div(1000)
end
end
describe "rb_time_timespec" do
it "creates a timespec for a Fixnum" do
sec, nsec = @s.rb_time_timespec(1232141421)
sec.should be_kind_of(Integer)
sec.should == 1232141421
nsec.should be_kind_of(Integer)
nsec.should == 0
end
it "creates a timespec for a Float" do
sec, nsec = @s.rb_time_timespec(1.5)
sec.should be_kind_of(Integer)
sec.should == 1
nsec.should be_kind_of(Integer)
nsec.should == 500000000
end
it "creates a timespec for a Rational" do
sec, nsec = @s.rb_time_timespec(Rational(3, 2))
sec.should be_kind_of(Integer)
sec.should == 1
nsec.should be_kind_of(Integer)
nsec.should == 500000000
end
it "creates a timespec for a negative Fixnum" do
sec, nsec = @s.rb_time_timespec(-1232141421)
sec.should be_kind_of(Integer)
sec.should == -1232141421
nsec.should be_kind_of(Integer)
nsec.should == 0
end
it "creates a timespec for a negative Float" do
sec, nsec = @s.rb_time_timespec(-1.5)
sec.should be_kind_of(Integer)
sec.should == -2
nsec.should be_kind_of(Integer)
nsec.should == 500000000
end
it "creates a timespec for a negative Rational" do
sec, nsec = @s.rb_time_timespec(Rational(-3, 2))
sec.should be_kind_of(Integer)
sec.should == -2
nsec.should be_kind_of(Integer)
nsec.should == 500000000
end
it "creates a timespec from a Time object" do
t = Time.now
sec, nsec = @s.rb_time_timespec(t)
sec.should == t.to_i
nsec.should == t.nsec
end
end
describe "rb_time_timespec_new" do
it "returns a time object with the given timespec and UTC offset" do
@s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400)
end
describe "when offset given is within range of -86400 and 86400 (exclusive)" do
it "sets time's is_gmt to false" do
@s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false
end
it "sets time's offset to the offset given" do
@s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399
end
end
it "returns time object in UTC if offset given equals INT_MAX - 1" do
@s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true
end
it "returns time object in localtime if offset given equals INT_MAX" do
@s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime
t = Time.now
@s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff
end
it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do
-> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError)
-> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError)
end
it "doesn't call Time.at directly" do
Time.should_not_receive(:at)
@s.rb_time_timespec_new(1447087832, 476451125, 32400).should be_kind_of(Time)
end
end
describe "rb_timespec_now" do
it "fills a struct timespec with the current time" do
now = Time.now
time = @s.rb_time_from_timespec(now.utc_offset)
time.should be_an_instance_of(Time)
(time - now).should be_close(0, TIME_TOLERANCE)
end
end
end
|