Simple C question

Kurt Wall kwall
Fri Jan 14 09:03:05 PST 2005


On Fri, Jan 14, 2005 at 08:19:47AM -0500, Bruce Marshall took 24 lines to write:
> I have a program written in C that I wrote long, long ago in the other 
> operating system.
> 
> The only thing it needs to bring it over to Linux is a call to get the current 
> date and time.

Several approaches. I usually use time() to get the calendar time
and then ctime() to format it a a proper string:

#include <stdio.h>
#include <time.h>

int main()
{
	time_t now;

	time(&now);
	printf("%s", ctime(&now));
	return 0;
}

You can also do something like asctime(localtime(&now));. Note that ctime()
appends the terminating newline ('\n'), so you don't need to include it
in your format string.

Kurt
-- 
We may not return the affection of those who like us, but we always
respect their good judgement.


More information about the Linux-users mailing list