Last active
February 20, 2020 21:08
-
-
Save ncornette/56313ef014ff82a1d1eaa37245278342 to your computer and use it in GitHub Desktop.
Kotlin extension functions for java TimeUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
import org.assertj.core.api.Assertions | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import org.junit.runners.Parameterized | |
import java.util.concurrent.TimeUnit | |
@RunWith(Parameterized::class) | |
class KotlinTimeUnitTest(private val timeValue: Long) { | |
companion object { | |
@JvmStatic | |
@Parameterized.Parameters | |
fun data() = listOf(1L, 100L, 1000L, 10000L, 100000L, 1000000L, 10000000L) | |
} | |
@Test | |
fun testTimeUnitMillis() { | |
Assertions.assertThat(timeValue.milliseconds.toDays()).isEqualTo(TimeUnit.MILLISECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toHours()).isEqualTo(TimeUnit.MILLISECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toMinutes()).isEqualTo(TimeUnit.MILLISECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toSeconds()).isEqualTo(TimeUnit.MILLISECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toMillis()).isEqualTo(TimeUnit.MILLISECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toMicros()).isEqualTo(TimeUnit.MILLISECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.milliseconds.toNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitDays() { | |
Assertions.assertThat(timeValue.days.toDays()).isEqualTo(TimeUnit.DAYS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.days.toHours()).isEqualTo(TimeUnit.DAYS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.days.toMinutes()).isEqualTo(TimeUnit.DAYS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.days.toSeconds()).isEqualTo(TimeUnit.DAYS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.days.toMillis()).isEqualTo(TimeUnit.DAYS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.days.toMicros()).isEqualTo(TimeUnit.DAYS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.days.toNanos()).isEqualTo(TimeUnit.DAYS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitHours() { | |
Assertions.assertThat(timeValue.hours.toDays()).isEqualTo(TimeUnit.HOURS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.hours.toHours()).isEqualTo(TimeUnit.HOURS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.hours.toMinutes()).isEqualTo(TimeUnit.HOURS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.hours.toSeconds()).isEqualTo(TimeUnit.HOURS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.hours.toMillis()).isEqualTo(TimeUnit.HOURS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.hours.toMicros()).isEqualTo(TimeUnit.HOURS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.hours.toNanos()).isEqualTo(TimeUnit.HOURS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMicros() { | |
Assertions.assertThat(timeValue.microseconds.toDays()).isEqualTo(TimeUnit.MICROSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toHours()).isEqualTo(TimeUnit.MICROSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toMinutes()).isEqualTo(TimeUnit.MICROSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toSeconds()).isEqualTo(TimeUnit.MICROSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toMillis()).isEqualTo(TimeUnit.MICROSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toMicros()).isEqualTo(TimeUnit.MICROSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.microseconds.toNanos()).isEqualTo(TimeUnit.MICROSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMinutes() { | |
Assertions.assertThat(timeValue.minutes.toDays()).isEqualTo(TimeUnit.MINUTES.toDays(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toHours()).isEqualTo(TimeUnit.MINUTES.toHours(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toMinutes()).isEqualTo(TimeUnit.MINUTES.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toSeconds()).isEqualTo(TimeUnit.MINUTES.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toMillis()).isEqualTo(TimeUnit.MINUTES.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toMicros()).isEqualTo(TimeUnit.MINUTES.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.minutes.toNanos()).isEqualTo(TimeUnit.MINUTES.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitNanos() { | |
Assertions.assertThat(timeValue.nanoseconds.toDays()).isEqualTo(TimeUnit.NANOSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toHours()).isEqualTo(TimeUnit.NANOSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toMinutes()).isEqualTo(TimeUnit.NANOSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toSeconds()).isEqualTo(TimeUnit.NANOSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toMillis()).isEqualTo(TimeUnit.NANOSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toMicros()).isEqualTo(TimeUnit.NANOSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.nanoseconds.toNanos()).isEqualTo(TimeUnit.NANOSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitSeconds() { | |
Assertions.assertThat(timeValue.seconds.toDays()).isEqualTo(TimeUnit.SECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toHours()).isEqualTo(TimeUnit.SECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toMinutes()).isEqualTo(TimeUnit.SECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toSeconds()).isEqualTo(TimeUnit.SECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toMillis()).isEqualTo(TimeUnit.SECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toMicros()).isEqualTo(TimeUnit.SECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.seconds.toNanos()).isEqualTo(TimeUnit.SECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMillisInt() { | |
Assertions.assertThat(timeValue.toInt().milliseconds.toDays()).isEqualTo(TimeUnit.MILLISECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toHours()).isEqualTo(TimeUnit.MILLISECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toMinutes()).isEqualTo(TimeUnit.MILLISECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toSeconds()).isEqualTo(TimeUnit.MILLISECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toMillis()).isEqualTo(TimeUnit.MILLISECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toMicros()).isEqualTo(TimeUnit.MILLISECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().milliseconds.toNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitDaysInt() { | |
Assertions.assertThat(timeValue.toInt().days.toDays()).isEqualTo(TimeUnit.DAYS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toHours()).isEqualTo(TimeUnit.DAYS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toMinutes()).isEqualTo(TimeUnit.DAYS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toSeconds()).isEqualTo(TimeUnit.DAYS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toMillis()).isEqualTo(TimeUnit.DAYS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toMicros()).isEqualTo(TimeUnit.DAYS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().days.toNanos()).isEqualTo(TimeUnit.DAYS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitHoursInt() { | |
Assertions.assertThat(timeValue.toInt().hours.toDays()).isEqualTo(TimeUnit.HOURS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toHours()).isEqualTo(TimeUnit.HOURS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toMinutes()).isEqualTo(TimeUnit.HOURS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toSeconds()).isEqualTo(TimeUnit.HOURS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toMillis()).isEqualTo(TimeUnit.HOURS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toMicros()).isEqualTo(TimeUnit.HOURS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().hours.toNanos()).isEqualTo(TimeUnit.HOURS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMicrosInt() { | |
Assertions.assertThat(timeValue.toInt().microseconds.toDays()).isEqualTo(TimeUnit.MICROSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toHours()).isEqualTo(TimeUnit.MICROSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toMinutes()).isEqualTo(TimeUnit.MICROSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toSeconds()).isEqualTo(TimeUnit.MICROSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toMillis()).isEqualTo(TimeUnit.MICROSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toMicros()).isEqualTo(TimeUnit.MICROSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().microseconds.toNanos()).isEqualTo(TimeUnit.MICROSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMinutesInt() { | |
Assertions.assertThat(timeValue.toInt().minutes.toDays()).isEqualTo(TimeUnit.MINUTES.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toHours()).isEqualTo(TimeUnit.MINUTES.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toMinutes()).isEqualTo(TimeUnit.MINUTES.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toSeconds()).isEqualTo(TimeUnit.MINUTES.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toMillis()).isEqualTo(TimeUnit.MINUTES.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toMicros()).isEqualTo(TimeUnit.MINUTES.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().minutes.toNanos()).isEqualTo(TimeUnit.MINUTES.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitNanosInt() { | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toDays()).isEqualTo(TimeUnit.NANOSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toHours()).isEqualTo(TimeUnit.NANOSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toMinutes()).isEqualTo(TimeUnit.NANOSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toSeconds()).isEqualTo(TimeUnit.NANOSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toMillis()).isEqualTo(TimeUnit.NANOSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toMicros()).isEqualTo(TimeUnit.NANOSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().nanoseconds.toNanos()).isEqualTo(TimeUnit.NANOSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitSecondsInt() { | |
Assertions.assertThat(timeValue.toInt().seconds.toDays()).isEqualTo(TimeUnit.SECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toHours()).isEqualTo(TimeUnit.SECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toMinutes()).isEqualTo(TimeUnit.SECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toSeconds()).isEqualTo(TimeUnit.SECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toMillis()).isEqualTo(TimeUnit.SECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toMicros()).isEqualTo(TimeUnit.SECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toInt().seconds.toNanos()).isEqualTo(TimeUnit.SECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMillisFloat() { | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toDays()).isEqualTo(TimeUnit.MILLISECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toHours()).isEqualTo(TimeUnit.MILLISECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toMinutes()).isEqualTo(TimeUnit.MILLISECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toSeconds()).isEqualTo(TimeUnit.MILLISECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toMillis()).isEqualTo(TimeUnit.MILLISECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toMicros()).isEqualTo(TimeUnit.MILLISECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().milliseconds.toNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitDaysFloat() { | |
Assertions.assertThat(timeValue.toFloat().days.toDays()).isEqualTo(TimeUnit.DAYS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toHours()).isEqualTo(TimeUnit.DAYS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toMinutes()).isEqualTo(TimeUnit.DAYS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toSeconds()).isEqualTo(TimeUnit.DAYS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toMillis()).isEqualTo(TimeUnit.DAYS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toMicros()).isEqualTo(TimeUnit.DAYS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().days.toNanos()).isEqualTo(TimeUnit.DAYS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitHoursFloat() { | |
Assertions.assertThat(timeValue.toFloat().hours.toDays()).isEqualTo(TimeUnit.HOURS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toHours()).isEqualTo(TimeUnit.HOURS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toMinutes()).isEqualTo(TimeUnit.HOURS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toSeconds()).isEqualTo(TimeUnit.HOURS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toMillis()).isEqualTo(TimeUnit.HOURS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toMicros()).isEqualTo(TimeUnit.HOURS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().hours.toNanos()).isEqualTo(TimeUnit.HOURS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMicrosFloat() { | |
Assertions.assertThat(timeValue.toFloat().microseconds.toDays()).isEqualTo(TimeUnit.MICROSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toHours()).isEqualTo(TimeUnit.MICROSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toMinutes()).isEqualTo(TimeUnit.MICROSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toSeconds()).isEqualTo(TimeUnit.MICROSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toMillis()).isEqualTo(TimeUnit.MICROSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toMicros()).isEqualTo(TimeUnit.MICROSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().microseconds.toNanos()).isEqualTo(TimeUnit.MICROSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitMinutesFloat() { | |
Assertions.assertThat(timeValue.toFloat().minutes.toDays()).isEqualTo(TimeUnit.MINUTES.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toHours()).isEqualTo(TimeUnit.MINUTES.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toMinutes()).isEqualTo(TimeUnit.MINUTES.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toSeconds()).isEqualTo(TimeUnit.MINUTES.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toMillis()).isEqualTo(TimeUnit.MINUTES.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toMicros()).isEqualTo(TimeUnit.MINUTES.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().minutes.toNanos()).isEqualTo(TimeUnit.MINUTES.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitNanosFloat() { | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toDays()).isEqualTo(TimeUnit.NANOSECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toHours()).isEqualTo(TimeUnit.NANOSECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toMinutes()).isEqualTo(TimeUnit.NANOSECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toSeconds()).isEqualTo(TimeUnit.NANOSECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toMillis()).isEqualTo(TimeUnit.NANOSECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toMicros()).isEqualTo(TimeUnit.NANOSECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().nanoseconds.toNanos()).isEqualTo(TimeUnit.NANOSECONDS.toNanos(timeValue)) | |
} | |
@Test | |
fun testTimeUnitSecondsFloat() { | |
Assertions.assertThat(timeValue.toFloat().seconds.toDays()).isEqualTo(TimeUnit.SECONDS.toDays(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toHours()).isEqualTo(TimeUnit.SECONDS.toHours(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toMinutes()).isEqualTo(TimeUnit.SECONDS.toMinutes(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toSeconds()).isEqualTo(TimeUnit.SECONDS.toSeconds(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toMillis()).isEqualTo(TimeUnit.SECONDS.toMillis(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toMicros()).isEqualTo(TimeUnit.SECONDS.toMicros(timeValue)) | |
Assertions.assertThat(timeValue.toFloat().seconds.toNanos()).isEqualTo(TimeUnit.SECONDS.toNanos(timeValue)) | |
} | |
} | |
val Long.nanoseconds: Duration get() = Duration(this, TimeUnit.NANOSECONDS) | |
val Long.microseconds: Duration get() = Duration(this, TimeUnit.MICROSECONDS) | |
val Long.milliseconds: Duration get() = Duration(this, TimeUnit.MILLISECONDS) | |
val Long.seconds: Duration get() = Duration(this, TimeUnit.SECONDS) | |
val Long.minutes: Duration get() = Duration(this, TimeUnit.MINUTES) | |
val Long.hours: Duration get() = Duration(this, TimeUnit.HOURS) | |
val Long.days: Duration get() = Duration(this, TimeUnit.DAYS) | |
val Int.nanoseconds: Duration get() = this.toLong().nanoseconds | |
val Int.microseconds: Duration get() = this.toLong().microseconds | |
val Int.milliseconds: Duration get() = this.toLong().milliseconds | |
val Int.seconds: Duration get() = this.toLong().seconds | |
val Int.minutes: Duration get() = this.toLong().minutes | |
val Int.hours: Duration get() = this.toLong().hours | |
val Int.days: Duration get() = this.toLong().days | |
val Float.nanoseconds: Duration get() = this.toLong().nanoseconds | |
val Float.microseconds: Duration get() = this.toDouble().microseconds | |
val Float.milliseconds: Duration get() = this.toDouble().milliseconds | |
val Float.seconds: Duration get() = this.toDouble().seconds | |
val Float.minutes: Duration get() = this.toDouble().minutes | |
val Float.hours: Duration get() = this.toDouble().hours | |
val Float.days: Duration get() = this.toDouble().days | |
val Double.nanoseconds: Duration get() = this.toLong().nanoseconds | |
val Double.microseconds: Duration get() = (this * 1000).toLong().nanoseconds | |
val Double.milliseconds: Duration get() = (this * 1000).toLong().microseconds | |
val Double.seconds: Duration get() = (this * 1000).toLong().milliseconds | |
val Double.minutes: Duration get() = (this * 1000 * 60).toLong().milliseconds | |
val Double.hours: Duration get() = (this * 1000 * 60 * 60).toLong().milliseconds | |
val Double.days: Duration get() = (this * 1000 * 60 * 60 * 24).toLong().milliseconds | |
data class Duration(private val value: Long, private val unit: TimeUnit) { | |
fun toNanos() = unit.toNanos(value) | |
fun toMicros() = unit.toMicros(value) | |
fun toMillis() = unit.toMillis(value) | |
fun toSeconds() = unit.toSeconds(value) | |
fun toMinutes() = unit.toMinutes(value) | |
fun toHours() = unit.toHours(value) | |
fun toDays() = unit.toDays(value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It allows to make calls like :
10.days.toMinutes()
or1.5.minutes.toSeconds()