变更吃药提醒为 12:00 一次,修改了吃药提醒的时间计算方式,为吃药提醒的时间计算添加了一个测试

- 将吃药提醒的时间计算变更为基于 LocalDateTime 的计算
  - 为其添加了 USE_TIME_ZONE 和 NOTIFY_AT_HOUR 两个参数
This commit is contained in:
A.C.Sukazyo Eyre 2022-11-13 13:22:17 +08:00
parent a83930efd0
commit 2c7c4a2a6b
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
3 changed files with 45 additions and 4 deletions

View File

@ -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 =

View File

@ -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<Integer> 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<Integer> 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());
}
}

View File

@ -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<Integer> at = Set.of(12);
Assertions.assertEquals(
expected.toEpochSecond()*1000,
MedicationTimer.calcNextRoutineTimestamp(base.toEpochSecond()*1000, zoneHour, at)
);
}
}