472,142 Members | 1,002 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

current time with ctime


Hi

I am not getting current time with this program, what am I doing
wrong?

#include <ctime>
#include <iostream>
using namespace std;

#define P(x) cout << #x " = " << (x) << "\n";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970
Jul 24 '06 #1
4 9182
Gary Wessle wrote:
>
Hi

I am not getting current time with this program, what am I doing
wrong?

#include <ctime>
#include <iostream>
using namespace std;

#define P(x) cout << #x " = " << (x) << "\n";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970
Why did you expect the current time from this? ctime gives you a string
describing the point in time you give it as argument. This argument must
contain the number of seconds elapsed since 00:00:00 UTC on January 1,
1970. Since your 'rawtime' is zero, that's the time you get.
If you want to get the current time, you first have to initialize your
time_t value to that.

Jul 24 '06 #2
Rolf Magnus <ra******@t-online.dewrites:
Gary Wessle wrote:

Hi

I am not getting current time with this program, what am I doing
wrong?

#include <ctime>
#include <iostream>
using namespace std;

#define P(x) cout << #x " = " << (x) << "\n";

int main(){
time_t rawtime;
P( rawtime );
P( ctime(& rawtime) );
}

**************** output ****************
rawtime = 0
ctime(& rawtime) = Thu Jan 1 10:00:00 1970

Why did you expect the current time from this?
because off
************************************************** **************
http://www.cplusplus.com/ref/ctime/ctime.html
Example.

/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );

return 0;
}
************************************************** **************
>ctime gives you a string
describing the point in time you give it as argument. This argument must
contain the number of seconds elapsed since 00:00:00 UTC on January 1,
1970. Since your 'rawtime' is zero, that's the time you get.
If you want to get the current time, you first have to initialize your
time_t value to that.
is there a good link out there?
Jul 24 '06 #3
Gary Wessle wrote:
>Why did you expect the current time from this?
because off
time_t rawtime;

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );
Note the call to "time()".
Jul 24 '06 #4
Rolf Magnus wrote:

Why did you expect the current time from this? ctime gives you a
string describing the point in time you give it as argument. This
argument must contain the number of seconds elapsed since 00:00:00
UTC on January 1, 1970.

That's not accurate. The form of the return from time() is not
specified, nor indeed is the type of time_t. A common implementation is
as you describe.


Brian
Jul 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Stockwell | last post: by
2 posts views Thread by zolaris | last post: by
18 posts views Thread by Tim Quon | last post: by
2 posts views Thread by Suresh | last post: by
17 posts views Thread by Razzel | last post: by
3 posts views Thread by rrs.matrix | last post: by
1 post views Thread by Allen Maki | last post: by
5 posts views Thread by Allen Maki | last post: by
1 post views Thread by Josef Dalcolmo | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.