我正在尝试将时间结构转换为 FAT 时间戳。我的代码如下所示:
unsigned long Fat(tm_struct pTime)
{
unsigned long FatTime = 0;
FatTime |= (pTime.seconds / 2) >> 1;
FatTime |= (pTime.minutes) << 5;
FatTime |= (pTime.hours) << 11;
FatTime |= (pTime.days) << 16;
FatTime |= (pTime.months) << 21;
FatTime |= (pTime.years + 20) << 25;
return FatTime;
}
有人有正确的代码吗?
The DOS date/time format is a bitmask:
24 16 8 0
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
|Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s|
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
\___________/\________/\_________/ \________/\____________/\_________/
year month day hour minute second
The year is stored as an offset from 1980.
Seconds are stored in two-second increments.
(So if the "second" value is 15, it actually represents 30 seconds.)
我不知道你正在使用的 tm_struct 但如果它是http://www.cplusplus.com/reference/ctime/tm/ http://www.cplusplus.com/reference/ctime/tm/ then
unsigned long FatTime = ((pTime.tm_year - 80) << 25) |
((pTime.tm_mon + 1) << 21) |
(pTime.tm_mday << 16) |
(pTime.tm_hour << 11) |
(pTime.tm_min << 5) |
(pTime.tm_sec >> 1);
Edit:正如评论中提到的,我在月份添加了 +1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)