Skip to content

Commit a3ca37e

Browse files
petoemasterite
authored andcommittedJan 10, 2018
Fix Time::Span multiply and divide (#5563)
1 parent 5f1440d commit a3ca37e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
 

‎spec/std/time/span_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,21 @@ describe Time::Span do
198198
it "test multiply" do
199199
t1 = Time::Span.new 5, 4, 3, 2, 1_000_000
200200
t2 = t1 * 61
201+
t3 = t1 * 0.5
201202

202203
t2.should eq(Time::Span.new 315, 7, 5, 2, 61_000_000)
204+
t3.should eq(Time::Span.new 2, 14, 1, 31, 500_000)
203205

204206
# TODO check overflow
205207
end
206208

207209
it "test divide" do
208210
t1 = Time::Span.new 3, 3, 3, 3, 3_000_000
209211
t2 = t1 / 2
212+
t3 = t1 / 1.5
210213

211214
t2.should eq(Time::Span.new(1, 13, 31, 31, 501_000_000) + Time::Span.new(nanoseconds: 500_000))
215+
t3.should eq(Time::Span.new 2, 2, 2, 2, 2_000_000)
212216

213217
# TODO check overflow
214218
end

‎src/time/span.cr

+14-3
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,21 @@ struct Time::Span
291291
end
292292

293293
# Returns a `Time::Span` that is *number* times longer.
294-
def *(number : Number) : Time::Span
294+
def *(number : Int) : Time::Span
295295
# TODO check overflow
296296
Span.new(
297-
seconds: to_i.to_i64 * number,
297+
seconds: to_i * number,
298298
nanoseconds: nanoseconds.to_i64 * number,
299299
)
300300
end
301301

302-
def /(number : Number) : Time::Span
302+
# Returns a `Time::Span` that is *number* times longer.
303+
def *(number : Float) : Time::Span
304+
(total_nanoseconds * number).nanoseconds
305+
end
306+
307+
# Returns a `Time::Span` that is divided by *number*.
308+
def /(number : Int) : Time::Span
303309
seconds = to_i.tdiv(number)
304310
nanoseconds = self.nanoseconds.tdiv(number)
305311

@@ -313,6 +319,11 @@ struct Time::Span
313319
)
314320
end
315321

322+
# Returns a `Time::Span` that is divided by *number*.
323+
def /(number : Float) : Time::Span
324+
(total_nanoseconds / number).nanoseconds
325+
end
326+
316327
def /(other : self) : Float64
317328
total_nanoseconds.to_f64 / other.total_nanoseconds.to_f64
318329
end

0 commit comments

Comments
 (0)