🔀 Merge “release/v81” into “master”
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / template / DurationFormatFilter.kt
1 package net.pterodactylus.sone.template
2
3 import net.pterodactylus.util.template.*
4 import java.time.*
5
6 class DurationFormatFilter : Filter {
7
8         override fun format(templateContext: TemplateContext?, data: Any?, parameters: Map<String, Any?>?): Any? {
9                 if (data is Number) {
10                         val scale = parameters?.get("scale")
11                         val duration = when (scale) {
12                                 "ms" -> Duration.ofSeconds(data.toLong() / 1_000, (data.toDouble() * 1_000_000 % 1_000_000_000).toLong())
13                                 "ÎĽs" -> Duration.ofSeconds(data.toLong() / 1_000_000, (data.toDouble() * 1_000 % 1_000_000_000).toLong())
14                                 "ns" -> Duration.ofSeconds(data.toLong() / 1_000_000_000, data.toLong() % 1_000_000_000)
15                                 else -> Duration.ofSeconds(data.toLong(), (data.toDouble() * 1_000_000_000 % 1_000_000_000).toLong())
16                         }
17                         return FixedDuration.values()
18                                         .map { it to it.number(duration) }
19                                         .firstOrNull { it.second >= 1 }
20                                         ?.let { "${"%.1f".format(it.second)}${it.first.symbol}" }
21                                         ?: "0s"
22                 }
23                 return data
24         }
25
26 }
27
28 @Suppress("unused")
29 private enum class FixedDuration {
30
31         WEEKS {
32                 override fun number(duration: Duration) = DAYS.number(duration) / 7.0
33                 override val symbol = "w"
34         },
35         DAYS {
36                 override fun number(duration: Duration) = HOURS.number(duration) / 24
37                 override val symbol = "d"
38         },
39         HOURS {
40                 override fun number(duration: Duration) = MINUTES.number(duration) / 60
41                 override val symbol = "h"
42         },
43         MINUTES {
44                 override fun number(duration: Duration) = SECONDS.number(duration) / 60
45                 override val symbol = "m"
46         },
47         SECONDS {
48                 override fun number(duration: Duration) = duration.seconds + duration.nano / 1_000_000_000.0
49                 override val symbol = "s"
50         },
51         MILLIS {
52                 override fun number(duration: Duration) = duration.nano / 1_000_000.0
53                 override val symbol = "ms"
54         },
55         MICROS {
56                 override fun number(duration: Duration) = duration.nano / 1_000.0
57                 override val symbol = "ÎĽs"
58         },
59         NANOS {
60                 override fun number(duration: Duration) = duration.nano.toDouble()
61                 override val symbol = "ns"
62         };
63
64         abstract fun number(duration: Duration): Double
65         abstract val symbol: String
66
67 }