Discussion:
Need to get current time using localtime and time, please help
(too old to reply)
Jamadagni
2006-01-09 05:05:41 UTC
Permalink
Hello - I'm just learning C++. I know the bare basics of C, but
pointers still have me muddled.

Basically what I want to do now 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:

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:

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

struct tm nowtm;
nowtm = localtime(now);

I get a warning and error for this:

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.
osmium
2006-01-09 16:44:59 UTC
Permalink
Post by Jamadagni
Hello - I'm just learning C++. I know the bare basics of C, but
pointers still have me muddled.
Basically what I want to do now 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
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.
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
struct tm nowtm;
nowtm = localtime(now);
time.c:12: warning: passing argument 1 of 'localtime' makes pointer
from integer without a cast
time.c:12: error: incompatible types in assignment
First, try this
-----------------
#include <iostream>
#include <ctime>

int main()
{
using std::cout;
using std::endl;
time_t start = time(NULL);
struct tm* decoded_time;
decoded_time = localtime(&start);
int mm = decoded_time->tm_mon + 1;
int dd = decoded_time->tm_mday;
int yy = decoded_time->tm_year + 1900;
cout << mm << ' ' << dd << ' ' << yy << endl;
//system("pause");
}
------------------
The value returned by time() is encoded by any method the compiler writer
prefers. Typically (as in your case) it is the number of seconds since Jan
1, 1970. The birth date of Unix. But you aren't supposed to know this. So
the compiler provides a structure and functions to do the conversion to
meaningful units.

The stuff in <ctime> is some of the ugliest stuff in any of the headers. If
you can make it there, you can make it anywhere. . It doesn't help that the
K&R description seems designed to cloud men's minds.
Mike Wahler
2006-01-09 23:20:35 UTC
Permalink
Post by Jamadagni
Hello - I'm just learning C++. I know the bare basics of C, but
pointers still have me muddled.
Basically what I want to do now 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
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.
Correct.
Post by Jamadagni
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.
Correct.
Post by Jamadagni
But I do not understand what struct tm *localtime means.
Does it mean
that localtime returns a pointer?
Yes. A pointer to an object of type 'struct tm'.
Post by Jamadagni
And localtime(const time_t *tp) means
that I have to give it a value or pointer (using &) as input?
You can pass for the argument either NULL, or the
address of a type 'time_t' object.
Post by Jamadagni
Please explain.
time_t now;
now = time(NULL);
printf("%d\n",now);
Here's a problem. '%d' is the specifier for argument
of type 'int'. Not 'time_t'.
Post by Jamadagni
whereby I get results like 1136698157 etc. Now I would like to know
what this number means.
To you, the programmer, nothing. An implementation can
represent this type however it wants. It might be a simple
integral type, or could be some elaborate structure, or
something else. This value is not intended to be used
'directly', only as input to other time handling functions.
Post by Jamadagni
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
struct tm nowtm;
nowtm = localtime(now);
Two problems here:
'nowtm's type is 'struct tm'. But note that 'localtime()'s
return type is not that, but 'struct tm *' (pointer to type
'struct tm'). Also, 'now's type is 'time_t'. But the paramter
type is 'time_t *' (pointer to type 'time_t').
Post by Jamadagni
time.c:12: warning: passing argument 1 of 'localtime' makes pointer
from integer without a cast
Right. This indicates that your implementation is using some
integer type to represent 'time_t' (but this isn't always the
case, so don't depend upon it). You're trying to assign
a pointer value to a type 'time_t' object. 'time_t' is
not a pointer type.
Post by Jamadagni
time.c:12: error: incompatible types in assignment
Right. You're trying to assign 'localtime()'s return
value (a pointer) to a type 'struct tm'. You need
to assign it to a pointer (to type 'struct tm').
Post by Jamadagni
So please tell me what I should do. Thanks for your patience and time.
time_t now = time(NULL);

struct tm nowtm;
struct tm *ptm;
pnowtm = localtime(&now);
nowtm = *pnowtm;

or more succinctly:

struct tm nowtm = *localtime(&now);

-Mike

Loading...