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.

Gettime project going wrong

Other Parts Discussed in Thread: CC3200

Hey guys I just got hold of the CC3200 I started with the get time example program everything works just fine except that the time displayed is wrong I have attached the screenshot of the output can anybody help 

  • Hi,

    Have you modified: GMT_DIFF_TIME_HRS & GMT_DIFF_TIME_MINS ?
    Please see: processors.wiki.ti.com/.../CC32xx_Info_Center_Get_Time_Application

    Regards,
    Gigi Joseph.
  • Yes I added the Gmt time difference now the time is right  but the date is a day ahead like it should 14 but its giving 15 is this due to 2016 being leap year?

  • Guys anybody can just give some  idea like  whats wrong

  • Hi Punit,

    Please refer to the GetSNTPTime function in main for the algorithm which converts the NTP (Network Time Protocol) packet from seconds since 1900 to a human readable date. A conversion error of this timestamp is likely causing the value to be a day off.

    Best,

    Austin

  • Here is a fix this should give you right date and time base on your time zone.

    long GetSNTPTime(/*unsigned char*/ long ucGmtDiffHr, /*unsigned char*/ long ucGmtDiffMins)
    {

    /*
    NTP Packet Header:


    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |LI | VN |Mode | Stratum | Poll | Precision |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Root Delay |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Root Dispersion |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Reference Identifier |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Reference Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Originate Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Receive Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Transmit Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Key Identifier (optional) (32) |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | |
    | Message Digest (optional) (128) |
    | |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    */
    char cDataBuf[48];
    long lRetVal = 0;
    int iAddrSize;
    //
    // Send a query ? to the NTP server to get the NTP time
    //
    memset(cDataBuf, 0, sizeof(cDataBuf));
    cDataBuf[0] = '\x1b';

    sAddr.sa_family = AF_INET;
    // the source port
    sAddr.sa_data[0] = 0x00;
    sAddr.sa_data[1] = 0x7B; // UDP port number for NTP is 123
    sAddr.sa_data[2] = (char)((g_sAppData.ulDestinationIP>>24)&0xff);
    sAddr.sa_data[3] = (char)((g_sAppData.ulDestinationIP>>16)&0xff);
    sAddr.sa_data[4] = (char)((g_sAppData.ulDestinationIP>>8)&0xff);
    sAddr.sa_data[5] = (char)(g_sAppData.ulDestinationIP&0xff);

    lRetVal = sl_SendTo(g_sAppData.iSockID,
    cDataBuf,
    sizeof(cDataBuf), 0,
    &sAddr, sizeof(sAddr));
    if (lRetVal != sizeof(cDataBuf))
    {
    // could not send SNTP request
    ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED);
    }

    //
    // Wait to receive the NTP time from the server
    //
    sLocalAddr.sin_family = SL_AF_INET;
    sLocalAddr.sin_port = 0;
    sLocalAddr.sin_addr.s_addr = 0;
    if(g_sAppData.ulElapsedSec == 0)
    {
    lRetVal = sl_Bind(g_sAppData.iSockID,
    (SlSockAddr_t *)&sLocalAddr,
    sizeof(SlSockAddrIn_t));
    }

    iAddrSize = sizeof(SlSockAddrIn_t);

    lRetVal = sl_RecvFrom(g_sAppData.iSockID,
    cDataBuf, sizeof(cDataBuf), 0,
    (SlSockAddr_t *)&sLocalAddr,
    (SlSocklen_t*)&iAddrSize);
    ASSERT_ON_ERROR(lRetVal);

    //
    // Confirm that the MODE is 4 --> server
    //
    if ((cDataBuf[0] & 0x7) != 4) // expect only server response
    {
    ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED); // MODE is not server, abort
    }
    else
    {
    unsigned char iIndex;

    //
    // Getting the data from the Transmit Timestamp (seconds) field
    // This is the time at which the reply departed the
    // server for the client
    //
    g_sAppData.ulElapsedSec = cDataBuf[40];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[41];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[42];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[43];

    //
    // seconds are relative to 0h on 1 January 1900
    //
    g_sAppData.ulElapsedSec -= TIME2013;

    //
    // in order to correct the timezone
    //
    g_sAppData.ulElapsedSec += (ucGmtDiffHr * SEC_IN_HOUR);
    g_sAppData.ulElapsedSec += (ucGmtDiffMins * SEC_IN_MIN);

    g_sAppData.pcCCPtr = &g_sAppData.acTimeStore[0];

    //
    // day, number of days since beginning of 2013
    //
    g_sAppData.isGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY;
    memcpy(g_sAppData.pcCCPtr,
    g_acDaysOfWeek2013[g_sAppData.isGeneralVar%7], 3);
    g_sAppData.pcCCPtr += 3;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // month
    //
    g_sAppData.isGeneralVar %= 365;
    for (iIndex = 0; iIndex < 12; iIndex++)
    {
    g_sAppData.isGeneralVar -= g_acNumOfDaysPerMonth[iIndex];
    if (g_sAppData.isGeneralVar <= 0)
    break;
    }
    if(iIndex == 12)
    {
    iIndex = 0;
    }
    memcpy(g_sAppData.pcCCPtr, g_acMonthOfYear[iIndex], 3);
    g_sAppData.pcCCPtr += 3;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // date
    // restore the day in current month
    //
    g_sAppData.isGeneralVar += g_acNumOfDaysPerMonth[iIndex];
    g_sAppData.uisCCLen = itoa(g_sAppData.isGeneralVar , g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // time
    //
    g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec%SEC_IN_DAY;

    // number of seconds per hour
    g_sAppData.ulGeneralVar1 = g_sAppData.ulGeneralVar%SEC_IN_HOUR;

    // number of hours
    g_sAppData.ulGeneralVar /= SEC_IN_HOUR;
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = ':';

    // number of minutes per hour
    g_sAppData.ulGeneralVar = g_sAppData.ulGeneralVar1/SEC_IN_MIN;

    // number of seconds per minute
    g_sAppData.ulGeneralVar1 %= SEC_IN_MIN;
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = ':';
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar1,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // year
    // number of days since beginning of 2013
    //
    g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY;
    g_sAppData.ulGeneralVar /= 365;
    g_sAppData.uisCCLen = itoa(YEAR2013 + g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;

    *g_sAppData.pcCCPtr++ = '\0';

    UART_PRINT("response from server: ");
    UART_PRINT((char *)g_acSNTPserver);
    UART_PRINT("\n\r");
    UART_PRINT(g_sAppData.acTimeStore);
    UART_PRINT("\n\r\n\r");
    }
    return SUCCESS;
    }

  • Did this work for you let me know if you still have an issue.
  • by removing 1 from the date calculation will print you the start of month as 0, so, Instead of changing the calculation change the array that has number of days to 29 in 1st index behalf of 28.

    const char g_acNumOfDaysPerMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    but this is also not a full solution, because,this will work fine for leap year not for ordinary year.
    If you got any solution for that please share...
  • Yes I have refined the whole code to get it working disappointing that TI has shipped wrong example code
  • I too refined now only, Here is the change that i have done.

    const char g_acNumOfDaysPerMonthNonLeapYear[12] = {31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31};

    long GetSNTPTime(unsigned char ucGmtDiffHr, unsigned char ucGmtDiffMins)
    {

    /*
    NTP Packet Header:


    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |LI | VN |Mode | Stratum | Poll | Precision |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Root Delay |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Root Dispersion |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Reference Identifier |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Reference Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Originate Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Receive Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | Transmit Timestamp (64) |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Key Identifier (optional) (32) |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | |
    | |
    | Message Digest (optional) (128) |
    | |
    | |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    */
    char cDataBuf[48];
    long lRetVal = 0;
    int iAddrSize;
    //
    // Send a query ? to the NTP server to get the NTP time
    //
    memset(cDataBuf, 0, sizeof(cDataBuf));
    cDataBuf[0] = '\x1b';

    sAddr.sa_family = AF_INET;
    // the source port
    sAddr.sa_data[0] = 0x00;
    sAddr.sa_data[1] = 0x7B; // UDP port number for NTP is 123
    sAddr.sa_data[2] = (char)((g_sAppData.ulDestinationIP>>24)&0xff);
    sAddr.sa_data[3] = (char)((g_sAppData.ulDestinationIP>>16)&0xff);
    sAddr.sa_data[4] = (char)((g_sAppData.ulDestinationIP>>8)&0xff);
    sAddr.sa_data[5] = (char)(g_sAppData.ulDestinationIP&0xff);

    lRetVal = sl_SendTo(g_sAppData.iSockID,
    cDataBuf,
    sizeof(cDataBuf), 0,
    &sAddr, sizeof(sAddr));
    if (lRetVal != sizeof(cDataBuf))
    {
    // could not send SNTP request
    ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED);
    }

    //
    // Wait to receive the NTP time from the server
    //
    sLocalAddr.sin_family = SL_AF_INET;
    sLocalAddr.sin_port = 0;
    sLocalAddr.sin_addr.s_addr = 0;
    if(g_sAppData.ulElapsedSec == 0)
    {
    lRetVal = sl_Bind(g_sAppData.iSockID,
    (SlSockAddr_t *)&sLocalAddr,
    sizeof(SlSockAddrIn_t));
    }

    iAddrSize = sizeof(SlSockAddrIn_t);

    lRetVal = sl_RecvFrom(g_sAppData.iSockID,
    cDataBuf, sizeof(cDataBuf), 0,
    (SlSockAddr_t *)&sLocalAddr,
    (SlSocklen_t*)&iAddrSize);
    ASSERT_ON_ERROR(lRetVal);

    //
    // Confirm that the MODE is 4 --> server
    //
    if ((cDataBuf[0] & 0x7) != 4) // expect only server response
    {
    ASSERT_ON_ERROR(SERVER_GET_TIME_FAILED); // MODE is not server, abort
    }
    else
    {
    unsigned char iIndex;

    //
    // Getting the data from the Transmit Timestamp (seconds) field
    // This is the time at which the reply departed the
    // server for the client
    //
    g_sAppData.ulElapsedSec = cDataBuf[40];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[41];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[42];
    g_sAppData.ulElapsedSec <<= 8;
    g_sAppData.ulElapsedSec += cDataBuf[43];

    //
    // seconds are relative to 0h on 1 January 1900
    //
    g_sAppData.ulElapsedSec -= TIME2013;

    //
    // in order to correct the timezone
    //
    g_sAppData.ulElapsedSec += (ucGmtDiffHr * SEC_IN_HOUR);
    g_sAppData.ulElapsedSec += (ucGmtDiffMins * SEC_IN_MIN);

    g_sAppData.pcCCPtr = &g_sAppData.acTimeStore[0];

    //
    // day, number of days since beginning of 2013
    //
    g_sAppData.isGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY;
    memcpy(g_sAppData.pcCCPtr,
    g_acDaysOfWeek2013[g_sAppData.isGeneralVar%7], 3);
    g_sAppData.pcCCPtr += 3;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // year
    // number of days since beginning of 2013
    //
    g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec/SEC_IN_DAY;
    g_sAppData.ulGeneralVar /= 365;
    g_sAppData.uisCCLen = itoa(YEAR2013 + g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;

    int Year = 0;
    Year = (YEAR2013 + g_sAppData.ulGeneralVar);
    *g_sAppData.pcCCPtr++ = '\x20';

    if(((Year % 4 == 0) && (Year % 100 != 0))||(Year % 400 == 0))
    {
    //
    // month
    //
    g_sAppData.isGeneralVar %= 365;
    for (iIndex = 0; iIndex < 12; iIndex++)
    {
    g_sAppData.isGeneralVar -= g_acNumOfDaysPerMonth[iIndex];
    if (g_sAppData.isGeneralVar < 0)
    break;
    }
    if(iIndex == 12)
    {
    iIndex = 0;
    }
    memcpy(g_sAppData.pcCCPtr, g_acMonthOfYear[iIndex], 3);
    g_sAppData.pcCCPtr += 3;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // date
    // restore the day in current month
    //

    g_sAppData.isGeneralVar += g_acNumOfDaysPerMonth[iIndex];
    g_sAppData.uisCCLen = itoa(g_sAppData.isGeneralVar + 1,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = '\x20';
    }
    else
    {
    //
    // month
    //
    g_sAppData.isGeneralVar %= 365;
    for (iIndex = 0; iIndex < 12; iIndex++)
    {
    g_sAppData.isGeneralVar -= g_acNumOfDaysPerMonthNonLeapYear[iIndex];
    if (g_sAppData.isGeneralVar < 0)
    break;
    }
    if(iIndex == 12)
    {
    iIndex = 0;
    }
    memcpy(g_sAppData.pcCCPtr, g_acMonthOfYear[iIndex], 3);
    g_sAppData.pcCCPtr += 3;
    *g_sAppData.pcCCPtr++ = '\x20';

    //
    // date
    // restore the day in current month
    //

    g_sAppData.isGeneralVar += g_acNumOfDaysPerMonthNonLeapYear[iIndex];
    g_sAppData.uisCCLen = itoa(g_sAppData.isGeneralVar + 1,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = '\x20';

    }

    //
    // time
    //
    g_sAppData.ulGeneralVar = g_sAppData.ulElapsedSec%SEC_IN_DAY;

    // number of seconds per hour
    g_sAppData.ulGeneralVar1 = g_sAppData.ulGeneralVar%SEC_IN_HOUR;

    // number of hours
    g_sAppData.ulGeneralVar /= SEC_IN_HOUR;
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = ':';

    // number of minutes per hour
    g_sAppData.ulGeneralVar = g_sAppData.ulGeneralVar1/SEC_IN_MIN;

    // number of seconds per minute
    g_sAppData.ulGeneralVar1 %= SEC_IN_MIN;
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = ':';
    g_sAppData.uisCCLen = itoa(g_sAppData.ulGeneralVar1,
    g_sAppData.pcCCPtr);
    g_sAppData.pcCCPtr += g_sAppData.uisCCLen;
    *g_sAppData.pcCCPtr++ = '\0';

    UART_PRINT("response from server: ");
    UART_PRINT((char *)g_acSNTPserver);
    UART_PRINT("\n\r");
    UART_PRINT(g_sAppData.acTimeStore);
    UART_PRINT("\n\r\n\r");
    }
    return SUCCESS;
    }