Sign in to follow this  

Subtracting Dates and Time.

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

RP: Subtracting Dates and Time. If today's date is 4/1/2006 I want to know 3 days ago, and that would be the 3/29/2006. is there a convential way to calculate dates using the tm structures? OP: Unexpected Results when requesting date on stat The actual thing:
151# ls -l
total 28
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:47 2BeoPccosL
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:49 58nD8CDDg8
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:46 7ilIlbBKi1
drwxrwxrwx  2 nobody  wheel  512 Apr 16 14:00 82uxb1RC3R
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:57 9S9wwwTvFp
drwxrwxrwx  2 nobody  wheel  512 Apr 16 12:35 CCGxMTwzlG
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:59 CX3oCk70jX
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:40 GPVMejRpSz
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:48 JRf7PSvghi
drwxrwxrwx  2 nobody  wheel  512 Apr 16 12:45 MgQQhpUrIO
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:45 OEIWptaYuG
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:49 XU17bHXnMR
drwxrwxrwx  2 nobody  wheel  512 Apr 16 14:08 aba6snUhe9
drwxrwxrwx  2 nobody  wheel  512 Apr 16 13:50 szfN9PR7V3



the C++ source
		for ( int i=0; i < directories.size(); i++ ){
			//std::cout << directories.at(i);
			struct stat sbuf;
			stat( directories.at(i).c_str(), &sbuf);
			innerMap["name"]=directories.at(i);
			innerMap["st_mtime"]=sbuf.st_mtime;
			std::cout << sbuf.st_mtime << std::endl;
			std::cout << sbuf.st_atime << std::endl;
			std::cout << sbuf.st_ctime << std::endl;
			directoriesMap.insert(std::pair<std::string, std::map<std::string, std::string> >(innerMap["name"], innerMap ) );
		}



The results from the C++ source code
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044
1
135065600
673164044



okay... why is it giving identical #'s... instead of dates and time.. could it be because... http://www.scit.wlv.ac.uk/~jphb/spos/notes/calls/stat.html
 from www.scit.wiv.ac.uk
time_t	st_atime	Time of last access 	
time_t	st_mtime	Time of last data modification 	
time_t	st_ctime	Time of last file status change

the data type returned is a time_t? maybe I need to convert this time_t into string or int format ?? I mean, still when I cout << buf.st_atime I get identical results for different things edit: please if I am missing something can you point it out... Thanks... I'm really stuck and can't find much information on the net about stat() [Edited by - Tradone on April 16, 2006 3:02:47 PM]

Share this post


Link to post
Share on other sites
um.. I think I finally found what I needed:

http://www.jb.man.ac.uk/~slowe/cpp/lastmod.html


#include <sys/stat.h>


#include <unistd.h>


#include <time.h>





struct tm* clock;// create a time structure


struct stat attrib;// create a file attribute structure


stat("afile.txt", &attrib);// get the attributes of afile.txt


clock = gmtime(&(attrib.st_mtime));// Get the last modified time and put it into the time structure





// clock->tm_year returns the year (since 1900)


// clock->tm_mon returns the month (January = 0)


// clock->tm_mday returns the day of the month



Share this post


Link to post
Share on other sites
DEAR MODERATOR:
it seems that there may be some parsing bugs on the forums.
for example)
when there is a /* inside the source tags, without the */
I can't submit.
and an empty post is uploaded.

you may rate me extra helpful for that! [cool]

[Edited by - Tradone on April 16, 2006 3:19:58 PM]

Share this post


Link to post
Share on other sites
RP: Subtracting Dates and Time.

If today's date is 4/1/2006 I want to know 3 days ago, and that would be the 3/29/2006.

is there a convential way to calculate dates using the tm structures?

Share this post


Link to post
Share on other sites
To adjust times, I use mktime followed by localtime. The first converts a time/date to a timestamp, which can be easily adjusted by any number of seconds, and the latter converts the resulting timestamp into time/date format. If you want to adjust the date, just set the time to mid-day and then ignore it (setting it to all 0s may cause problems if the functions happen to take into account leap seconds, where as mid-day is least likely to be pushed to an incorrect date)

Share this post


Link to post
Share on other sites

This topic is 4261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this