diff --git a/spec/std/time/span_spec.cr b/spec/std/time/span_spec.cr index e5905839b9c5..15fadeb05b52 100644 --- a/spec/std/time/span_spec.cr +++ b/spec/std/time/span_spec.cr @@ -198,8 +198,10 @@ describe Time::Span do it "test multiply" do t1 = Time::Span.new 5, 4, 3, 2, 1_000_000 t2 = t1 * 61 + t3 = t1 * 0.5 t2.should eq(Time::Span.new 315, 7, 5, 2, 61_000_000) + t3.should eq(Time::Span.new 2, 14, 1, 31, 500_000) # TODO check overflow end @@ -207,8 +209,10 @@ describe Time::Span do it "test divide" do t1 = Time::Span.new 3, 3, 3, 3, 3_000_000 t2 = t1 / 2 + t3 = t1 / 1.5 t2.should eq(Time::Span.new(1, 13, 31, 31, 501_000_000) + Time::Span.new(nanoseconds: 500_000)) + t3.should eq(Time::Span.new 2, 2, 2, 2, 2_000_000) # TODO check overflow end diff --git a/src/time/span.cr b/src/time/span.cr index e9e5e28218da..894af5eb33cb 100644 --- a/src/time/span.cr +++ b/src/time/span.cr @@ -291,15 +291,21 @@ struct Time::Span end # Returns a `Time::Span` that is *number* times longer. - def *(number : Number) : Time::Span + def *(number : Int) : Time::Span # TODO check overflow Span.new( - seconds: to_i.to_i64 * number, + seconds: to_i * number, nanoseconds: nanoseconds.to_i64 * number, ) end - def /(number : Number) : Time::Span + # Returns a `Time::Span` that is *number* times longer. + def *(number : Float) : Time::Span + (total_nanoseconds * number).nanoseconds + end + + # Returns a `Time::Span` that is divided by *number*. + def /(number : Int) : Time::Span seconds = to_i.tdiv(number) nanoseconds = self.nanoseconds.tdiv(number) @@ -313,6 +319,11 @@ struct Time::Span ) end + # Returns a `Time::Span` that is divided by *number*. + def /(number : Float) : Time::Span + (total_nanoseconds / number).nanoseconds + end + def /(other : self) : Float64 total_nanoseconds.to_f64 / other.total_nanoseconds.to_f64 end