兩個SYSTEMTIME比較差異(秒數、天數……)的方法

來源:互聯網
上載者:User

原貼地址:

http://topic.csdn.net/t/20040707/08/3151675.html

 

我稍微加工了一下:

 

 

====================Begin====================

 

time_t FileTimeToTime_t(const FILETIME &ft) { ULARGE_INTEGER ui; ui.LowPart = ft.dwLowDateTime; ui.HighPart = ft.dwHighDateTime; return ((LONGLONG)(ui.QuadPart - 116444736000000000) / 10000000); } int GetDiffSeconds(const SYSTEMTIME &t1, const SYSTEMTIME &t2) { FILETIME fTime1 = { 0, 0 }; FILETIME fTime2 = { 0, 0 }; SystemTimeToFileTime(&t1, &fTime1); SystemTimeToFileTime(&t2, &fTime2); time_t tt1 = FileTimeToTime_t(fTime1); time_t tt2 = FileTimeToTime_t(fTime2); return (int)(tt2 - tt1); } int GetDiffDays(const SYSTEMTIME &t1, const SYSTEMTIME &t2) { int diffSeconds = GetDiffSeconds(t1, t2); return diffSeconds / (24 * 3600); }

====================End====================

 

 

// 我順便補充一個:反推SYSTEMTIME隔N天之後的SYSTEMTIME

 


void Time_tToFileTime(FILETIME &ft, time_t time) { ULARGE_INTEGER ui; ui.QuadPart = (LONGLONG)(time * 10000000) + (LONGLONG)(116444736000000000); ft.dwLowDateTime = ui.LowPart; ft.dwHighDateTime = ui.HighPart; } void GetOffsetDaysTime(const SYSTEMTIME &stSrc, int offDays, SYSTEMTIME &stDst) { FILETIME fTimeSrc = { 0, 0 }; SystemTimeToFileTime(&stSrc, &fTimeSrc); time_t tmSrc = FileTimeToTime_t(fTimeSrc); time_t tmDst = tmSrc + SEC_OF_DAY * offDays; FILETIME fTimeDst = { 0, 0 }; Time_tToFileTime(fTimeDst, tmDst); FileTimeToSystemTime(&fTimeDst, &stDst); }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.