fix!: fixed timestamp not converting correctly

Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
moonleay 2023-10-21 14:56:26 +02:00
parent 40c53f4313
commit b3ba4e3e99
2 changed files with 20 additions and 6 deletions

View file

@ -29,7 +29,7 @@ import java.time.LocalDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
object MessageUtil { 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 ///Send an embedded message as a reply
suspend fun sendEmbedForPublicSlashCommand( suspend fun sendEmbedForPublicSlashCommand(

View file

@ -20,13 +20,27 @@ package net.moonleay.rssbot.util
import java.time.ZonedDateTime import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
import java.time.format.DateTimeParseException
object TimeUtil { object TimeUtil {
fun getUnixTimeFromStamp(input: String): Long { // Pattern: Sun, 15 Oct 2023 11:04:57 GMT 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") try {
val result = ZonedDateTime.parse(input, formatter).toEpochSecond() * 1000 val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz")
Logger.out("Converted $input to $result") val result = ZonedDateTime.parse(input, formatter).toEpochSecond() * 1000
return result 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.
} }