Skip to content

MTDriver_CheckTime得出来的时间戳不准确 #1

Description

@yj525300

在XP和win7下测试,得出来的时间戳都不准确,比正确时间戳晚了很多。以下为修正后代码:
`#include <ntddk.h>

const unsigned char g_day_per_mon[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/*

  • 功能:
  • 判断是否是闰年
    
  • 参数:
  • year:需要判断的年份数
    
  • 返回值:
  • 闰年返回1,否则返回0
    

*/
unsigned char applib_dt_is_leap_year(unsigned short year)
{
if ((year % 400) == 0) {
return 1;
} else if ((year % 100) == 0) {
return 0;
} else if ((year % 4) == 0) {
return 1;
} else {
return 0;
}
}

/*

  • 功能:
  • 得到每个月有多少天
    
  • 参数:
  • month:需要得到天数的月份数
    
  • year:该月所对应的年份数
    
  • 返回值:
  • 该月有多少天
    

*/
unsigned char applib_dt_last_day_of_mon(unsigned char month, unsigned short year)
{
if ((month == 0) || (month > 12)) {
return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}

if (month != 2) {
    return g_day_per_mon[month - 1];
} else {
    return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}

}
// 日期转时间戳
int TimeStamp()
{
LARGE_INTEGER snow, now, tickcount;
TIME_FIELDS now_fields;
unsigned short i = 0;
unsigned int no_of_days = 0;
int utc_time = 0;
UINT16 iYear,iMonth,iDay,iHour,iMin,iSec;
// 获取标准时间
KeQuerySystemTime(&snow);

// 转换为当地时间
ExSystemTimeToLocalTime(&snow, &now);

// 整理出年、月、日、时、分、秒
RtlTimeToTimeFields(&now, &now_fields);

// 打印年月日
DbgPrint("NowTime : %4d-%2d-%2d %2d:%2d:%2d\n", 
	now_fields.Year, now_fields.Month, now_fields.Day, 
	now_fields.Hour, now_fields.Minute, now_fields.Second);


iYear=now_fields.Year;
iMonth=now_fields.Month;
iDay=now_fields.Day;
iHour=now_fields.Hour;
iMin=now_fields.Minute;
iSec=now_fields.Second;

/* year */
for (i = 1970; i < iYear; i++) {
    no_of_days += (365 + applib_dt_is_leap_year(i));
}

/* month */
for (i = 1; i < iMonth; i++) {
    no_of_days += applib_dt_last_day_of_mon((unsigned char) i, iYear);
}

/* day */
no_of_days += (iDay - 1);

/* sec */
utc_time = (unsigned int) no_of_days * 86400 + (unsigned int) (iHour * 3600 +iMin * 60 + iSec);

utc_time -= 3600*8;

return utc_time;

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions