净日期时间到DOS日期32位转换日期、时间、DOS

2023-09-05 04:30:52 作者:攻略无数成为神。

我需要从一个32位DOS日期为.NET System.DateTime的,然后再返回转换。我使用的两个例程的下方,然而,当我将它们转换来回它们是由若干秒。任何人都可以看到,为什么?

 公共静态的DateTime ToDateTime(这个中断dosDateTime)
{
    VAR日期=(dosDateTime&安培; 0xFFFF0000地址)GT;> 16;
    VAR的时间=(dosDateTime&安培; 0x0000FFFF);

    VAR年=(日期>> 9)+ 1980;
    VAR月=(日期和放大器; 0x01e0)>> 5;
    VAR天=日期和放大器; 0x1F的;
    VAR小时=时间>> 11;
    VAR分钟=(时间放; 0x07e0)>> 5;
    VAR秒=(时间放;为0x1F)* 2;

    返回新的日期时间((INT)年,(INT)个月,(INT)日,(INT)小时,(INT)分,(INT)二);
}

公共静态INT ToDOSDate(这个日期时间日期时间)
{
    VAR年= dateTime.Year  -  1980年;
    VAR个月= dateTime.Month;
    VAR天= dateTime.Day;
    VAR小时= dateTime.Hour;
    VAR分钟= dateTime.Minute;
    VAR秒= dateTime.Second;

    VAR日期=(年<< 9)| (月&其中;小于5)|天;
    VAR的时间=(小时<< 11)| (分钟&其中;小于5)| (秒-1;&小于1);

    返回(日期<< 16)|时间;
}
 

解决方案

ToDOSDate ,需要的秒数由两个被存储在时间变量。 (秒-1;< 1)左移,其中乘法两个。更改为正确的按位移位((秒>> 1))由两个分

开机DOS显示时间怎样调整

请注意,有没有办法避免的 ToDOSDate 失去第二次时,有几秒钟的日期时间。右位移位来划分由两个会一直下降的最低显著位。

I need to convert from a 32 bit Dos Date to a .NET System.DateTime and back again. I'm using the two routines below, however when I convert them back and forth they are out by a number of seconds. Can anyone see why?

public static DateTime ToDateTime(this int dosDateTime)
{
    var date = (dosDateTime & 0xFFFF0000) >> 16;
    var time = (dosDateTime & 0x0000FFFF);

    var year = (date >> 9) + 1980;
    var month = (date & 0x01e0) >> 5;
    var day =  date & 0x1F;
    var hour = time >> 11;
    var minute = (time & 0x07e0) >> 5;
    var second = (time & 0x1F) * 2;

    return new DateTime((int)year, (int)month, (int)day, (int)hour, (int)minute, (int)second);
}

public static int ToDOSDate(this DateTime dateTime)
{
    var years = dateTime.Year - 1980;
    var months = dateTime.Month;
    var days = dateTime.Day;
    var hours = dateTime.Hour;
    var minutes = dateTime.Minute;
    var seconds = dateTime.Second;

    var date = (years << 9) | (months << 5) | days;
    var time = (hours << 11) | (minutes << 5) | (seconds << 1);

    return (date << 16) | time;
}

解决方案

In ToDOSDate, the number of seconds needs to be divided by two before being stored in the time variable. (seconds << 1) left shifts, which multiplies seconds by two. Change that to a right bitwise shift ((seconds >> 1)) to divide by two.

Note that there's no way to avoid losing a second in ToDOSDate when there are an odd number of seconds in dateTime. The right bit shift to divide seconds by two will always drop the least significant bit.