[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]
On Wed, 15 Jul 2009, Sam Grabham wrote: > Hi > > In C++ how should i compare dates as in the following: > > I have been using these headers > #include <sys/types.h> > #include <sys/stat.h> > > struct stat fileInfo; > std::ctime(&fileInfo.st_mtime); I'm a C programmer, and while I did spend a year progrmming in C++ once upon a time, I've erased it from my memory, but the pronciples are the same - to get the stat information on a file, you need to give it a filename: so in C: struct stat buf ; time_t ctime ; ... r = stat ("path/to/file", &buf) ; /* check r here for e.g. file not found, etc. */ ctime = buf.st_ctime ; etc. > so how should i compare now, as in time() and the file modified date? > > I wish to calc how many minutes old a file is Sounds like homework to me ... Anyway - you can't get what you're after. Unix traditionally does not store the creation date of a file, only: the time of last access (atime - reading a file), time of last modification (mtime - writing a file) and time of last status change (ctime). At first glance, time() - ctime might look like the answer (in seconds), but while ctime is set when the file is created, it is also updated by commands like chmod, chown, etc. so the original file creation time can be lost. If you want to know how long since it was last written, then using mtime will give you the correct result. atime will only work on filesystems that have not been mounted with the noatime flag. > How would i convert say a string to a date? With difficulty, unless you know beforehand the format of the string. (or you're coding in php, in which case lookup the strtotime() function - this may exit in other languages though) > would you use difftime in some way? (fDif = difftime > (fi.fileModifiedDate("C:\\temp\\test\\Project1.dev"),now); ) You've got a C: and backsashes in there... isn't this a Linux users group? And if you're writing this to run on a Win box, you will have issues with time zones and daylight savings. Good luck. Gordon -- The Mailing List for the Devon & Cornwall LUG http://mailman.dclug.org.uk/listinfo/list FAQ: http://www.dcglug.org.uk/linux_adm/list-faq.html