diff --git a/src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt b/src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt index 12fedd7..21b109e 100644 --- a/src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt +++ b/src/main/kotlin/net/moonleay/rssbot/util/MessageUtil.kt @@ -29,7 +29,7 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter object MessageUtil { - private val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy @ HH:mm:ss") + private val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss") ///Send an embedded message as a reply suspend fun sendEmbedForPublicSlashCommand( diff --git a/src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt b/src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt index 644d0c0..18fa0f1 100644 --- a/src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt +++ b/src/main/kotlin/net/moonleay/rssbot/util/TimeUtil.kt @@ -20,13 +20,27 @@ package net.moonleay.rssbot.util import java.time.ZonedDateTime import java.time.format.DateTimeFormatter +import java.time.format.DateTimeParseException object TimeUtil { fun getUnixTimeFromStamp(input: String): Long { // Pattern: Sun, 15 Oct 2023 11:04:57 GMT - val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z") - val result = ZonedDateTime.parse(input, formatter).toEpochSecond() * 1000 - Logger.out("Converted $input to $result") - return result - } + try { + val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz") + val result = ZonedDateTime.parse(input, formatter).toEpochSecond() * 1000 + Logger.out("Converted $input to $result") + return result + } catch (e: DateTimeParseException) { + Logger.out("Could not convert $input to unix time, trying other pattern") + return try { + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX") + val result = ZonedDateTime.parse(input, formatter).toEpochSecond() * 1000 + Logger.out("Converted $input to $result") + result + } catch (e: DateTimeParseException) { + Logger.out("Could not convert $input to unix time") + 0 + } + } + } // I'm ashamed of this code. I'm sorry. I don't know how to do it better. }