[英]Conversion from SYSTEMTIME to time_t gives out Time in UTC/GMT
我正在嘗試通過各種論壇中的實現將SYSTEMTIME
轉換為time_t
。
time_t TimeFromSystemTime(const SYSTEMTIME * pTime)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_year = pTime->wYear - 1900; // EDIT 2 : 1900's Offset as per comment
tm.tm_mon = pTime->wMonth - 1;
tm.tm_mday = pTime->wDay;
tm.tm_hour = pTime->wHour;
tm.tm_min = pTime->wMinute;
tm.tm_sec = pTime->wSecond;
tm.tm_isdst = -1; // Edit 2: Added as per comment
return mktime(&tm);
}
但是令我驚訝的是, tm
承載的數據對應於本地時間,但是mktime()
返回的time_t
對應於UTC時間。
這是它的工作方式還是我在這里錯過了任何東西嗎?
我在這里先向您的幫助表示感謝 !!
編輯1:我想轉換SYSTEMTIME
准確地將我的本地時間作為time_t
。
我在基於VC6的MFC應用程序中使用它。
編輯2:修改的代碼。
我終於通過TIME_ZONE_INFORMATION和_timezone從Windows SDK找到了解決方案
time_t GetLocaleDateTime( time_t ttdateTime) // The time_t from the mktime() is fed here as the Parameter
{
if(ttdateTime <= 0)
return 0;
TIME_ZONE_INFORMATION tzi;
GetTimeZoneInformation(&tzi); // We can also use the StandardBias of the TIME_ZONE_INFORMATION
int iTz = -_timezone; // Current Timezone Offset from UTC in Seconds
iTz = (iTz > 12*3600) ? (iTz - 24*3600) : iTz; // 14 ==> -10
iTz = (iTz < -11*3600) ? (iTz + 24*3600) : iTz; // -14 ==> 10
ttdateTime += iTz;
return ttdateTime;
}
編輯1 :請添加您的評論,如果您發現任何錯誤,請隨時評論或編輯。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.