I'd like to put a date and time into my program, but I'm having a hard time understanding the functions in <ctime> like time(), localtime() and so on. I've also read about pointers and structures. I understand a little bit how they work, but somehow I just couldn't understand the examples on this website about the different functions in <ctime> and how it used pointers, struct tm, and type time_t. Here's the example I'm having a hard time understanding:
/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
/* prompt user for date */
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);
/* get current timeinfo and modify it to the user's choice */
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;
/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );
printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
return 0;
}
Can anyone tell me what line "time(&rawtime)" does, how it works and why is it necessary in this program? I tried replacing &rawtime with 'NULL' and it did the same thing. Nothing changed. Please help. I really want to understand this. Also, can anyone try to explain the whole program? I'm really sorry. I'm just a beginner who wants to learn more and more everyday...
I think the purpose of that line was to make sure rawtime was initialized to some default values in case localtime() failed for some reason.
The program is basically asking for input, using that input to set timeinfo's variables, then calling a function that checks the year, month, and day, and sets (among other things) the weekday, which it then prints for the user to see.
Do you know why the ampersand sign (&) was placed before rawtime? Does it mean that the function time() puts the value of the current time in the allocated memory or address of variable rawtime? In my understanding about pointers, ampersand sign (&) is a way of saying "the address of" which in this case would mean "the address of rawtime." So whatever the current time is, function time() puts the value of the current time in variable rawtime. Is my understanding correct?
I tried removing the line timeinfo = localtime ( &rawtime ); and assigned the value of time ( &rawtime ); to timeinfo. There was an error message in my compiler. It says: Cannot convert 'long' to 'tm *'. If localtime() puts the value of the local time to variable rawtime and assigns that value to object timeinfo, why is it so much different if I assign the value of time ( &rawtime ); to timeinfo?
I aslo tried removing time ( &rawtime );, and nothing happened. The program still works. Why is it so?
No. localtime() takes the value of its parameter and fills with it a global structure. Then returns a pointer to this structure.
time_t is an integral data type, tm is a structure type.
Simple: you're manually setting the values in timeinfo on lines 22-24. That completely overwrites anything there was before.