文言时间使用古代时辰格式 (#4413)

This commit is contained in:
Glavo 2025-09-09 20:23:13 +08:00 committed by GitHub
parent 41e004f4d5
commit 8db8ade9c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

View File

@ -74,6 +74,11 @@ public final class WenyanUtils {
builder.append(DI_ZHI[mod(yearOffset, DI_ZHI.length)]); builder.append(DI_ZHI[mod(yearOffset, DI_ZHI.length)]);
} }
static void appendHour(StringBuilder builder, int hour) {
builder.append(DI_ZHI[((hour + 1) % 24) / 2]);
builder.append(hour % 2 == 0 ? '正' : '初');
}
public static String formatDateTime(TemporalAccessor time) { public static String formatDateTime(TemporalAccessor time) {
LocalDateTime localDateTime; LocalDateTime localDateTime;
if (time instanceof Instant) if (time instanceof Instant)
@ -92,8 +97,7 @@ public final class WenyanUtils {
builder.append(' '); builder.append(' ');
builder.append(numberToString(localDateTime.getHour())); appendHour(builder, localDateTime.getHour());
builder.append('时');
builder.append(numberToString(localDateTime.getMinute())); builder.append(numberToString(localDateTime.getMinute()));
builder.append('分'); builder.append('分');
builder.append(numberToString(localDateTime.getSecond())); builder.append(numberToString(localDateTime.getSecond()));

View File

@ -19,6 +19,8 @@ package org.jackhuang.hmcl.util.i18n;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
@ -39,4 +41,18 @@ public final class WenyanUtilsTest {
assertYearToString("甲子", -2996); assertYearToString("甲子", -2996);
assertYearToString("庚子", 1000); assertYearToString("庚子", 1000);
} }
@Test
public void testHourToString() {
List<String> list = List.of(
"子正", "丑初", "丑正", "寅初", "寅正", "卯初", "卯正", "辰初", "辰正", "巳初", "巳正", "午初",
"午正", "未初", "未正", "申初", "申正", "酉初", "酉正", "戌初", "戌正", "亥初", "亥正", "子初"
);
for (int hour = 0; hour < list.size(); hour++) {
StringBuilder builder = new StringBuilder(2);
WenyanUtils.appendHour(builder, hour);
assertEquals(list.get(hour), builder.toString());
}
}
} }