From 7ab73765e44286f1669edcf9e8c3c10fa27f6d0a Mon Sep 17 00:00:00 2001 From: limited_dev Date: Wed, 12 Jul 2023 13:05:12 +0200 Subject: [PATCH] feat: added function to get a ZonedDateTimeObject from a string, added function to generate a cronjob string from a ZonedDateTime object Signed-off-by: limited_dev --- .../kotlin/net/moonleay/lilJudd/util/TimeUtil.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/kotlin/net/moonleay/lilJudd/util/TimeUtil.kt b/src/main/kotlin/net/moonleay/lilJudd/util/TimeUtil.kt index 7e5fbec..c3c3e54 100644 --- a/src/main/kotlin/net/moonleay/lilJudd/util/TimeUtil.kt +++ b/src/main/kotlin/net/moonleay/lilJudd/util/TimeUtil.kt @@ -20,8 +20,10 @@ package net.moonleay.lilJudd.util import kotlinx.datetime.DayOfWeek import java.time.Duration +import java.time.LocalDateTime import java.time.ZoneId import java.time.ZonedDateTime +import java.time.format.DateTimeFormatter import java.util.concurrent.TimeUnit @@ -142,4 +144,18 @@ object TimeUtil { return ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withDayOfMonth(getMondayDayOfMonth()).withHour(4) .withMinute(0).withSecond(0) } + + fun getDateFromString(input: String): ZonedDateTime { + val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm") + val localDateTime = LocalDateTime.parse(input, formatter) + val zoneId = ZoneId.of("UTC+2") // TODO: Add the possibility to set your timezone + return ZonedDateTime.of(localDateTime, zoneId) + } + + fun getCronjobStringFromDate(zdt: ZonedDateTime): String { + // I'll have to add the possibility to set your timezone in the future + // Only subtracting 1 hour, because I want to run the job 1 hour later + val zdt_ = zdt.minusHours(1) + return "0 ${zdt_.minute} ${zdt_.hour} ${zdt_.dayOfMonth - 1} ${zdt_.month.value - 1} ${zdt_.year}"// 0o *w" + } }