Skip to content

Commit

Permalink
Allow durations to be multiplied by floats (#798)
Browse files Browse the repository at this point in the history
* Allow durations to be multiplied by floats

* Tiny improvement
  • Loading branch information
Erik Corry authored Jun 15, 2022
1 parent 7eb3fde commit f1d7923
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/core/time.toit
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ class Duration implements Comparable:
print 5 * t_5s // Error, num's * does not know Duration!
```
*/
operator * factor/int -> Duration:
return Duration ns_ * factor
operator * factor/num -> Duration:
return Duration (factor * ns_).to_int

/**
Divides the duration by the $factor.
Expand All @@ -431,8 +431,8 @@ class Duration implements Comparable:
print t_9s / 3 // >> 3s
```
*/
operator / factor/int -> Duration:
return Duration ns_ / factor
operator / factor/num -> Duration:
return Duration (ns_ / factor).to_int

/**
A constant 0-duration singleton.
Expand Down
3 changes: 3 additions & 0 deletions tests/time_test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,10 @@ main:
expect_equals (Duration --h=1 --m=1) d2 - d1
expect_equals (Duration --h=-1) -d1
expect_equals (Duration --h=4 --m=2) d2 * 2
expect_equals (Duration --h=4 --m=2) d2 * 2.0
expect_equals (Duration --h=1 --s=30) d2 * 0.5
expect_equals (Duration --h=1 --s=30) d2 / 2
expect_equals (Duration --h=4 --m=2) d2 / 0.5

with_dst := Time.local 2020 10 25 2 59 --dst
without_dst := Time.local 2020 10 25 2 59 --no-dst
Expand Down

0 comments on commit f1d7923

Please sign in to comment.