Naman
Naman

Reputation: 118

Converting seconds after epoch into unix time

I have seconds (say x, x is long long data type) after epoch. I wish to convert it into unix time using <ctime> library. The issue is, I want a time_t variable for gmtime() to work, I am unable to make my x (long long) converted to time_t.

This is what am doing:

//x is number of seconds after epoch, I want that in unix time 
time_t t=x; 
printf("%s\n",asctime(gmtime(&t)));

Can I somehow typecast x into time_t?

Upvotes: 0

Views: 983

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

Just cast it?

time_t t = (time_t) x;

It will cut of the top 32 bits but you still have over 25 years until you really need 64-bit timestamps. :)

Of course, there might be trouble if your epoch is not the same as the POSIX epoch (1970-01-01 00:00:00).

Upvotes: 5

Related Questions