c - 运行的进程不遵循系统时间

标签 c linux time timezone

考虑以下程序,它除了每分钟打印一次时间之外什么也不做。 当我将/etc/localtime 链接更改为不同的时区时,我希望它根据新时区打印时间,但它仍然使用启动时有效的时区。

如何让程序使用正确的时间,即使在运行时更改了时区?

顺便说一句,系统是CentOS 5.8

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define MAX_STIME_LEN 32
int
main()
{
    while(1){
        struct timeval timev;
        gettimeofday(&timev,0);
        struct tm now_tm;
        time_t now = timev.tv_sec;
        char saved_time[MAX_STIME_LEN];
        strftime(saved_time, sizeof(saved_time), "%b %e %T", localtime_r (&now, &now_tm));
        printf("%s\n",saved_time);
        sleep(15);
    }
}

请注意,有必要使用 localtime_r 和 strftime 而不是使用 ons-stop 调用。这只是一个例子。

最佳答案

好吧,我在这里找到了答案:stackoverflow.com/questions/19170721/…,所缺少的是我必须取消设置环境变量 TZ (即使它只是指向/etc/localtime)

关于c - 运行的进程不遵循系统时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21661118/

相关文章:

linux - 合并两个 CSV,同时解析重复项

linux - 错误 : 'b' argument of 'sign' intrinsic at (1) must be the same type and kind as 'a'

MySQL从毫秒字段中选择格式化日期

java - 计算两次android之间的差异

c - MISRA 2012 违规 - 类型不匹配(规则 10.1、10.4)

c - gtk 从对话框中显示对话框

python - 从 Bash 使用 STDIN 运行 Python

ios - 如何最好地按时间戳保存聊天中的消息?

c 一个数组到另一个数组的大小

C:使用 do while 循环写入/读取文件