none
Get OleDateTime in UTC RRS feed

  • Question

  • I am using COleDateTime::GetCurrentTime() to get the current time. It is observed that this function returns my local time. Anyone knows how to get COleDateTime in UTC?
    • Edited by SQLNeophyte Tuesday, October 8, 2019 6:07 AM
    Tuesday, October 8, 2019 6:05 AM

Answers

  • You could initialize a COleDateTime object with a UTC time -

    For example,

    SYSTEMTIME stUTC{};
    COleDateTime dt1, dt2;
    
    GetSystemTime(&stUTC);
    dt1 = COleDateTime::GetCurrentTime();
    dt2 = stUTC;
    
    wprintf(L" Local Time : %s\n", (LPCWSTR) dt1.Format());
    wprintf(L" UTC Time : %s\n", (LPCWSTR) dt2.Format());
    

    • Proposed as answer by Guido Franzke Wednesday, October 9, 2019 8:44 AM
    • Marked as answer by SQLNeophyte Monday, October 14, 2019 3:15 PM
    Tuesday, October 8, 2019 9:55 AM

All replies

  • Hello,

    Thank you for posting here.

    What about trying to get UTC time indirectly? Modify the time zone through SetTimeZoneInformation(), then get the time, and then modify the time zone back.

    	HANDLE hToken;
    	TOKEN_PRIVILEGES tkp;
    	OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
    	LookupPrivilegeValue(NULL, SE_TIME_ZONE_NAME, &tkp.Privileges[0].Luid);
    	tkp.PrivilegeCount = 1;
    	tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    	AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
    	TIME_ZONE_INFORMATION UTC_ZONE;
    	ZeroMemory(&UTC_ZONE,sizeof(UTC_ZONE));
    	StringCchCopy(UTC_ZONE.StandardName, 32, L"Coordinated Universal Time");
    	StringCchCopy(UTC_ZONE.DaylightName, 32, L"Coordinated Universal Time");
    	TIME_ZONE_INFORMATION zone;
    	GetTimeZoneInformation(&zone);
    	SetTimeZoneInformation(&UTC_ZONE);
    	COleDateTime test = COleDateTime::GetCurrentTime();
    	SetTimeZoneInformation(&zone);

    Best Regards,

    Suarez Zhou


    Tuesday, October 8, 2019 9:33 AM
  • You could initialize a COleDateTime object with a UTC time -

    For example,

    SYSTEMTIME stUTC{};
    COleDateTime dt1, dt2;
    
    GetSystemTime(&stUTC);
    dt1 = COleDateTime::GetCurrentTime();
    dt2 = stUTC;
    
    wprintf(L" Local Time : %s\n", (LPCWSTR) dt1.Format());
    wprintf(L" UTC Time : %s\n", (LPCWSTR) dt2.Format());
    

    • Proposed as answer by Guido Franzke Wednesday, October 9, 2019 8:44 AM
    • Marked as answer by SQLNeophyte Monday, October 14, 2019 3:15 PM
    Tuesday, October 8, 2019 9:55 AM
  • Hello,

    If your issue is solved, please "Mark as answer" or "Vote as helpful" post to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue. If not, please feel free to contact us.

    Best Regards,

    Suarez Zhou

    Monday, October 14, 2019 8:25 AM
  • Thank you all for the help...
    Monday, October 14, 2019 3:15 PM