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.