优化CP56Time2a

This commit is contained in:
三丙
2025-03-14 03:24:59 +08:00
parent 780e9e630d
commit cacb98768f
4 changed files with 90 additions and 49 deletions

View File

@@ -6,62 +6,97 @@
*/
package sanbing.jcpp.infrastructure.util.codec;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class CP56Time2aUtil {
/**
* 解码 CP56Time2a 字节数组为 Instant 对象
* 高性能解码 CP56Time2a 字节数组为本地时间对象
*
* @param bytes 字节数组
* @return Instant 对象
* @param bytes 7字节的CP56Time2a数组
* @return 解码后的本地时间(系统默认时区)
* @throws IllegalArgumentException 当输入不符合规范时抛出异常
*/
public static Instant decode(byte[] bytes) {
// 将字节数组解释为各个时间部分
int milliseconds = ((bytes[0] & 0xFF) + ((bytes[1] & 0xFF) << 8)); // 处理字节的无符号值
int minutes = bytes[2] & 0x3F;
int hours = bytes[3] & 0x1F;
int days = bytes[4] & 0x1F;
int months = bytes[5] & 0x0F;
int years = bytes[6] & 0x7F;
public static LocalDateTime decode(byte[] bytes) {
if (bytes.length != 7) {
throw new IllegalArgumentException("Invalid CP56Time2a format: 需要7字节");
}
// 将 CP56Time2a 转换为 LocalDateTime
LocalDateTime dateTime = LocalDateTime.of(
years + 2000,
months,
days,
hours,
minutes,
milliseconds / 1000 // 秒数
);
// 预处理字节为 Unsigned Int避免重复转换
final int b6 = bytes[6] & 0xFF;
final int b5 = bytes[5] & 0xFF;
final int b4 = bytes[4] & 0xFF;
final int b3 = bytes[3] & 0xFF;
final int b2 = bytes[2] & 0xFF;
final int b1 = bytes[1] & 0xFF;
final int b0 = bytes[0] & 0xFF;
// 返回对应的 Instant 对象
return dateTime.atZone(ZoneId.systemDefault()).toInstant();
// 年份2000~2127: 7位无符号
final int year = 2000 + (b6 & 0x7F);
// 月份1~12: 低4位
final int month = b5 & 0x0F;
if (month < 1 || month > 12) { // 内联校验
throw new IllegalArgumentException("非法月份值:" + month);
}
// 日期1~31: 低5位
final int day = b4 & 0x1F;
if (day < 1) { // 内联校验
throw new IllegalArgumentException("非法日期值:" + day);
}
// 小时0~23: 低5位
final int hour = b3 & 0x1F;
if (hour > 23) { // 内联校验
throw new IllegalArgumentException("非法小时值:" + hour);
}
// 分钟0~59: 低6位
final int minute = b2 & 0x3F;
if (minute > 59) { // 内联校验
throw new IllegalArgumentException("非法分钟值:" + minute);
}
// 合并秒和毫秒(小端序优化)
final int combined = (b1 << 8) | b0;
final int second = combined / 1000;
if (second > 59) { // 内联校验
throw new IllegalArgumentException("非法秒数值:" + second);
}
return LocalDateTime.of(year, month, day, hour, minute, second, (combined % 1000) * 1_000_000);
}
/**
* 编码 Instant 对象为 CP56Time2a 字节数组
* 高性能编码本地时间对象为 CP56Time2a 字节数组
*
* @param instant Instant 对象
* @return 字节数组
* @param dateTime 本地时间对象(需保证为系统默认时区)
* @return 7字节的CP56Time2a数组
*/
public static byte[] encode(Instant instant) {
// 将 Instant 转换到 LocalDateTime
LocalDateTime aTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
public static byte[] encode(LocalDateTime dateTime) {
final byte[] cp56Time2a = new byte[7];
byte[] result = new byte[7];
int milliseconds = aTime.getSecond() * 1000; // 获取毫秒部分
// 年份2000~2127
final int year = dateTime.getYear();
cp56Time2a[6] = (byte) ((year - 2000) & 0x7F); // 7位掩码优化
// 填充字节数组
result[0] = (byte) (milliseconds % 256);
result[1] = (byte) (milliseconds / 256);
result[2] = (byte) aTime.getMinute();
result[3] = (byte) aTime.getHour();
result[4] = (byte) aTime.getDayOfMonth();
result[5] = (byte) aTime.getMonthValue(); // 1-12
result[6] = (byte) (aTime.getYear() % 100); // 00-99
// 月份1~12
final int month = dateTime.getMonthValue();
cp56Time2a[5] = (byte) month; // 直接赋值协议层保证低4位有效
return result;
// 日期1~31
cp56Time2a[4] = (byte) dateTime.getDayOfMonth(); // 协议层保证低5位有效
// 时间字段(直接赋值,协议层保证掩码)
cp56Time2a[3] = (byte) dateTime.getHour();
cp56Time2a[2] = (byte) dateTime.getMinute();
// 合并秒和毫秒(避免中间变量)
final int nano = dateTime.getNano();
final int combined = dateTime.getSecond() * 1000 + (nano / 1_000_000);
cp56Time2a[1] = (byte) (combined >>> 8); // 小端序高字节
cp56Time2a[0] = (byte) combined; // 小端序低字节
return cp56Time2a;
}
}

View File

@@ -6,22 +6,27 @@
*/
package sanbing.jcpp.infrastructure.util.codec;
import cn.hutool.core.util.HexUtil;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
class CP56Time2aUtilTest {
@Test
void encodeTest() {
Instant time = Instant.ofEpochMilli(1727798453000L);
LocalDateTime time = LocalDateTime.of(2025, 1, 22, 14, 30, 45, 123_000_000);
byte[] bytes = CP56Time2aUtil.encode(time);
System.out.println(Arrays.toString(bytes));
System.out.println(HexUtil.encodeHex(bytes));
System.out.println(time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
Instant decode = CP56Time2aUtil.decode(bytes);
LocalDateTime decode = CP56Time2aUtil.decode(bytes);
System.out.println(decode.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
assert time.equals(decode);
}