none
DateTime Conversion - Local to UTC time

    Question

  • Hello, I am trying to convert a datatime object which is in long to its UTC equivalent.

    Here foundTime is in long


                COleDateTime dateTimeObj (foundTime);

                SYSTEMTIME utcTime;

                SYSTEMTIME localTime;

               

                //Gets the Time zone of the System

                TIME_ZONE_INFORMATION TimeZoneInfo;

                GetTimeZoneInformation( &TimeZoneInfo );

     

                dateTimeObj.GetAsSystemTime(localTime);   [As per my understanding GetAsSystemTime converts the time of dt to its SYSTEMTIME equivalent , however an automatic UTC conversion occurs here?]



    TzSpecificLocalTimeToSystemTime(&TimeZoneInfo,&localTime,&utcTime);

         

                dtFinal = COleDateTime(utcTime);

                CString sfoundTimeStamp = dtFinal.Format(_T("%Y-%m-%d %H:%M:%S"));

                COleDateTime lookUpTime;

                lookUpTime.ParseDateTime(sfoundTimeStamp);

           


    However I belive that when GetAsSystemTime is called, an auto UTC converstion occurs thus breaking this logic . As an alternative I would like to convert the long time to SYSTEMTIME .

    Thanks!




    C# Fan! ananda84(at)gmail(dot)com
    Thursday, August 27, 2009 4:31 AM

All replies

  • Hi Ananda,

    I am not sure I understand you clearly, if I misunderstood you, please let me know. Based on my understanding, GetAsSystemTime will return SYSTEMTIME data structure, it will just return the time stored in COleDateTime object. It is maybe  local time of system, or UTC time. For example:

      t.ParseDateTime(L"1/25/1996 8:30:00");  
      SYSTEMTIME   st;   
      t.GetAsSystemTime(st);  

    After running, st is 1/25/1996 8:30:00 with SYSTEMTIME data structure. If you want to convert to UTC, you can use  TzSpecificLocalTimeToSystemTime() to convert. If you want to convert to local time, you can use SystemTimeToTzSpecificLocalTime() to convert. 

    For your reference, please refer to the following articles:


    Best Regards,
    Nancy


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by Nancy Shao Wednesday, September 02, 2009 8:14 AM Add information
    Friday, August 28, 2009 10:18 AM