I'm trying to extract just the time element from time()
my code at present is.
Which prints for example,
Thu May 20 11:40:56 2004
how do i change it to just extract the 11:40:56
I would use localtime in time.h Then you could get the information from the structure
You could use strftime.
if ( time(&now) != (time_t)(-1) )
struct tm *mytime = localtime(&now);
if ( mytime )
char buffer [ 32 ];
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
printf("buffer = \"%s\"\n", buffer);
/* my output
buffer = "09:09:26"
I usually always forget about strftime, very handy for printing, not much use when doing algorithms and stuff, so I usually end up doing what linuxdude suggested, but strftime is the better alternative.