C++ Time returned is two hours out

后端 未结 2 950
天涯浪人
天涯浪人 2021-01-22 13:15

Bit of a c++ newbie so here we go;

I have a method that is parsing a date/time, however that date/time is passed to me always with 00:00:00 as the hh:mm:ss. As such i wa

相关标签:
2条回答
  • 2021-01-22 13:35

    Try this function:

    CTime Time2UTC(CTime original)
    {
        CString Formatted = original.FormatGmt(L"%Y%m%d%H%M%S");
        int Year, Month, Day, Hour, Minute;
        if (Formatted != L"" && Formatted.GetLength() >= 12)
        {
            Year = _wtol(Formatted.Left(4));
            Month = _wtol(Formatted.Mid(4, 2));
            Day = _wtol(Formatted.Mid(6,2));
            Hour = _wtol(Formatted.Mid(8, 2));
            Minute = _wtol(Formatted.Mid(10, 2));
            CTime result(Year, Month, Day, Hour, Minute, 0);
            return result;
        }
        else
            return (CTime)NULL;
    }
    
    0 讨论(0)
  • 2021-01-22 13:44

    The reson is the mktime() method used in the first function uses local time, but gmtime() uses UTC time.

    See http://www.cplusplus.com/reference/clibrary/ctime/mktime/ and http://www.cplusplus.com/reference/clibrary/ctime/gmtime/ for further explanation.

    0 讨论(0)
提交回复
热议问题