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.

CC3220SF-LAUNCHXL: CC3220SF: SNTP server

Part Number: CC3220SF-LAUNCHXL

Hi

    How to get proper india time i configured india ntp server but every time i getting utc time not a india time.

    SNTP_getTime() and  SNTP_getTimeByAddr() this api mention only get UTC time. otherwise we need to convert UTC to IST using code, difference is 5 hours 30 minutes. 

    If not getting proper time means can we use external RTC(time is important for our project).

Thank You 

   Vasu

     

  • Hi,

    SNTP protocol does not recognize time zones or daylight saving time, you need manage this by yourself. NTP use only UTC time.

    Jan

  • Hi Jan

    Manage means manually we need calculate time difference ?

    thank you
  • Hi,

    Yes, in case of NTP protocol is used. Your device cannot know itself where is located.

    Jan
  • Hi Jan

     Where i need to change for (5:30) time difference ?

    *
     * Difference between NTP Epoch (seconds since January 1, 1900 GMT) and UNIX
     * Epoch (seconds since January 1, 1970 GMT)
     */
    #define TIME_BASEDIFF ((((uint32_t)70 * 365 + 17) * 24 * 3600))
    
    #define TIME_NTP_TO_UNIX(t) ((t) - TIME_BASEDIFF)
    
    #define NTP_SERVERS 1
    #define NTP_SERVER_PORT 123
    
    /*  Time to wait for reply from server (seconds) */
    #define NTP_REPLY_WAIT_TIME 5
    
    /* Must wait at least 15 sec to retry NTP server (RFC 4330) */
    #define NTP_POLL_TIME 15
    
    /*
     *  ======== setNwpTime ========
     *  Set the time on the network processor
     *
     *  ts: time in seconds since the Epoch
     */
    
    static void setNwpTime(time_t ts)
    {
        SlDateTime_t dt;
        struct tm tm;
    
        /* Convert time since Epoch to local time */
        tm = *localtime(&ts);
    
        /* Set system clock on network processor to validate certificate */
        dt.tm_day  = tm.tm_mday;
        /* tm.tm_mon is the month since January, so add 1 to get the actual month */
        dt.tm_mon  = tm.tm_mon + 1;
        /* tm.tm_year is the year since 1900, so add 1900 to get the actual year */
        dt.tm_year = tm.tm_year + 1900;
        dt.tm_hour = tm.tm_hour;
        dt.tm_min  = tm.tm_min;
        dt.tm_sec  = tm.tm_sec;
        sl_DeviceSet(SL_DEVICE_GENERAL, SL_DEVICE_GENERAL_DATE_TIME,
                sizeof(SlDateTime_t), (unsigned char *)(&dt));
    }
    
    /*
     *  ======== startSNTP ========
     */
    void startSNTP(void)
    {
        uint64_t ntpTimeStamp;
        uint32_t currentTimeNtp = 0;
        uint32_t currentTimeUnix = 0;
        int32_t retval;
        time_t ts;
        SlNetSock_Timeval_t timeval;
        struct timespec tspec;
    
        /* Set timeout value for NTP server reply */
        timeval.tv_sec = NTP_REPLY_WAIT_TIME;
        timeval.tv_usec = 0;
    
        do {
            /* Get the time using the built in NTP server list: */
            retval = SNTP_getTime(NULL, 0, &timeval, &ntpTimeStamp);
            if (retval != 0) {
                UART_PRINT(
                    "startSNTP: couldn't get time (%ld), will retry in %d secs ...\n",
                    retval, NTP_POLL_TIME);
                sleep(NTP_POLL_TIME);
                UART_PRINT("startSNTP: retrying ...\n");
            }
    
            /* Save the current (NTP Epoch based) time */
            currentTimeNtp = ntpTimeStamp >> 32;
    
        } while (retval < 0);
    
        /*
         * Set the time on the application processor. Always pass a time value
         * based on the UNIX Epoch
         */
        currentTimeUnix = TIME_NTP_TO_UNIX(currentTimeNtp);
        tspec.tv_nsec = 0;
        tspec.tv_sec = currentTimeUnix;
        if (clock_settime(CLOCK_REALTIME, &tspec) != 0) {
            UART_PRINT("startSNTP: Failed to set current time\n");
            while(1);
        }
    
        /*
         * Use time.h APIs to set the time on the NWP and display it on the console.
         * Must call time.h APIs using the appropriate (toolchain dependent) Epoch
         * time base
         */
    #if defined(__TI_COMPILER_VERSION__)
        /* For the TI toolchain, time APIs expect times based on the NTP Epoch */
        ts = currentTimeNtp;
    #else
        /* Time APIs for GCC and IAR expect times based on the UNIX Epoch */
        ts = currentTimeUnix;
    #endif
    
        /* Set the time on the network processor */
        setNwpTime(ts);
    
        /* Print out the time in calendar format: */
        UART_PRINT("Current time: %s\n\r", ctime(&ts));
    }
    

    Thank You

  • Hi,

    Just add offset in seconds to you Unix Time (currentTimeUnix).

    Jan
  • Hi Jan

    #define TIME_BASEDIFF ((((uint32_t)70 * 365 + 17) * 24 * 3600))

    Can you explain this macro i forgot ?
    Where i need add offset ?

    Thank You
  • Hi,

    This macro is for offset between NTP time and Unix time.
    - NTP time: number of seconds of January 1, 1900 (NTP epoch)
    - Unix time: number of seconds of January 1, 1970 (the Unix epoch)

    currentTimeUnix = currentTimeUnix + (5*3600)+30*60;

    Jan
  • Hi Jan

    Sorry for delayed response,

    Now i getting proper india time.

    Thank you Jan helped solve this issue.