Issue
I have some programs that write to nohup files with date stamps. When the programs are run in a terminal and printed to the screen, the date shows the correct local time. However, when the programs are started from bootup with a nohup command and the output is sent to a file, the time is always in UTC.
time_t curtime;
time(&curtime);
//Printed to nohup.out (processlog.txt)
printf("Application Started %s", ctime(&curtime));
I tried localtime() and strftime() and the results are the same.
I would perhaps use an manual offset of some sort. I tried with using a simple tm_hour offset but this will not work when the UTC time transitions into the next day.
Any suggestions?
Solution
Even using system("date"); did not show local time in the nohup file date/time stamps. The date command would print "UTC".
Perhaps this is "walking around the block to cross the threshold" but adding this sub routine works. I just hope the daylight savings time switches properly later in the year.
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);
}
Run the sub anywhere a date/time stamp is needed. Ex:
//Printed to nohup.out (application_log.txt)
printf("Application Started ");
print_time();
Answered By - DaDaDadeo