Skip to content

Commit

Permalink
Add Duration.*
Browse files Browse the repository at this point in the history
This allows multiplying a Duration with an Int, such as
`Duration.from_secs(1) * 5`.

Changelog: added
  • Loading branch information
yorickpeterse committed Sep 24, 2024
1 parent 15ed990 commit 9f68de6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion std/src/std/time.inko
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import std.cmp (Compare, Equal, Ordering)
import std.float (ToFloat)
import std.fmt (Format, Formatter)
import std.int (ToInt)
import std.ops (Add, Subtract)
import std.ops (Add, Multiply, Subtract)

fn extern inko_time_monotonic(state: Pointer[UInt8]) -> Int64

Expand Down Expand Up @@ -191,6 +191,12 @@ impl Subtract[Duration, Duration] for Duration {
}
}

impl Multiply[Int, Duration] for Duration {
fn pub *(other: ref Int) -> Duration {
Duration(@nanos * other)
}
}

impl Compare[Duration] for Duration {
fn pub cmp(other: ref Duration) -> Ordering {
@nanos.cmp(other.nanos)
Expand Down
8 changes: 8 additions & 0 deletions std/test/std/test_time.inko
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ fn pub tests(t: mut Tests) {
Duration.from_nanos(-9_223_372_036_854_775_808) - Duration.from_nanos(1)
})

t.test('Duration.*', fn (t) {
t.equal(Duration.from_secs(1) * 5, Duration.from_secs(5))
})

t.panic('Duration.* with an argument that overflows', fn {
Duration.from_secs(1) * -9_223_372_036_854_775_808
})

t.test('Duration.cmp', fn (t) {
let a = Duration.from_secs(1)
let b = Duration.from_secs(2)
Expand Down

0 comments on commit 9f68de6

Please sign in to comment.