D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] C++ advice?

 

Hi

Well spotted with the "backsashes", This is due to wanting code to compile 
on windows and linux.

And it's not homework, I am wanting to create this as i am forcing myself to 
learn C++ and it's for an server agent that reports back to FMS pandora 
monitoring (http://pandorafms.org/)  that looks for a backup file in the 
last 45 mins etc.

I still haven't found a method to convert the  ".st_mtime" in the stat.h 
into a t_time so i can pass to the difftime function.

Does any one know if the date string returned from the ".st_mtime" value 
will always be in the same format?

should i be stripping the values from string and setting into a tm varible, 
then setting a t_time for a compare using difftime?

What method would you use?

are  time() - ctime the same type of varible?

Regards

Sam

----- Original Message ----- 
From: "Gordon Henderson" <gordon+dcglug@xxxxxxxxxx>
To: <list@xxxxxxxxxxxxx>
Sent: Thursday, July 16, 2009 8:04 AM
Subject: Re: [LUG] C++ advice?


> 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


--------------------------------------------------------------------------------



No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.375 / Virus Database: 270.13.15/2239 - Release Date: 07/15/09 
06:07:00


-- 
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