How to convert a time into epoch time?

后端 未结 5 1490
感情败类
感情败类 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:21

    Sample code to convert date string in some format to unix epoch time.

    struct tm tm;
    double dateInEpoch;
    
    if (strptime("06 Jul 2022", "%d %b %Y", &tm)) {
        time_t curTime;
        struct tm * timeinfo;
                    
        time(&curTime );
        timeinfo = localtime(&curTime);
                    
        timeinfo->tm_year = tm.tm_year
        timeinfo->tm_mon = tm.tm_mon;
        timeinfo->tm_mday = tm.tm_mday;
            
        dateInEpoch = mktime( timeinfo );
    }
    

    For date format, please visit http://www.cplusplus.com/reference/ctime/strftime/

    To test epoch date, visit https://www.epochconverter.com

提交回复
热议问题