This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1294KCPDT: Setting timezone from the time.h file

Part Number: TM4C1294KCPDT


Hello Everyone,

I am using Tiva TM4c1294kcpdt. In my application, I am using it's inbuilt hibernate module RTC. But at system power on, I am reading RTC from NTP server which gives me time for timezone GMT 0.

I want to set/convert timezone, while reading time from NTP server, as per end user selection of timezone. For e.g. Set timezone as GMT:+5.30 (IST), how should I do this in c? Also how to use 'TZ' structure for setting timezone in time.h file?

Any help is appreciated.

  • Hello Urvi,

    Our example is only using the the time.h structure as a framework for the Hibernate API's. As such, there isn't support built into it for the TZ structure and the examples don't make use of the time.h API which are specific to the LTS/RTS compiler.

    You would need to make these adjustments as an application modification based on the outputs you get, or have the user input their timezone at the beginning and adjust your calendar set based on the timezone. At that point it's just math to add/subtract hours and/or minutes from the received data based on the timezone.

  • To convert from UTC time to local time, take the 32 bit value from the NTP server that represents the number of seconds since 00:00 January 1, 1900 and subtract the value in the TZ struct xxx->timezone (number of seconds West of UTC). In the case of IST that value would be -19800   (-1 * (((5 * 60) + 30) * 60)). Since you are subtracting a negative number, the time value will be larger than UTC.

  • Hi Bob Crosby,

    Thanks, this worked for me. But right now I have hard coded 'timezone=19800' in TZ structure. But what if I want to change timezone at run time as per user configuration?

    How or by using which function I can set timezone at runtime?

    I am unable to use statement as: _tz.timezone=19800; it gives me error as below:

    #148 declaration is incompatible with "TZ _tz" (declared at line 114 of "C:/ti/ccsv6/tools/compiler/arm_5.1.6/include/time.h") 

    #66 expected a ";" 

  • That looks like a C syntax error.  This is really a C run-time support question instead of a TM4C129x question. You really don't need to deal with the TZ structure, just apply the appropriate offset to the time_t variable. Here is an example code that runs on a PC (not a TM4C129).

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define IST_offset ((time_t)-19800)
    
    int main()
    {
        time_t time_tUTCTime;
        time_t time_tISTime;
        struct tm *ptmUTCTime;
        struct tm *ptmISTime;
    
        printf("Hello world!\n");
        time_tUTCTime = time(&time_tUTCTime);
        ptmUTCTime = gmtime(&time_tUTCTime);
        printf("UTC is: %s", asctime(ptmUTCTime));
        time_tISTime = time_tUTCTime - IST_offset;
        ptmISTime = gmtime(&time_tISTime);
        printf("IST is: %s", asctime(ptmISTime));
    
        return 0;
    }
    

    On the TM4C129 device you would replace initializing the time_tUTCTime variable in line 15 with the value returned by the NTP server. 

  • Hi Bob Crosby,

    Thank you, that really helps me.