windows时间操作的技巧

windows时间操作的技巧

// 取得距离到下周X, H点M分剩余的毫秒,用来启动定时器

// 注意传进来的dwTime格式1245(12:45) 305(3:05) 5(00:05) 59(00:59)
DWORD GetNextPeriod(int nDate, DWORD dwTime)
{
time_t now;
time(&now);
tm now_tm = *localtime(&now);
// 精确到分
int nNowMin = now_tm.tm_wday * 24 * 60 + 60 * (now_tm.tm_hour) + now_tm.tm_min;

int nFutureMin = nDate * 24 * 60 + 60 * (dwTime / 100) + (dwTime % 100);

if (nNowMin <= nFutureMin)
{
   return (nFutureMin - nNowMin) * 60 * 1000;
}
return (nFutureMin - nNowMin + 7 * 24 * 60) * 60 * 1000;
}

其他:

time_t now;
time(&now);
tm now_tm = *localtime(&now);

time_t tomorrow = now + (7 - now_tm.tm_wday) * 24 * 3600;
tm tomorrow_tm = *localtime(&tomorrow);
tomorrow_tm.tm_hour = 0;
tomorrow_tm.tm_min = 0;
tomorrow_tm.tm_sec = 0;

time_t OnTimer_t = mktime(&tomorrow_tm);

DWORD dwElapse = (OnTimer_t - now) * 1000;


你可能感兴趣的:(windows时间操作的技巧)