Click to See Complete Forum and Search --> : C time...


goon12
12-12-2002, 04:03 PM
I have been reading all kinds of man pages, and searching on google.. I am just looking to show the current date / time using C. Could someone let me know the simplest way?


Thanks,
goon12

PS The only way I can get it working is with this UGLY hack:

void tagdate( void )
{
FILE *out;
char now[20];

memset(&now, 0, sizeof(now));

system("date > .dt");
out=fopen(".dt", "r");

fgets(now, sizeof(now), out);
now[strlen(now) - 1] = 0;
printf("date: %s\n", now);

}

binaryDigit
12-12-2002, 05:16 PM
http://www.opengroup.org/onlinepubs/7908799/xsh/strftime.html

spacedog
12-12-2002, 05:23 PM
this should work....
try man ctime



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

int main(){
struct tm* time;
time_t* local;

time = localtime(local);

printf("%s", asctime(time));
exit(EXIT_SUCCESS);
}

spacedog
12-12-2002, 05:46 PM
oops sorry i made a little mistake.

try this one.


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

int main ()
{
time_t local;
struct tm * date_and_time;

time ( &local );
date_and_time = localtime ( &local );
printf ( "The date and time are: %s", asctime (date_and_time) );

exit(0);
}

bwkaz
12-12-2002, 07:08 PM
man -k time will print (assuming the whatis database is OK -- try makewhatis as root to update it) all the man pages that have "time" in either their title or description line.

;)

goon12
12-13-2002, 10:48 AM
Thank you for all the help, it's working now ;)