Switched uptime to Duration API.

This commit is contained in:
Erik C. Thauvin 2021-11-28 23:17:35 -08:00
parent 7c270ed872
commit 4e1e09b069

View file

@ -49,8 +49,9 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.util.Date import java.util.Date
import java.util.Properties import java.util.Properties
import java.util.concurrent.TimeUnit
import java.util.stream.Collectors import java.util.stream.Collectors
import kotlin.time.DurationUnit
import kotlin.time.toDuration
/** /**
* Miscellaneous utilities. * Miscellaneous utilities.
@ -64,7 +65,7 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun String.appendIfMissing(suffix: Char): String { fun String.appendIfMissing(suffix: Char): String {
return if (this.last() != suffix) { return if (last() != suffix) {
"$this${suffix}" "$this${suffix}"
} else { } else {
this this
@ -75,19 +76,19 @@ object Utils {
* Makes the given int bold. * Makes the given int bold.
*/ */
@JvmStatic @JvmStatic
fun Int.bold(): String = this.toString().bold() fun Int.bold(): String = toString().bold()
/** /**
* Makes the given long bold. * Makes the given long bold.
*/ */
@JvmStatic @JvmStatic
fun Long.bold(): String = this.toString().bold() fun Long.bold(): String = toString().bold()
/** /**
* Makes the given string bold. * Makes the given string bold.
*/ */
@JvmStatic @JvmStatic
fun String?.bold(): String = this.colorize(Colors.BOLD) fun String?.bold(): String = colorize(Colors.BOLD)
/** /**
* Returns the [PircBotX] instance. * Returns the [PircBotX] instance.
@ -110,7 +111,7 @@ object Utils {
* Capitalize a string. * Capitalize a string.
*/ */
@JvmStatic @JvmStatic
fun String.capitalise(): String = this.lowercase().replaceFirstChar { it.uppercase() } fun String.capitalise(): String = lowercase().replaceFirstChar { it.uppercase() }
/** /**
* Capitalize words * Capitalize words
@ -123,7 +124,7 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun String?.colorize(color: String): String { fun String?.colorize(color: String): String {
return if (this.isNullOrEmpty()) { return if (isNullOrEmpty()) {
"" ""
} else if (color == DEFAULT_COLOR) { } else if (color == DEFAULT_COLOR) {
this this
@ -138,7 +139,7 @@ object Utils {
* Makes the given string cyan. * Makes the given string cyan.
*/ */
@JvmStatic @JvmStatic
fun String?.cyan(): String = this.colorize(Colors.CYAN) fun String?.cyan(): String = colorize(Colors.CYAN)
/** /**
* URL encodes the given string. * URL encodes the given string.
@ -151,14 +152,14 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun Properties.getIntProperty(key: String, defaultValue: Int): Int { fun Properties.getIntProperty(key: String, defaultValue: Int): Int {
return this.getProperty(key)?.toIntOrDefault(defaultValue) ?: defaultValue return getProperty(key)?.toIntOrDefault(defaultValue) ?: defaultValue
} }
/** /**
* Makes the given string green. * Makes the given string green.
*/ */
@JvmStatic @JvmStatic
fun String?.green(): String = this.colorize(Colors.DARK_GREEN) fun String?.green(): String = colorize(Colors.DARK_GREEN)
/** /**
* Returns a formatted help string. * Returns a formatted help string.
@ -178,13 +179,23 @@ object Utils {
return event.bot().userChannelDao.getChannel(channel).isOp(event.user) return event.bot().userChannelDao.getChannel(channel).isOp(event.user)
} }
/**
* Return the last item of a list of strings or empty if none.
*/
fun List<String>.lastOrEmpty() : String {
return if (this.size >= 2) {
this.last()
} else
""
}
/** /**
* Obfuscates the given string. * Obfuscates the given string.
*/ */
@JvmStatic @JvmStatic
fun String.obfuscate(): String { fun String.obfuscate(): String {
return if (this.isNotBlank()) { return if (isNotBlank()) {
"x".repeat(this.length) "x".repeat(length)
} else this } else this
} }
@ -200,7 +211,7 @@ object Utils {
* Makes the given string red. * Makes the given string red.
*/ */
@JvmStatic @JvmStatic
fun String?.red(): String = this.colorize(Colors.RED) fun String?.red(): String = colorize(Colors.RED)
/** /**
* Replaces all occurrences of Strings within another String. * Replaces all occurrences of Strings within another String.
@ -220,7 +231,7 @@ object Utils {
* Makes the given string reverse color. * Makes the given string reverse color.
*/ */
@JvmStatic @JvmStatic
fun String?.reverseColor(): String = this.colorize(Colors.REVERSE) fun String?.reverseColor(): String = colorize(Colors.REVERSE)
/** /**
* Send a formatted commands/modules, etc. list. * Send a formatted commands/modules, etc. list.
@ -284,7 +295,7 @@ object Utils {
@JvmStatic @JvmStatic
fun String.toIntOrDefault(defaultValue: Int): Int { fun String.toIntOrDefault(defaultValue: Int): Int {
return try { return try {
this.toInt() toInt()
} catch (e: NumberFormatException) { } catch (e: NumberFormatException) {
defaultValue defaultValue
} }
@ -295,28 +306,28 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun Date.toIsoLocalDate(): String { fun Date.toIsoLocalDate(): String {
return LocalDateTime.ofInstant(this.toInstant(), ZoneId.systemDefault()).toIsoLocalDate() return LocalDateTime.ofInstant(toInstant(), ZoneId.systemDefault()).toIsoLocalDate()
} }
/** /**
* Returns the specified date as an ISO local date string. * Returns the specified date as an ISO local date string.
*/ */
@JvmStatic @JvmStatic
fun LocalDateTime.toIsoLocalDate(): String = this.format(DateTimeFormatter.ISO_LOCAL_DATE) fun LocalDateTime.toIsoLocalDate(): String = format(DateTimeFormatter.ISO_LOCAL_DATE)
/** /**
* Returns the specified date formatted as `yyyy-MM-dd HH:mm`. * Returns the specified date formatted as `yyyy-MM-dd HH:mm`.
*/ */
@JvmStatic @JvmStatic
fun Date.toUtcDateTime(): String { fun Date.toUtcDateTime(): String {
return LocalDateTime.ofInstant(this.toInstant(), ZoneId.systemDefault()).toUtcDateTime() return LocalDateTime.ofInstant(toInstant(), ZoneId.systemDefault()).toUtcDateTime()
} }
/** /**
* Returns the specified date formatted as `yyyy-MM-dd HH:mm`. * Returns the specified date formatted as `yyyy-MM-dd HH:mm`.
*/ */
@JvmStatic @JvmStatic
fun LocalDateTime.toUtcDateTime(): String = this.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) fun LocalDateTime.toUtcDateTime(): String = format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
/** /**
* Converts XML/XHTML entities to plain text. * Converts XML/XHTML entities to plain text.
@ -329,38 +340,35 @@ object Utils {
*/ */
@JvmStatic @JvmStatic
fun uptime(uptime: Long): String { fun uptime(uptime: Long): String {
val info = StringBuilder() uptime.toDuration(DurationUnit.MILLISECONDS).toComponents { wholeDays, hours, minutes, _, _ ->
var days = TimeUnit.MILLISECONDS.toDays(uptime) val years = wholeDays / 365
val years = days / 365 var days = wholeDays % 365
days %= 365 val months = days / 30
val months = days / 30 days %= 30
days %= 30 val weeks = days / 7
val weeks = days / 7 days %= 7
days %= 7
val hours = TimeUnit.MILLISECONDS.toHours(uptime) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(uptime))
val minutes =
TimeUnit.MILLISECONDS.toMinutes(uptime) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(uptime))
with(info) { with(StringBuffer()) {
if (years > 0) { if (years > 0) {
append(years).append(" year".plural(years)).append(' ') append(years).append(" year".plural(years)).append(' ')
} }
if (months > 0) { if (months > 0) {
append(weeks).append(" month".plural(months)).append(' ') append(weeks).append(" month".plural(months)).append(' ')
} }
if (weeks > 0) { if (weeks > 0) {
append(weeks).append(" week".plural(weeks)).append(' ') append(weeks).append(" week".plural(weeks)).append(' ')
} }
if (days > 0) { if (days > 0) {
append(days).append(" day".plural(days)).append(' ') append(days).append(" day".plural(days)).append(' ')
} }
if (hours > 0) { if (hours > 0) {
append(hours).append(" hour".plural(hours)).append(' ') append(hours).append(" hour".plural(hours.toLong())).append(' ')
} }
append(minutes).append(" minute".plural(minutes)) append(minutes).append(" minute".plural(minutes.toLong()))
return toString() return toString()
}
} }
} }