.NET日期时间,转换和OADate当不同的分辨率?分辨率、不同、日期、时间

2023-09-02 10:51:27 作者:痴情浪人

我转换日期时间为OADate。我期待转换OADate回来时,得到完全相同的日期时间,但现在它只有毫秒级的分辨率,因此是不同的。

I'm converting a DateTime to OADate. I was expecting to get the exact same DateTime when converting the OADate back, but now it has only millisecond resolution, and is therefore different.

var a = DateTime.UtcNow;
double oadate = a.ToOADate();
var b = DateTime.FromOADate(oadate);
int compare = DateTime.Compare(a, b); 

//Compare is not 0; the date times are not the same

滴答:634202170964319073

Ticks from a: 634202170964319073

从B蜱:634202170964310000

Ticks from b: 634202170964310000

在OADate双:40437.290467951389

The OADate double: 40437.290467951389

,这是什么原因呢?日期时间的分辨率显然是不够好。

What is the reason for this? The resolution of DateTime is clearly good enough.

推荐答案

在调用ToOADate静态方法明确划分蜱10000,然后将结果存储在一个长期的,从而消除任何子毫秒资讯

The static method called by ToOADate clearly divides the ticks by 10000 and then stores the result in a long, thus removing any sub millisecond info

有谁知道在哪里可以找到OADate格式的规格?

Does anyone know where to find the specs of the OADate format?

    private static double TicksToOADate(long value)
    {
        if (value == 0L)
        {
            return 0.0;
        }
        if (value < 0xc92a69c000L)
        {
            value += 0x85103c0cb83c000L;
        }
        if (value < 0x6efdddaec64000L)
        {
            throw new OverflowException(Environment.GetResourceString("Arg_OleAutDateInvalid"));
        }
        long num = (value - 0x85103c0cb83c000L) / 0x2710L;
        if (num < 0L)
        {
            long num2 = num % 0x5265c00L;
            if (num2 != 0L)
            {
                num -= (0x5265c00L + num2) * 2L;
            }
        }
        return (((double)num) / 86400000.0);
    }
 
精彩推荐
图片推荐