Skip to content

Commit

Permalink
Add example for creating custom time units.
Browse files Browse the repository at this point in the history
  • Loading branch information
kizitonwose committed Oct 26, 2017
1 parent 3ef832c commit e5baf40
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions time/src/test/kotlin/com/kizitonwose/time/TimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,25 @@ class TimeTest {

assertTrue(tenDaysAgo < now)
}

@Test
fun `custom time units work as expected`() {
val twoWeeks = 2.weeks
val fourteenDays = 14.days

assertEquals(twoWeeks, fourteenDays)
assertEquals(336.hours.inWeeks, twoWeeks)
}
}


// Custom time unit.
class Week : TimeUnit {
override val timeIntervalRatio = 604800.0
}

val Number.weeks: Interval<Week>
get() = Interval(this.toDouble())

val Interval<TimeUnit>.inWeeks: Interval<Week>
get() = converted()

0 comments on commit e5baf40

Please sign in to comment.