64位浮点日期和时间值为DateTime c#
64-bit floating point date and time Value to DateTime C#
我有一个64位的浮点值,我需要转换为DateTime。我所得到的是一个C/Cpp代码块,但我不能理解它,所以我可以在c#中做同样的事情,应该感谢帮助。
我确实有这条信息:
这是C代码:日期和时间是一个8字节的64位浮点值,表示自1900年1月1日起的天数。一天中的时间表示为一天的一小部分。
//Time is first 8 bytes, converted to an 8-byte float, in units of days
m1 = (unsigned int)(((((((((unsigned int)line[1]) & 0xFF)<<8) | ((unsigned int)line[2])&0xFF) << 8) | ((unsigned int)line[3])&0xFF) << 8) | ((unsigned int)line[4])&0xFF);
m2 = (unsigned int)(((((((((unsigned int)line[5]) & 0xFF)<<8) | ((unsigned int)line[6])&0xFF) << 8) | ((unsigned int)line[7])&0xFF) << 8) | ((unsigned int)line[8])&0xFF);
//Mask off mantissa bits and add back in the "hidden bit"
time = (double)((m1 & 0x000FFFFF) | 0x00100000) + ((double)m2)/thirty_two_bits;
time = time / 32.0; //Normalise by "shifting right" to complete mantissa extraction
exp = (m1 >> 20) - 0x0400 - 14; //The above calculation is good for an exponent of 14...
while (exp != 0){
if (exp < 0){
time = time / 2.0;
exp++;
}else{
time = time * 2.0;
exp--;
}
}
time = time * 3600.0 * 24.0; //Convert days to seconds
其中line
是一个字节数组,包含从索引[1]开始的日期和时间的8个字节。
line
的示例值为:{X, 64, 228, 8, 46, 222, 232, 221, 125, ...}
。
我确实尝试了以下操作,但没有正确的结果:(c#):
long longvalue = BitConverter.ToInt64(line, 1);
DateTime dtm = DateTime.FromBinary(longvalue);
汉斯·帕桑特在评论中回答:
使用MemoryStream来存储字节,
BinaryReader.GetBytes()
到从中读取8个字节,Array.Reverse()
反转字节,BitConverter.ToDouble()
将byte[]
转换为double
;DateTime.FromOADate()
转换为日期。您的示例字节生产{4/26/2012 11:09:11 AM},看起来像一个快乐的约会。
感谢汉斯·帕桑。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 在已经使用Git的情况下减少编译时间
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 从文本文件中读取时钟时间和事件时间并进行处理
- 具有未知值时的时间复杂性
- 如何减少花费的时间
- C++在变量给定的指定时间内关闭电脑
- 将DateTime字符串分为C 的日期时间
- 将datetime c++结构(以字节形式接收)转换为c#日期时间实例
- C++ Boost将UNIX时间戳转换为MySQL兼容的DATETIME字符串
- 从系统中拆分日期和时间::DateTime
- c++ unix时间从Mysql DateTime字符串
- c++时间戳到人类可读的datetime函数
- 64位浮点日期和时间值为DateTime c#