How to Convert Time into Epoch Time in C++

c++time

Say I have a specific instant in time where I know the hour, minute, day, second, month, year, etc; how can I convert this epoch time (seconds since 1970)?

I can't use Boost, so please don't suggest a Boost solution.

Best Answer

Use the mktime(3) function. For example:

struct tm t = {0};  // Initalize to all 0's
t.tm_year = 112;  // This is year-1900, so 112 = 2012
t.tm_mon = 8;
t.tm_mday = 15;
t.tm_hour = 21;
t.tm_min = 54;
t.tm_sec = 13;
time_t timeSinceEpoch = mktime(&t);
// Result: 1347764053
Related Question