gpt4 book ai didi

C 代码时间函数在 Debian Linux 和 OpenWRT 上返回不同的结果

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:41 24 4
gpt4 key购买 nike

你能帮我写这段代码吗?您能解释一下为什么在 Debian 和 Openwrt 中使用相同的代码会收到不同的结果吗?

#include <stdio.h>
#include <time.h>
#include <syslog.h>

int main (void){

char *on_time = "16:45:00";
char *off_time = "19:20:00";

struct tm *tm_on;
struct tm *tm_off;
struct tm *tm_curr;

time_t t_on_time, t_off_time, t_curr_time;
time_t t_now, t_now_on, t_now_off;

t_now = time (NULL);
t_now_on = time(NULL);
t_now_off = time (NULL);

tm_curr = localtime (&t_now);
tm_on = localtime (&t_now_on);
tm_off = localtime (&t_now_off);

t_curr_time = mktime (tm_curr);



syslog (LOG_INFO, "CURRENT TIME IS : %ld is: %s", t_curr_time, ctime (&t_curr_time));

if (sscanf (on_time, "%d:%d:%d", &tm_on->tm_hour, &tm_on->tm_min, &tm_on->tm_sec) == 3)
{

syslog (LOG_INFO, "TM_ON : %s", asctime(tm_on));
t_on_time = mktime (tm_on);
syslog (LOG_INFO, "ON TIME IS : %ld is: %s", t_on_time, ctime (&t_on_time));
}


if (sscanf (off_time, "%d:%d:%d", &tm_off->tm_hour, &tm_off->tm_min, &tm_off->tm_sec) == 3)
{
syslog (LOG_INFO, "TM_OFF : %s", asctime(tm_off));
t_off_time = mktime (tm_off);
syslog (LOG_INFO, "OFF TIME IS : %ld is: %s", t_off_time, ctime (&t_off_time));
}

if (((t_on_time <= t_off_time) && (t_curr_time >= t_on_time) && (t_curr_time <= t_off_time)) ||
((t_off_time < t_on_time) && ((t_curr_time <= t_off_time) || (t_curr_time >= t_on_time))))
{
printf("SWITCH ON\n");
}
else
{
printf("SWITCH OFF\n");
}

}

Debian 中的结果:

Oct 29 18:25:18 s2 helloworld: CURRENT TIME IS : 1477754718 is: Sat Oct 29 18:25:18 2016
Oct 29 18:25:18 s2 helloworld: TM_ON : Sat Oct 29 16:45:00 2016
Oct 29 18:25:18 s2 helloworld: ON TIME IS : 1477748700 is: Sat Oct 29 16:45:00 2016
Oct 29 18:25:18 s2 helloworld: TM_OFF : Sat Oct 29 19:20:00 2016
Oct 29 18:25:18 s2 helloworld: OFF TIME IS : 1477758000 is: Sat Oct 29 19:20:00 2016

OpenWRT 中的结果:

Oct 29 18:25:25 192.168.11.200 syslog: CURRENT TIME IS : 1477754725 is: Sat Oct 29 18:25:25 2016
Oct 29 18:25:25 192.168.11.200 syslog: TM_ON : Sat Oct 29 18:25:25 2016
Oct 29 18:25:25 192.168.11.200 syslog: ON TIME IS : 1477748700 is: Sat Oct 29 18:25:25 2016
Oct 29 18:25:25 192.168.11.200 syslog: TM_OFF : Sat Oct 29 18:25:25 2016
Oct 29 18:25:25 192.168.11.200 syslog: OFF TIME IS : 1477758000 is: Sat Oct 29 18:25:25 2016

错在哪里?为什么 OpenWRT 中的结果不同?有什么建议吗?

最佳答案

你的时区是什么?是不是碰巧是 GMT +02:00,并且您在 Debian 机器上设置了时区,但在 OpenWRT 路由器上没有设置?

您可以通过将 localtime() 替换为 gmtime() 并在 GMT/UTC 重新进行所有时间比较来避免这些问题。

这是一个简单得多的例子(稍微修改过):

#include <stdio.h>
#include <time.h>

int main (void){

struct tm *tm_curr;
time_t t_now;

t_now = time (NULL);
tm_curr = localtime(&t_now);
printf("Local: %ld %s", t_now, asctime(tm_curr));

tm_curr = gmtime(&t_now);
printf("GMT : %ld %s", t_now, asctime(tm_curr));

}

我使用 asctime()struct tm 实际打印。当我将它(任意)设置为柏林时,你会看到影响第一行的本地时间(对我来说是中央时间和设置时间)之间的区别:

edd@max:/tmp$ ./timeex 
Local: 1477755946 Sat Oct 29 10:45:46 2016
GMT : 1477755946 Sat Oct 29 15:45:46 2016
edd@max:/tmp$ TZ="Europe/Berlin" ./timeex
Local: 1477755960 Sat Oct 29 17:46:00 2016
GMT : 1477755960 Sat Oct 29 15:46:00 2016
edd@max:/tmp$

但是,gmtime() 调用的结果不受 TZ 值的影响,并且始终是 UTC 或 GMT。

真正关键的要点time_t 在这两种情况下都是 UTC,但 struct tm 不是。你必须清楚你在哪里比较什么

计算时间和时区很麻烦。与 C 相比,我通常更喜欢 C++,但最好在 C 级别完成一次或两次。

关于C 代码时间函数在 Debian Linux 和 OpenWRT 上返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40320684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com