|
17 | 17 | */ |
18 | 18 | package spp.protocol.utils |
19 | 19 |
|
20 | | -fun Int.toPrettyDuration(): String { |
| 20 | +import kotlin.jvm.JvmOverloads |
| 21 | + |
| 22 | +fun Int.toPrettyDuration(translate: (String)->String = { it }): String { |
21 | 23 | val days = this / 86400000.0 |
22 | 24 | if (days > 1) { |
23 | | - return "${days.toInt()}dys" |
| 25 | + return "${days.toInt()}" + translate("dys") |
24 | 26 | } |
25 | 27 | val hours = this / 3600000.0 |
26 | 28 | if (hours > 1) { |
27 | | - return "${hours.toInt()}hrs" |
| 29 | + return "${hours.toInt()}" + translate("hrs") |
28 | 30 | } |
29 | 31 | val minutes = this / 60000.0 |
30 | 32 | if (minutes > 1) { |
31 | | - return "${minutes.toInt()}mins" |
| 33 | + return "${minutes.toInt()}" + translate("mins") |
32 | 34 | } |
33 | 35 | val seconds = this / 1000.0 |
34 | 36 | if (seconds > 1) { |
35 | | - return "${seconds.toInt()}secs" |
| 37 | + return "${seconds.toInt()}" + translate("secs") |
36 | 38 | } |
37 | | - return "${this}ms" |
| 39 | + return "$this" + translate("ms") |
38 | 40 | } |
39 | 41 |
|
40 | | -fun Double.fromPerSecondToPrettyFrequency(): String { |
| 42 | +@JvmOverloads |
| 43 | +fun Double.fromPerSecondToPrettyFrequency(translate: (String)->String = { it }): String { |
41 | 44 | return when { |
42 | | - this > 1000000.0 -> "${this / 1000000.0.toInt()}M/sec" |
43 | | - this > 1000.0 -> "${this / 1000.0.toInt()}K/sec" |
44 | | - this > 1.0 -> "${this.toInt()}/sec" |
45 | | - else -> "${(this * 60.0).toInt()}/min" |
| 45 | + this > 1000000.0 -> "${this / 1000000.0.toInt()}M/" + translate("sec") |
| 46 | + this > 1000.0 -> "${this / 1000.0.toInt()}K/" + translate("sec") |
| 47 | + this > 1.0 -> "${this.toInt()}/" + translate("sec") |
| 48 | + else -> "${(this * 60.0).toInt()}/" + translate.invoke("min") |
46 | 49 | } |
47 | 50 | } |
48 | 51 |
|
49 | | -fun Long.toPrettyDuration(): String { |
| 52 | +fun Long.toPrettyDuration(translate: (String)->String = { it }): String { |
50 | 53 | val days = this / 86400000.0 |
51 | 54 | if (days > 1) { |
52 | | - return "${days.toInt()}d" |
| 55 | + return "${days.toInt()}" + translate("dys") |
53 | 56 | } |
54 | 57 | val hours = this / 3600000.0 |
55 | 58 | if (hours > 1) { |
56 | | - return "${hours.toInt()}h" |
| 59 | + return "${hours.toInt()}" + translate("hrs") |
57 | 60 | } |
58 | 61 | val minutes = this / 60000.0 |
59 | 62 | if (minutes > 1) { |
60 | | - return "${minutes.toInt()}m" |
| 63 | + return "${minutes.toInt()}" + translate("mins") |
61 | 64 | } |
62 | 65 | val seconds = this / 1000.0 |
63 | 66 | if (seconds > 1) { |
64 | | - return "${seconds.toInt()}s" |
| 67 | + return "${seconds.toInt()}" + translate("secs") |
65 | 68 | } |
66 | | - return "${this}ms" |
| 69 | + return "$this" + translate("ms") |
67 | 70 | } |
0 commit comments