tags:

views:

38

answers:

2

I need to convert a given date to an int containing the number of milliseconds since Jan 1 1970. (unix epoch)

I tried the following code:

tm lDate;

lDate.tm_sec = 0;  
lDate.tm_min = 0;  
lDate.tm_hour = 0;  
lDate.tm_mday = 1;  
lDate.tm_mon = 10;  
lDate.tm_year = 2010 - 1900;  

time_t lTimeEpoch = mktime(&lDate);

cout << "Epoch: " << lTimeEpoch << endl;

The result is Epoch: 1288584000 which corresponds to Mon, 01 Nov 2010 04:00:00 GMT

Edit: I was expecting Oct 01 2010, apparently tm_mon is the number of months SINCE January, so the correct line would be lDate.tm_mon = 10 -1;

A: 

You are likely getting confused by timezones. I think you're missing this, from the man page:

The mktime() function converts a broken-down time structure, expressed as local time...

Thanatos
The problem described is on the month, not on the hour.
Benoit Thiery
@Benoit Thiery: The OP has since edited his post: when I posted this answer, the OP hadn't specified what was wrong with the output he had gotten. Your post was what made me post the comment asking what date he was expecting, as I felt the timezone or the month answers were both equally possible.
Thanatos
Ok sorry, I missed the history. You might want to remove the answer in this case, but up to you.
Benoit Thiery
+1  A: 

As specified in the man page, tm_mon is: The number of months since January, in the range 0 to 11.

Benoit Thiery