How to convert a time into epoch time?

后端 未结 5 1468
感情败类
感情败类 2020-12-06 16:16

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

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 17:20

    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
    

提交回复
热议问题