通過unix時間戳獲得兩個時間相差多少自然日
阿新 • • 發佈:2019-01-04
由兩個unix時間戳得到之間相差自然日數
這兩個函式模組是我在參加一個新專案的編寫時偶然發現並且特別依賴的部分,為實現當時手上累計登陸完成任務有很大的幫助。該模組用C++編寫,在linux環境上編譯測試通過
#include <time.h>
int GetTimeZone()
{
time_t tm1, tm2;
tm1 = time( NULL );
struct tm stTm;
tm2 = mktime( gmtime_r( &tm1, &stTm ) );
return static_cast<int>( tm1 - tm2 );
}
int GetDiffDay( unsigned int nTime1, unsigned int nTime2 )
{
static int nDaySec = 3600 * 24;
static int nZone = GetTimeZone();
int nTempTimeSec = 0;
nTempTimeSec = (nZone >= 0)?nZone:(nZone + nDaySec);
int nDay1 = ( nTime1 + nTempTimeSec ) / nDaySec;
int nDay2 = ( nTime2 + nTempTimeSec ) / nDaySec;
return nDay2 - nDay1;
}
下次有空把整個專案用到的時間處理的模組整理一下