none
Convert UTC to local time using SystemTimeToTzSpecificLocalTime? RRS feed

  • Question

  • I have an epoc UTC time 1432743742 that represents Wed, 27 May 2015 16:22:22.

    (See http://www.epochconverter.com/)

    This time is actually 27/05/2015, 17:22:22 GMT+1:00 DST

    I know that the timezone in question is GMT Standard Time so I want to convert the epoc UTC time to the local time based on the timezone.

    I believe that this function allows me to do it: SystemTimeToTzSpecificLocalTime.

    If so, how can I use this function to convert an epoc UTC time to local time based on the timezone name?

    Wednesday, May 27, 2015 7:50 PM

Answers

  • In that case I would use SystemTimeToTzSpecificLocalTime(). The problem is getting that ancient epoch format into SYSTEMTIME format.

    Here is a little function that should do it:

    void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
    {
        LARGE_INTEGER jan1970FT = {0};
        jan1970FT.QuadPart = 116444736000000000I64; // january 1st 1970
        LARGE_INTEGER utcFT = {0};
        utcFT.QuadPart = ((unsigned __int64)dosTime)*10000000 + jan1970FT.QuadPart;
        FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
    }
    I can't take credit for this; I found this at http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx which is an interesting read.

    • Proposed as answer by Shu 2017 Friday, May 29, 2015 2:22 AM
    • Marked as answer by Shu 2017 Friday, June 5, 2015 8:02 AM
    Wednesday, May 27, 2015 10:04 PM

All replies

  • There is a much easer way:

     
    time_t rawtime;
    struct tm * timeinfo;
    
    time (&rawtime); // this give you epoch time
    timeinfo = localtime (&rawtime); // convert to local time
    printf ("Current local time and date: %s", asctime(timeinfo));
    
    
    

    Wednesday, May 27, 2015 8:45 PM
  • Ok I see that  the localtime function takes into the consideration the timezone on the current PC.

    But I need to be able to indicate what the timezone will be? For example the timezone can change so that its UTC+1 or UTC+2 etc.

    How can I cater for that?

    Wednesday, May 27, 2015 9:02 PM
  • In that case I would use SystemTimeToTzSpecificLocalTime(). The problem is getting that ancient epoch format into SYSTEMTIME format.

    Here is a little function that should do it:

    void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
    {
        LARGE_INTEGER jan1970FT = {0};
        jan1970FT.QuadPart = 116444736000000000I64; // january 1st 1970
        LARGE_INTEGER utcFT = {0};
        utcFT.QuadPart = ((unsigned __int64)dosTime)*10000000 + jan1970FT.QuadPart;
        FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
    }
    I can't take credit for this; I found this at http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx which is an interesting read.

    • Proposed as answer by Shu 2017 Friday, May 29, 2015 2:22 AM
    • Marked as answer by Shu 2017 Friday, June 5, 2015 8:02 AM
    Wednesday, May 27, 2015 10:04 PM