[improve] 完善注释和优化单元测试

This commit is contained in:
VampireAchao 2024-02-25 21:29:09 +08:00 committed by VampireAchao
parent 30ea0060a5
commit b9cf5e48c7
3 changed files with 50 additions and 9 deletions

View File

@ -168,6 +168,14 @@ public class DateBetween implements Serializable {
return result;
}
public Date getBegin() {
return begin;
}
public Date getEnd() {
return end;
}
/**
* 格式化输出时间差
*

View File

@ -9,8 +9,22 @@ import java.util.HashMap;
import java.util.Map;
/**
* ChangAnTimeConverter
*
* 长安时辰转换器
* <p>
* 23-1 -> 子时
* 1-3 -> 丑时
* 3-5 -> 寅时
* 5-7 -> 卯时
* 7-9 -> 辰时
* 9-11 -> 巳时
* 11-13 -> 午时
* 13-15 -> 未时
* 15-17 -> 申时
* 17-19 -> 酉时
* 19-21 -> 戌时
* 21-23 -> 亥时
* 24/-1/其他值 -> 未知
* </p>
* @author achao@hutool.cn
*/
public class ChangAnTimeConverter {
@ -33,6 +47,16 @@ public class ChangAnTimeConverter {
timeMap.put("", new int[]{21, 23});
}
/**
* 将长安时辰转换为现代时间
* <p>
* toModernTime("子时").getBegin().getHours() -> 23
* toModernTime("子时").getEnd().getHours() -> 1
* </p>
*
* @param changAnTime 长安时辰
* @return 现代时间段
*/
public static DateBetween toModernTime(String changAnTime) {
String time = changAnTime.replace("", "");
int[] hours = timeMap.get(time);
@ -53,6 +77,14 @@ public class ChangAnTimeConverter {
return DateBetween.of(startDate, endDate);
}
/**
* 将小时转换为长安时辰
* <p>
* toChangAnTime(1) -> "子时"
*</p>
* @param hour 小时
* @return 长安时辰
*/
public static String toChangAnTime(int hour) {
for (Map.Entry<String, int[]> entry : timeMap.entrySet()) {
int startHour = entry.getValue()[0];
@ -61,7 +93,7 @@ public class ChangAnTimeConverter {
return entry.getKey() + "";
}
}
return "未知";
return "未知";
}
}

View File

@ -16,22 +16,23 @@ public class ChangAnTimeConverterTest {
void testToModernTimeForAllTimes() {
// 测试每个时辰的转换
String[] times = {"", "", "", "", "", "", "", "", "", "", "", ""};
int[] expectedHours = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] expectedHours = {{23, 1}, {1, 3}, {3, 5}, {5, 7}, {7, 9}, {9, 11}, {11, 13}, {13, 15}, {15, 17}, {17, 19}, {19, 21}, {21, 23}};
for (int i = 0; i < times.length; i++) {
DateBetween dateBetween = ChangAnTimeConverter.toModernTime(times[i] + "");
long hoursBetween = dateBetween.between(DateUnit.HOUR);
Assertions.assertEquals(expectedHours[i], hoursBetween, times[i] + "时 should last for 2 hours.");
Assertions.assertEquals(2, dateBetween.between(DateUnit.HOUR));
Assertions.assertEquals(expectedHours[i][0], dateBetween.getBegin().getHours());
Assertions.assertEquals(expectedHours[i][1], dateBetween.getEnd().getHours());
}
}
@Test
void testToChangAnTimeForAllHours() {
// 从23时开始测试因为子时开始于23时
String[] expectedTimes = {"子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时", "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时", "子时"};
for (int hour = 0; hour < 24; hour++) {
String[] expectedTimes = {"子时", "丑时", "丑时", "寅时", "寅时", "卯时", "卯时", "辰时", "辰时", "巳时", "巳时", "午时", "午时", "未时", "未时", "申时", "申时", "酉时", "酉时", "戌时", "戌时", "亥时", "亥时", "子时", "未知"};
for (int hour = 0; hour <= 24; hour++) {
String expectedTime = expectedTimes[hour];
String actualTime = ChangAnTimeConverter.toChangAnTime(hour);
Assertions.assertEquals(expectedTime, actualTime, "Hour " + hour + " should be in " + expectedTime);
Assertions.assertEquals(expectedTime, actualTime);
}
}