Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ctime question

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
Hey guys, if I wanted to get the current time in seconds, how would I do it with Ctime....or maybe I don't even use Ctime. Please help

Thanks
Mike
 
#include <time.h>
#include <stdio.h>
Code:
int main(int argc, char* argv[])
{
	time_t tm;
	time(&tm);
	printf(&quot;%s\r\n&quot;, ctime(&tm));
}

-pete
 
when I run this, I get:
Thu Sep 4 10:45:02 2003

I need the time in seconds only, how would I do that?

I want to delete files after a certain time limit and I do a stat on the file and it gives me it's creation time in seconds (I believe ;o)), so I need to get this current time and compare them.

Thanks
Mike
 
I certainly misread your post. Don’t know what happened there [lol]

To get and compare file times you may want to use an OS dependent function and structure. What OS are you on?


-pete
 
Well that leaves me out [sadeyes]

If no one jumps in here soon try the C++: Unix forum: forum208

-pete
 
> it gives me it's creation time in seconds (I believe ;o)),
Not quite - the result is a time_t which MAY be stored in seconds (and frequently is)
Fortunately, there's a handy way of getting real elapsed seconds from this information.

Anyway, to work out what you want, do this
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>

int main ( void ) {
    struct stat sb;
    time_t  now = time(NULL);
    stat( &quot;a.out&quot;, &sb );
    printf( &quot;Seconds since last modification=%f\n&quot;, difftime(now,sb.st_mtime) );
    return 0;
}
 
apparently this version of UNIX doesn't support difftime...any ideas?

Mike
 
> apparently this version of UNIX doesn't support difftime...any ideas?
Then it is woefully broken - its surprising that anything is working.
What error message(s) do you get when compiling my previous code?
 
all it did at the time of compiling is:

ld: Undefined external -
_difftime
ld: output file still contains undefined symbols
ld: (Warning) did not generate an output file

Mike
 
If you do
Code:
man difftime
do you get a manual page?

difftime() is an ANSI-C function, so it ought to be available everywhere now.

Can you be more specific about which UNIX operating system you have, and the compiler you have (names and versions).
 
when I do a 'man difftime' it gives me
&quot;no manual entry for difftime.&quot;

I'm running HP-UX version 6.5 on an HP 9000/350 system and I compile with cc

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top