+ 1

How to use the asctime( ) function in C library <time.h> multiple times in a program?

I am working on a project about the Parking Management system. In the project, I need to find the number of hours spent in order to calculate the amount to be charged for parking. Although I can ask the user to enter time and then find the difference, I want my program to be able to do that by itself. For that i am using the asctime( ) function in the <time.h> library. But my program keeps returning the same value of time after subsequent calls. I have even tried storing the return value from asctime( ) in a file but it still isn't working 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( ) returns different values for the time and not the same value again and again. Your help is highly appreciated. Thanks!!

1st Dec 2018, 6:55 AM
Yusha Arif
Yusha Arif - avatar
8 Answers
+ 6
I test it this way and it worked on VS2015 and cpp.sh online compiler. #include <stdio.h> #include <time.h> char *f() { time_t t = time(NULL); struct tm *tm = localtime(&t); char *s = asctime(tm); return s; } int main() { int a; printf ("%s\n", f()); scanf("%d", &a); printf("%s\n", f()); } Sample output on cpp.sh: 45 Sat Dec 1 07:32:24 2018 Sat Dec 1 07:32:36 2018 NOTE: The SL's wouldn't show you the correct answer. _____ http://cpp.sh/6slwt
1st Dec 2018, 7:35 AM
Babak
Babak - avatar
+ 6
As had been shown in C++ Soldier (Babak) code, you should query the time twice, first when the customer enters the parking lot, and once again when they're about to leave. Your initial code is passing the arrival time structure, that's why it's not showing difference (cmiiw) : )
1st Dec 2018, 7:51 AM
Ipang
+ 5
"you;re doing right now is copy the address of asctime value to your own variables." WRONG! Because the current elapsed time from epoch has been retrieved and the program just ran once. The solution would be to separate the time() from the main().
1st Dec 2018, 7:40 AM
Babak
Babak - avatar
+ 2
i'm sorry last time your replied was slipped in notification. what i mean by store the value is allocate a new memory then put the value in there. what you;re doing right now is copy the address of asctime value to your own variables. thus s and z are pointing to same address, try to print the address they're pointing to you;ll understand. here an example code cpp.sh/8e7i7 (EDIT: i forget the header) how to solve it ? use strdup(asctime(tm)) (https://en.cppreference.com/w/c/experimental/dynamic/strdup )
1st Dec 2018, 7:30 AM
Taste
Taste - avatar
+ 2
You're welcome. 8D You probably have to break it down to its building blocks (member objects). Particularly the following object tm -> tm_hour; and then calculate the difference like so #include <stdio.h> #include <time.h> char *f(int &hour) { time_t t = time(NULL); struct tm *tm = localtime(&t); char *s = asctime(tm); hour = tm->tm_hour; return s; } int main() { int a; int h1 = 0, h2 = 0, diff = 0; printf ("%s\n", f(h1)); scanf("%d", &a); printf("%s\n", f(h2)); diff = h2 - h1; printf("Diff: %d hour(s)\n", diff); } Of course, you have to wait for at least 1 hour and hit the enter to see "Diff: 1 hour(s)"! 8D Edit: It's necessary to also bring the `tm -> tm_min` object into play and apply the minute differences, as well. _____ https://en.cppreference.com/w/c/chrono/tm
1st Dec 2018, 9:42 AM
Babak
Babak - avatar
+ 1
@C++ Soldier(Babak), thanks a lot for the help. The code is indeed working in Codeblocks as well. That really did help solve the problem. Thanks once again!!
1st Dec 2018, 9:22 AM
Yusha Arif
Yusha Arif - avatar
+ 1
Could anyone also explain how to use this code to get the number of hours? The problem is that I need to find the number of hours and it's physically not possible to keep my application running for that long. Is storing the return value from asctime( ) when first called, in a file the solution here? Or is there any other efficient way?
1st Dec 2018, 9:28 AM
Yusha Arif
Yusha Arif - avatar
0
Not at all a problem Taste. Thanks a lot for the help.
1st Dec 2018, 9:19 AM
Yusha Arif
Yusha Arif - avatar