diff --git a/gradle.properties b/gradle.properties index 798df0e..305ce56 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ MORNY_ARCHIVE_NAME = morny-coeur MORNY_CODE_STORE = https://github.com/Eyre-S/Coeur-Morny-Cono MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s -VERSION = 1.0.0-RC1.1 +VERSION = 1.0.0-RC2 USE_DELTA = false VERSION_DELTA = diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java index 5faba4a..0c95a39 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -9,14 +9,19 @@ import com.pengrad.telegrambot.request.EditMessageText; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.response.SendResponse; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static cc.sukazyo.cono.morny.Log.exceptionLog; import static cc.sukazyo.cono.morny.Log.logger; public class MedicationTimer extends Thread { + public static final ZoneOffset USE_TIME_ZONE = ZoneOffset.ofHours(8); + public static final Set NOTIFY_AT_HOUR = Set.of(12); public static final long NOTIFY_CHAT = -1001729016815L; public static final String NOTIFY_MESSAGE = "\uD83C\uDF65⏲"; private static final String DAEMON_THREAD_NAME = "TIMER_Medication"; @@ -69,12 +74,19 @@ public class MedicationTimer extends Thread { lastNotify = LAST_NOTIFY_ID_NULL; } - private static long calcNextRoutineTimestamp () { - return ((System.currentTimeMillis()+8*60*60*1000) / (12*60*60*1000) + 1) * 12*60*60*1000 - 8*60*60*1000; + public static long calcNextRoutineTimestamp (long baseTimeMillis, ZoneOffset useTimeZone, Set atHours) { + LocalDateTime time = LocalDateTime.ofEpochSecond( + baseTimeMillis/1000, (int)baseTimeMillis%1000*1000*1000, + useTimeZone + ).withMinute(0).withSecond(0).withNano(0); + do { + time = time.plusHours(1); + } while (!atHours.contains(time.getHour())); + return time.withMinute(0).withSecond(0).withNano(0).toInstant(useTimeZone).toEpochMilli(); } private void waitToNextRoutine () throws InterruptedException { - sleep(calcNextRoutineTimestamp() - System.currentTimeMillis()); + sleep(calcNextRoutineTimestamp(System.currentTimeMillis(), USE_TIME_ZONE, NOTIFY_AT_HOUR) - System.currentTimeMillis()); } } diff --git a/src/test/java/cc/sukazyo/cono/morny/daemon/TestMedicationTimer.java b/src/test/java/cc/sukazyo/cono/morny/daemon/TestMedicationTimer.java new file mode 100644 index 0000000..cb74065 --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/daemon/TestMedicationTimer.java @@ -0,0 +1,29 @@ +package cc.sukazyo.cono.morny.daemon; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Set; + +public class TestMedicationTimer { + + @ParameterizedTest + @CsvSource(textBlock = """ + 2022-11-13T13:14:35+08, +08, 2022-11-14T12:00:00+08 + 2022-11-13T13:14:35+02, +02, 2022-11-14T12:00:00+02 + 2022-11-13T08:14:35+08, +08, 2022-11-13T12:00:00+08 + 2022-11-13T00:14:35+08, +08, 2022-11-13T12:00:00+08 + 2022-11-13T12:00:00+00, +00, 2022-11-14T12:00:00+00 + """) + void testCalcNextRoutineTimestamp (ZonedDateTime base, ZoneOffset zoneHour, ZonedDateTime expected) { + final Set at = Set.of(12); + Assertions.assertEquals( + expected.toEpochSecond()*1000, + MedicationTimer.calcNextRoutineTimestamp(base.toEpochSecond()*1000, zoneHour, at) + ); + } + +}