Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.timeunit |
| 6 | |
| 7 | /** |
| 8 | * Time unit. This class is provided for better JVM interoperability. |
| 9 | * **It is available for common code, but its use in common code is not recommended.** |
| 10 | */ |
| 11 | @Deprecated("Using this TimeUnit enum in JS code is not recommended, use functions without it") |
| 12 | public actual enum class TimeUnit { |
| 13 | /** Milliseconds. */ |
| 14 | MILLISECONDS, |
| 15 | /** Seconds. */ |
| 16 | SECONDS; |
| 17 | |
| 18 | /** |
| 19 | * Converts time in this time unit to milliseconds. |
| 20 | */ |
| 21 | public actual fun toMillis(time: Long): Long = when(this) { |
| 22 | MILLISECONDS -> time |
| 23 | SECONDS -> when { |
| 24 | time >= Long.MAX_VALUE / 1000L -> Long.MAX_VALUE |
| 25 | time <= Long.MIN_VALUE / 1000L -> Long.MIN_VALUE |
| 26 | else -> time * 1000L |
| 27 | } |
| 28 | } |
| 29 | } |