none
How to convert a date in string to UTC? RRS feed

  • Question

  • Hi,

    I have a string containing a date in format MM/DD/YYYY HH:MM:SS UU  (for example: 01/31/2008 14:22:54 PM).

    I want to convert it UTC. How to do that in C++?. I don;t want to use .net ( System: Datetime).

    We can use gmtime() but that does this mean we would have to parse the TString and put it into the struct tm?

    Please advise,

    Machhindra



    Software Eng
    Wednesday, April 29, 2009 1:30 PM

Answers

  • Hello Machhindra,

    Apart from Jijo’s valuable input. I would like to share my idea with you.

    To convert a string to date time, we can get year, month, day, hour, minute and second(integer type) information from the string.

           int yy, mm, dd, hour, min, sec;
           sscanf_s(sztime, "%d/%d/%d %d:%d:%d", &mm, &dd, &yy, &hour, &min, &sec);

    we can then set these values to a time structure instance(http://www.cplusplus.com/reference/clibrary/ctime/tm/). Once we get the time structure instance, we can convert it to time_t bu calling mktime (http://www.cplusplus.com/reference/clibrary/ctime/mktime/).

           time_t tme;
           tme =  mktime(&when); //when is a time structure


    Let me know if this helps, if not, please show us more information?

    Thanks,
    Rong-Chun Zhang

    Please mark the replies as answers if they help and unmark if they don't.
    Welcome to the All-In-One Code Framework, a sample code project owned by the MSDN Forum Support team!
    • Marked as answer by Machhindra Friday, May 8, 2009 9:04 AM
    Thursday, April 30, 2009 9:20 AM
  • Hello Machhindra,

    1. Parse your date string to COleDateTime by using COleDateTime::ParseDateTime() .
    2. Now get SYSTEMTIME by calling COleDateTime::GetAsSystemTime() .
    3. Now convert to UTC by calling - TzSpecificLocalTimeToSystemTime() .
    4. This article will help you - How to convert local system time to UTC or GMT .

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Wednesday, April 29, 2009 1:50 PM

All replies

  • Hello Machhindra,

    1. Parse your date string to COleDateTime by using COleDateTime::ParseDateTime() .
    2. Now get SYSTEMTIME by calling COleDateTime::GetAsSystemTime() .
    3. Now convert to UTC by calling - TzSpecificLocalTimeToSystemTime() .
    4. This article will help you - How to convert local system time to UTC or GMT .

    Regards,
    Jijo.
    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Wednesday, April 29, 2009 1:50 PM
  • Hello Machhindra,

    Apart from Jijo’s valuable input. I would like to share my idea with you.

    To convert a string to date time, we can get year, month, day, hour, minute and second(integer type) information from the string.

           int yy, mm, dd, hour, min, sec;
           sscanf_s(sztime, "%d/%d/%d %d:%d:%d", &mm, &dd, &yy, &hour, &min, &sec);

    we can then set these values to a time structure instance(http://www.cplusplus.com/reference/clibrary/ctime/tm/). Once we get the time structure instance, we can convert it to time_t bu calling mktime (http://www.cplusplus.com/reference/clibrary/ctime/mktime/).

           time_t tme;
           tme =  mktime(&when); //when is a time structure


    Let me know if this helps, if not, please show us more information?

    Thanks,
    Rong-Chun Zhang

    Please mark the replies as answers if they help and unmark if they don't.
    Welcome to the All-In-One Code Framework, a sample code project owned by the MSDN Forum Support team!
    • Marked as answer by Machhindra Friday, May 8, 2009 9:04 AM
    Thursday, April 30, 2009 9:20 AM
  • Hello Machhindra,

    Have you got any progress on the issue? If you have any question, feel free to post here.

    Thanks,
    Rong-Chun Zhang
    Please mark the replies as answers if they help and unmark if they don't.
    Welcome to the All-In-One Code Framework, a sample code project owned by the MSDN Forum Support team!
    Wednesday, May 6, 2009 10:12 AM
  • Thanks for help.

    I will try to implement the solution and update the post.
    Software Eng
    Friday, May 8, 2009 9:04 AM