0

Hi, Could anyone explain how to use the C library's <time.h> header file to get the time in the internal clock?

I am currently using the asctime( ) function which gives the day, date, time and year. However, what i want is that whenever i call this function, it gives a different time( the actual time ) since time is not static and is continuously changing. However, whenever i call this function twice, it gives the exact same time (even the seconds are the same). Btw, my first call and second call do have a time gap between them which is governed by a condition which if only true would call the asctime( ) function for the second time. I have even tried storing the time from my first call in a file, then after some time, calling the asctime( ) function again and then reading the time that i stored from the file that i created. But it still gives the exact same time!! The reason i want 2 different times is so that i can calculate the difference between these times which i will use in calculating a price Eg:- price/ hr Thanks alot!!

28th Nov 2018, 2:41 AM
Yusha Arif
Yusha Arif - avatar
2 Answers
0
The returned value points to an internal array whose validity or value may be altered by any subsequent call to asctime or ctime. from: http://www.cplusplus.com/reference/ctime/asctime/ It seems each call will replace the value of an array, i suggest to copy the value entirely to a new variable, it might work
28th Nov 2018, 3:47 AM
Taste
Taste - avatar
0
Taste, could you kindly elaborate bit more on what you exactly mean by copying the value entirely into a new variable?? To be a bit more precise, here is what i was doing int a ; time_t t = time(NULL); struct tm *tm = localtime(&t); char *s= asctime(tm), *z ; printf("%s",s) ; scanf("%d",&a) ; z= asctime(tm) ; printf("%s",z) ; I added a scanf( ) statement to pause the program with the intention that as time was still running, the next call to asctime( ) would give a different value of time. I even stored it in a different pointer array but it STILL ended up printing the exact same time. Is there a way through which subsequent calls to asctime( ) return different values for the time and not the same value again and again. Thanks!!
28th Nov 2018, 9:55 AM
Yusha Arif
Yusha Arif - avatar