3

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.

Sathish Guru V
  • 1,417
  • 2
  • 15
  • 39
  • 1
    Yes, the manual says that mktime() converts from local time to UTC. As required by time_t, it stores the number of seconds since 1/1/1970, 12am UTC. Feature, not a bug. – Hans Passant Feb 24 '15 at 13:54
  • use _mkgmtime() which just does a conversion between the two without taking timezones into acount. – Paul Ogilvie Feb 24 '15 at 14:00
  • Your explanation is somewhat confusing: what does the SYSTEMTIME contain? local time or UTC time ? – chqrlie Feb 24 '15 at 14:04
  • SYSTEMTIME is always local time. – Hans Passant Feb 24 '15 at 14:14
  • Then `mktime` does what you want with 2 caveats: the C library's notion of the time zone must be in sync with that of the system, and the `tm_isdst` flag in the `tm` structure must be set properly. Check for a similar field in the `SYSTEMTIME` structure. – chqrlie Feb 24 '15 at 14:26
  • Also check if the `SYSTEMTIME` structure's `wYear` field is the number of years since 1900 as expected for the `tm_year` field. – chqrlie Feb 24 '15 at 14:27
  • 1
    @hans-passant, it is not stated how the SYSTEMTIME his function receives is obtained. As it is a stuct, it could have ben populated with any time value and so not necesarilly is in local time. All he wants is to convert between the two representations. – Paul Ogilvie Feb 24 '15 at 15:32
  • @Paul No Paul. The SYSTEMTIME is populated legally – Sathish Guru V Feb 24 '15 at 15:39

1 Answers1

3

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.

Sathish Guru V
  • 1,417
  • 2
  • 15
  • 39