D&C GLug - Home Page

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

Re: [LUG] C++ advice?

 

On Thu, 16 Jul 2009, Sam Grabham wrote:

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

It's a shame that page, like so many others don't actually tell you what 
pandorafms actually is. I can get screen shots, downloads, documentation, 
but not a single, simple paragraph telling me what it is and what it does. 
(It took me 2 more click to find out)

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

.st_mtime is of type time_t

Why not just look at the stat.h file? It's under /usr/include/

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

stat, fstat and lstat return numbers not strings.

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

They are in the C world, and I'm sure this is the same for c++ too, the 
structure member st_mtime is of type time_t (as is the returned value of 
time()). Think of them as long unsigned integers, (Which they sort of are) 
rather than strings with a date in them.

And I have to say, if I wanted to look for a file that was < 45 minutes 
old, I'd probably use find rather than write a program to do it.

   find /path/to/directory -cmin -45 -print

Gordon



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

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