c - File modified time result does not match up with current time -
i working on program in need compare time of file modified current time. if file has not been modified amount of time, considered "old" , process killed.
however, when current time , modification time not return comparable results. appear come in same format, difference between 2 doesn't make sense. assuming has timezone or perhaps how returning times. tried doing searching haven't been able find has fixed issue.
get modified time:
time_t getmod(const char *file){ struct stat attrib; if (stat(file, &attrib) == -1) { perror(file); exit(1); } return attrib.st_mtime; } get current time , compare:
while(loop == 1){ printf ("sleeping..\n"); sleep(numsleep); //sleep printf ("awake..\n"); curtime = time(0); //current time modtime = getmod(argv[fileindex]); //modified time printf ("current time: %d\n", curtime); printf ("modified time: %d\n", modtime); seconds = curtime - modtime; printf ("seconds: %d\n", seconds); if(seconds > numsleep){ kill(pid, sigkill); } } an example of output have been getting when modify file while sleep:
current time: 1433371851 modified time: 1433374890
this has file stored on different server. st_mtime field updated server using server's clock if file modified remotely client machine. when clocks differ on client , server, can lead apparent discrepencies modification times.
either server or host running program has clock set wrong.
the values returned either time() or stat should irrespective of timezones should standard linux epoch (which number of seconds since 00:00:00 utc jan 1 1970 -with minor caveats) differences in timezone settings shouldn't account it.
you can simple test host running program while in same directory using now:
echo x > file; ls -l file; date the times reported ls , date should closely agree. if not, clock set wrong on client or server.
Comments
Post a Comment