本地时间在 nohup 文件中恢复为 UTC

Local Time reverts to UTC in nohup file

我有一些程序可以写入带有日期戳的 nohup 文件。当程序在终端中 运行 并打印到屏幕上时,日期显示正确的当地时间。但是,当使用 nohup 命令从启动启动程序并将输出发送到文件时,时间始终为 UTC。

    time_t curtime;
    time(&curtime);
    //Printed to nohup.out (processlog.txt)
    printf("Application Started  %s", ctime(&curtime));

我试过 localtime() 和 strftime() 结果是一样的。

我可能会使用某种手动偏移。我尝试使用简单的 tm_hour 偏移量,但是当 UTC 时间过渡到第二天时,这将不起作用。

有什么建议吗?

甚至使用系统("date");没有在 nohup 文件 date/time 时间戳中显示本地时间。 date 命令将打印 "UTC".

也许这是 "walking around the block to cross the threshold" 但添加此子例程有效。我只希望夏令时在今年晚些时候能正常切换。

int print_time()
  {
    struct tm *localtime;
    time_t rawtime;
    time_t offset;

    setenv( "TZ", "EST5EDT", 1 );
    tzset();

    time(&rawtime);
    /* Get GMT time Offset by the timezone and DST*Number of seconds in an hour*/
    offset = (rawtime - timezone + (daylight*3600));
    localtime = gmtime(&offset);
    printf("%02d/%02d/%02d %2d:%02d:%02d\n",localtime->tm_year+1900, localtime->tm_mon+1, localtime->tm_mday,  localtime->tm_hour, localtime->tm_min, localtime->tm_sec);

    return(0);
  }

运行 任何需要 date/time 邮票的地方。例如:

//Printed to nohup.out (application_log.txt)
printf("Application Started  ");
print_time();