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.

[参考译文] LAUNCHCC3235MOD:设置夏令时

Guru**** 2381740 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/1279687/launchcc3235mod-setting-daylight-savings-time

器件型号:LAUNCHCC3235MOD

当我访问 launchpad 网页并设置时间时、它显示了一个很好的本地时间、但我的 SNTP 代码

//...
if (clock_settime(CLOCK_REALTIME, &ts) != 0) {
        Display_printf(display, 0, 0, "startSNTP: Failed to set current time\n");
        iret=0;
    }
    else{
        iret=1;
        int isz=sizeof(tp);
        int ires=clock_gettime(CLOCK_REALTIME,&tp);
        strftime(&(acdt[0]), 0x30, "%D %T", gmtime(&tp.tv_sec));
        Display_printf(display, 0, 0, "Time=%s\n",&acdt[0] );
        strftime(&(acdt[0]), 0x30, "%D %T", localtime(&tp.tv_sec));
        Display_printf(display, 0, 0, "Local Time=%s\n",&acdt[0] );
    }
//...

我得到的结果就好像 dst 未正确配置:

retVal=0

Time=10/11/23 15:42:22

Local Time=10/11/23 09:42:22

这对于 CDT 来说是错误的、

如何在本地时间正确配置 dst?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    SNTP 协议提供 UTC 时间。 Re 时间与当地时间的计算取决于您的代码。

    常用方法是按用户设置设备的位置、并从此位置根据该位置的 dst 规则计算 UTC 偏移。 其他选项可以使用 DHCP 选项2。 但 NWP 中的 DHCP 客户端不支持此功能。 您需要在应用处理器上运行自己的 DHCP 客户端才能添加此功能。

    1月

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    如前所述、该器件由用户在 Web 界面中设置、并显示正确的本地时间。 必须有一些方法可以做到这一点。 当前代码如下所示  

     

    // Time
    #include <time.h>
    #include <unistd.h>
    #include <ti/net/sntp/sntp.h>
    #define TIME_BASEDIFF        ((((uint32_t)70 * 365 + 17) * 24 * 3600))
    #define TIME_NTP_TO_LOCAL(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
    #define TIME_BASEDIFF        ((((uint32_t)70 * 365 + 17) * 24 * 3600))
    #define TIME_NTP_TO_LOCAL(t) ((t) - TIME_BASEDIFF)
    volatile int g_ibtime=0;
    //
    int startSNTP(void){
        uint32_t uimax = 0xffffffff;
        int iret=0;
        if (g_ibtime==1) return 1;
        int32_t  retVal;
        uint32_t seconds;
        uint32_t secondsFraction;
        SlNetSock_Timeval_t timeval;
        uint64_t ntpTimeStamp = 0;
        uint32_t uiunix_secs;
        struct timespec ts;
        //struct tm;
        struct timespec tp;
        char acdt[0x30];
        /* Set timeout value for NTP server reply */
        timeval.tv_sec = NTP_REPLY_WAIT_TIME;
        timeval.tv_usec = 0;
        do {
            /* Get the time use the built in NTP server list: */
            //retVal = SNTP_getTime((const char *)"192.168.1.85", 1, &timeval, &ntpTimeStamp);
            retVal = SNTP_getTime(0, 0, &timeval, &ntpTimeStamp);
            if (retVal != 0) {
                Display_printf(display, 0, 0,
                    "startSNTP: couldn't get time (%d), will retry in %d secs ...",
                    retVal, NTP_POLL_TIME);
                sleep(NTP_POLL_TIME);//
                Display_printf(display, 0, 0, "startSNTP: retrying ...");
            }
            Display_printf(display, 0, 0,"startSNTP: Got time.\n");
            seconds = (0xFFFFFFFF00000000 & ntpTimeStamp) >> 32;
            // The seconds fraction is stored in the lower 32 bits
            secondsFraction = ntpTimeStamp;
            Display_printf(display, 0, 0,"seconds=0x%08x\n",seconds);
            ts.tv_sec=TIME_NTP_TO_LOCAL(seconds);
            Display_printf(display, 0, 0,"unix seconds=0x%08x\n",ts.tv_sec);
            ts.tv_nsec=(int32_t)(((double)secondsFraction / (double)uimax) *( (double)0x3B9ACA00));
            Display_printf(display, 0, 0,"nanoseconds=0x%08x\n",ts.tv_nsec);
            int ix= strftime(&acdt[0], 0x30, "%D %T", &ts.tv_sec);
            Display_printf(display, 0, 0,"retVal=%d\n",retVal);
        } while (retVal < 0);
        if (clock_settime(CLOCK_REALTIME, &ts) != 0) {
            Display_printf(display, 0, 0, "startSNTP: Failed to set current time\n");
            iret=0;
        }
        else{
            iret=1;
            int isz=sizeof(tp);
            int ires=clock_gettime(CLOCK_REALTIME,&tp);
    
            strftime(&(acdt[0]), 0x30, "%D %T", gmtime(&tp.tv_sec));
            Display_printf(display, 0, 0, "Time=%s\n",&acdt[0] );
            strftime(&(acdt[0]), 0x30, "%D %T", localtime(&tp.tv_sec));
            Display_printf(display, 0, 0, "Local Time=%s\n",&acdt[0] );
        }
        g_ibtime=1;
        return iret;
    }
    

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    我从未将 time.h 库与 CC32xx 一起使用。 我建议使用 clock_sync.h 库、这是与 TI 演示示例通用的方法(例如 local_time)。 时区设置有 ClockSync_setTimeZone()函数。

    1月

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    谢谢。 我开始根据复杂性以及它在时间戳中的用处来重新考虑这一点。 UTC 和 XSt 对我来说已经足够好了。 我认为我的路由器不支持 DHCP 选项2。