I am trying to convert SYSTEMTIME
to time_t
through the implementation I found in various forums.
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);
}
But to my surprise, the tm
is carrying the data corresponds to the local time but the mktime()
returns the time_t
corresponds to the time in UTC.
Is this the way it works or am I missing anything here?
Thanks for the help in advance !!
EDIT 1: I want to convert the SYSTEMTIME
which carries my Local time as the time_t
exactly.
I am using this in the VC6 based MFC application.
EDIT 2: Modified Code.
I finally found the Solution from the Windows SDK, through TIME_ZONE_INFORMATION and _timezone
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;
}
EDIT 1 : Please do add your comments, and also if you see any bugs feel free to comment or edit. Thanks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.