Problem with pointers while using localtime() and time()
Basically what I want to do is get the current year, month, day, hour, minute and second.
I know I should use first time() and then pass the result to localtime() after which I can extract the desired values from the members of the tm struct. But the pointers involved in between have me muddled. The following are the declarations of the time() and localtime() functions:
Code:
time_t time(time_t *tp);
struct tm *localtime(const time_t *tp);
Now I am confused - If I get it correctly, time(time_t *tp) means that the function time gets some argument, which is equated to time_t *tp. That means (to my foggy brain) that tp is a pointer to a value of type time_t. The documentation says that the current calendar time is returned, and if tp is not NULL then the current calendar time is also assigned to *tp.
But I do not understand what struct tm *localtime means. Does it mean that localtime returns a pointer? And localtime(const time_t *tp) means that I have to give it a value or pointer (using &) as input? Please explain.
Turning to the practical details, I have successfully implemented:
Code:
time_t now;
now = time(NULL);
printf("%d\n",now);
whereby I get results like 1136698157 etc. Now I would like to know what this number means.
Also, I face problems when I try to convert the time_t value to a struct tm variable using localtime. If I simply do a
Code:
struct tm nowtm;
nowtm = localtime(now);
I get a warning and error for this:
Quote:
time.c:12: warning: passing argument 1 of 'localtime' makes pointer
from integer without a cast
time.c:12: error: incompatible types in assignment
So please tell me what I should do. Thanks for your patience and time.
Re: Problem with pointers while using localtime() and time()
See the code below you'll get some usefull indications on what to do:
Code:
time_t timval=time(NULL);
struct tm tims=*(localtime(&timval));
char temp[100];
int year;
year=1900+tims.tm_year;
sprintf(temp,"%02d/%02d/%04d - %02d:%02d:%02d",
tims.tm_mday,tims.tm_mon+1,year,
tims.tm_hour,tims.tm_min,tims.tm_sec);
Why don' you use QDateTime insread?
Re: Problem with pointers while using localtime() and time()
Quote:
Originally Posted by yop
struct tm tims=*(localtime(&timval));
This means that localtime returns a pointer, which is dereferenced by the * and the value stored in the variable tm. And &timval corresponds to const time_t *tp.
So this means that whenever I see a * inside the argument part of a function declaration, I must understand that that function must be passed a pointer, and the variables so preceded by stars are handled as pointers within the function.
Similarly whenever I see a * before the function name in the function declaration, that function returns a pointer.
Quote:
Why don' you use QDateTime insread?
Trying to do some pure ANSI C first before jumping into Qt...
BTW can I know what the meaning of the 1136698157 number is?
Re: Problem with pointers while using localtime() and time()
I tried using
Code:
struct tm now = *localtime(&time(NULL));
instead of
Code:
time_t timval = time(NULL); struct tm now = *localtime(&timval);
and got an error:
Quote:
error: invalid lvalue in unary '&'
Why?
Re: Problem with pointers while using localtime() and time()
Quote:
Originally Posted by jamadagni
I tried using
Code:
struct tm now = *localtime(&time(NULL));
instead of
Code:
time_t timval = time(NULL); struct tm now = *localtime(&timval);
and got an error: Why?
What time(NULL) returns is not a left value so cannot be 'get-addressed'
, coz the return value is a temporary value and will be destroyed after the statement.
Quote:
BTW can I know what the meaning of the 1136698157 number is?
read the manual please
Re: Problem with pointers while using localtime() and time()
Quote:
Originally Posted by jamadagni
But I do not understand what struct tm *localtime means. Does it mean that localtime returns a pointer? And localtime(const time_t *tp) means that I have to give it a value or pointer (using &) as input? Please explain.
Yes, the localtime() function returns a pointer to tm struct.
So the following C code should work:
Code:
time_t now;
struct tm * local;
int year;
time( &now );
local = localtime( &now );
year = local->tm_year;
BTW here is a good reference for the standard C time functions: http://www.cplusplus.com/ref/ctime/
Re: Problem with pointers while using localtime() and time()
Quote:
BTW can I know what the meaning of the 1136698157 number is?
It is the number of seconds since Jan 1, 1970, 12 AM.
Do the math! :)
Re: Problem with pointers while using localtime() and time()
Quote:
Originally Posted by GreyGeek
It is the number of seconds since Jan 1, 1970, 12 AM.
Do the math! :)
Thanks. I later found out from the Linux manpages. :) And you left out one thing - UTC.