|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace HybirdFrameworkServices.Charger.BatCharging.Service
|
|
|
|
|
{
|
|
|
|
|
public static class BcdTimeConverter
|
|
|
|
|
{
|
|
|
|
|
public static long ConvertToBcdTime(DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
// 将DateTime的各个部分分离出来
|
|
|
|
|
int year = dateTime.Year;
|
|
|
|
|
int month = dateTime.Month;
|
|
|
|
|
int day = dateTime.Day;
|
|
|
|
|
int hour = dateTime.Hour;
|
|
|
|
|
int minute = dateTime.Minute;
|
|
|
|
|
int second = dateTime.Second;
|
|
|
|
|
int millisecond = dateTime.Millisecond;
|
|
|
|
|
|
|
|
|
|
// BCD编码转换函数
|
|
|
|
|
byte BcdEncode(int value)
|
|
|
|
|
{
|
|
|
|
|
return (byte)((value / 10 * 16) + (value % 10));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构造BCD时间
|
|
|
|
|
// 注意:这里假设毫秒只取前两位(十位和个位),忽略剩余的毫秒数
|
|
|
|
|
byte secondTens = BcdEncode(second / 10);
|
|
|
|
|
byte secondUnits = BcdEncode(second % 10);
|
|
|
|
|
byte minuteTens = BcdEncode(minute / 10);
|
|
|
|
|
byte minuteUnits = BcdEncode(minute % 10);
|
|
|
|
|
byte hourTens = BcdEncode(hour / 10);
|
|
|
|
|
byte hourUnits = BcdEncode(hour % 10);
|
|
|
|
|
byte dayTens = BcdEncode(day / 10);
|
|
|
|
|
byte dayUnits = BcdEncode(day % 10);
|
|
|
|
|
byte monthTens = BcdEncode(month / 10);
|
|
|
|
|
byte monthUnits = BcdEncode(month % 10);
|
|
|
|
|
byte yearTens = BcdEncode(year / 10);
|
|
|
|
|
byte yearUnits = BcdEncode(year % 10);
|
|
|
|
|
|
|
|
|
|
// 组合成一个8字节的数
|
|
|
|
|
long bcdTime = (long)secondTens << 56 | (long)secondUnits << 52 |
|
|
|
|
|
(long)minuteTens << 48 | (long)minuteUnits << 44 |
|
|
|
|
|
(long)hourTens << 40 | (long)hourUnits << 36 |
|
|
|
|
|
(long)dayTens << 32 | (long)dayUnits << 28 |
|
|
|
|
|
(long)monthTens << 24 | (long)monthUnits << 20 |
|
|
|
|
|
(long)yearTens << 16 | (long)yearUnits;
|
|
|
|
|
|
|
|
|
|
return bcdTime;
|
|
|
|
|
}
|
|
|
|
|
public static byte[] ConvertToBcdTimeByte(DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
// 将DateTime的各个部分分离出来
|
|
|
|
|
int year = dateTime.Year;
|
|
|
|
|
int month = dateTime.Month;
|
|
|
|
|
int day = dateTime.Day;
|
|
|
|
|
int hour = dateTime.Hour;
|
|
|
|
|
int minute = dateTime.Minute;
|
|
|
|
|
int second = dateTime.Second;
|
|
|
|
|
int millisecond = dateTime.Millisecond;
|
|
|
|
|
|
|
|
|
|
byte[] bytes= new byte[8];
|
|
|
|
|
bytes[0]= (byte)year;
|
|
|
|
|
bytes[1]= (byte)month;
|
|
|
|
|
bytes[2]= (byte)day;
|
|
|
|
|
bytes[3]= (byte)hour;
|
|
|
|
|
bytes[4]= (byte)minute;
|
|
|
|
|
bytes[5]= (byte)second;
|
|
|
|
|
bytes[6]= (byte)0;
|
|
|
|
|
bytes[7]= (byte)0;
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|