From 2c7c4a2a6bb4853375055a770755d119c00f02d4 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 13 Nov 2022 13:22:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4=E5=90=83=E8=8D=AF=E6=8F=90?= =?UTF-8?q?=E9=86=92=E4=B8=BA=2012:00=20=E4=B8=80=E6=AC=A1=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=B8=BA=E5=90=83=E8=8D=AF=E6=8F=90=E9=86=92=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E8=AE=A1=E7=AE=97=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将吃药提醒的时间计算变更为基于 LocalDateTime 的计算 - 为其添加了 USE_TIME_ZONE 和 NOTIFY_AT_HOUR 两个参数 --- gradle.properties | 2 +- .../cono/morny/daemon/MedicationTimer.java | 18 ++++++++++-- .../morny/daemon/TestMedicationTimer.java | 29 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/test/java/cc/sukazyo/cono/morny/daemon/TestMedicationTimer.java 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) + ); + } + +}