From 58f30aef1526b41b90506e0e875e24ce63120477 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 15 Jun 2022 12:40:03 +0600 Subject: [PATCH] fix of #27 --- .../kotlin/dev/inmo/krontab/StringParser.kt | 14 +++++++------- .../dev/inmo/krontab/internal/CronDateTime.kt | 2 +- .../krontab/internal/NearDateTimeCalculator.kt | 7 ++++--- .../inmo/krontab/utils/InfinityLoopCheckTest.kt | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 src/commonTest/kotlin/dev/inmo/krontab/utils/InfinityLoopCheckTest.kt diff --git a/src/commonMain/kotlin/dev/inmo/krontab/StringParser.kt b/src/commonMain/kotlin/dev/inmo/krontab/StringParser.kt index 9e1e6da..1a3b1e2 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/StringParser.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/StringParser.kt @@ -57,13 +57,13 @@ typealias KrontabTemplate = String * * "0/5,L * * * *" for every five seconds triggering and on 59 second * * "0/15 30 * * *" for every 15th seconds in a half of each hour * * "0/15 30 * * * 500ms" for every 15th seconds in a half of each hour when milliseconds equal to 500 - * * "1 2 3 F,4,L 5" for triggering in near first second of second minute of third hour of fourth day of may - * * "1 2 3 F,4,L 5 60o" for triggering in near first second of second minute of third hour of fourth day of may with timezone UTC+01:00 - * * "1 2 3 F,4,L 5 60o 0-2w" for triggering in near first second of second minute of third hour of fourth day of may in case if it will be in Sunday-Tuesday week days with timezone UTC+01:00 - * * "1 2 3 F,4,L 5 2021" for triggering in near first second of second minute of third hour of fourth day of may of 2021st year - * * "1 2 3 F,4,L 5 2021 60o" for triggering in near first second of second minute of third hour of fourth day of may of 2021st year with timezone UTC+01:00 - * * "1 2 3 F,4,L 5 2021 60o 0-2w" for triggering in near first second of second minute of third hour of fourth day of may of 2021st year if it will be in Sunday-Tuesday week days with timezone UTC+01:00 - * * "1 2 3 F,4,L 5 2021 60o 0-2w 500ms" for triggering in near first second of second minute of third hour of fourth day of may of 2021st year if it will be in Sunday-Tuesday week days with timezone UTC+01:00 when milliseconds will be equal to 500 + * * "1 2 3 F,4,L 5" for triggering in near first second of second minute of third hour of first, fifth and last days of may + * * "1 2 3 F,4,L 5 60o" for triggering in near first second of second minute of third hour of first, fifth and last days of may with timezone UTC+01:00 + * * "1 2 3 F,4,L 5 60o 0-2w" for triggering in near first second of second minute of third hour of first, fifth and last days of may in case if it will be in Sunday-Tuesday week days with timezone UTC+01:00 + * * "1 2 3 F,4,L 5 2021" for triggering in near first second of second minute of third hour of first, fifth and last days of may of 2021st year + * * "1 2 3 F,4,L 5 2021 60o" for triggering in near first second of second minute of third hour of first, fifth and last days of may of 2021st year with timezone UTC+01:00 + * * "1 2 3 F,4,L 5 2021 60o 0-2w" for triggering in near first second of second minute of third hour of first, fifth and last days of may of 2021st year if it will be in Sunday-Tuesday week days with timezone UTC+01:00 + * * "1 2 3 F,4,L 5 2021 60o 0-2w 500ms" for triggering in near first second of second minute of third hour of first, fifth and last days of may of 2021st year if it will be in Sunday-Tuesday week days with timezone UTC+01:00 when milliseconds will be equal to 500 * * @return In case when offset parameter is absent in [incoming] will be used [createSimpleScheduler] method and * returned [CronDateTimeScheduler]. In case when offset parameter there is in [incoming] [KrontabTemplate] will be used diff --git a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt index 729a784..a27e330 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt @@ -8,7 +8,7 @@ import dev.inmo.krontab.KronScheduler * @param daysOfWeek 0-6 * @param years any int * @param months 0-11 - * @param daysOfMonth 0-31 + * @param daysOfMonth 0-30 * @param hours 0-23 * @param minutes 0-59 * @param seconds 0-59 diff --git a/src/commonMain/kotlin/dev/inmo/krontab/internal/NearDateTimeCalculator.kt b/src/commonMain/kotlin/dev/inmo/krontab/internal/NearDateTimeCalculator.kt index 714a3f6..a6ada42 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/internal/NearDateTimeCalculator.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/internal/NearDateTimeCalculator.kt @@ -103,12 +103,13 @@ internal fun NearDateTimeCalculatorDays( times, { it.dayOfMonth.toByte() }, { dateTime, newOne -> - (if (newOne < dateTime.dayOfMonth) { + val dateTime = (if (newOne < dateTime.dayOfMonth) { dateTime.plus(1.months) } else { dateTime - }).copy( - dayOfMonth = min(dateTime.month.days(dateTime.year), newOne.toInt() + 1), // index1 + }) + dateTime.copy( + dayOfMonth = min(dateTime.month.days(dateTime.year) - 1, newOne.toInt()), // index1 hour = 0, minute = 0, second = 0, diff --git a/src/commonTest/kotlin/dev/inmo/krontab/utils/InfinityLoopCheckTest.kt b/src/commonTest/kotlin/dev/inmo/krontab/utils/InfinityLoopCheckTest.kt new file mode 100644 index 0000000..8e4cd60 --- /dev/null +++ b/src/commonTest/kotlin/dev/inmo/krontab/utils/InfinityLoopCheckTest.kt @@ -0,0 +1,14 @@ +package dev.inmo.krontab.utils + +import dev.inmo.krontab.buildSchedule +import kotlin.test.Test +import kotlin.test.assertNotNull + +class InfinityLoopCheckTest { + @Test + fun absenceOfInfinityLoopCheckTest() { + runTest { + assertNotNull(buildSchedule("0 0 0 1 *").next()) + } + } +}