C ++,如何生成time_t?

时间:2016-11-09 06:49:59

标签: c++ time

istringstream ss ("12/01/2015-05:00:00")
ss.imbue("en_US.utf-8"));
struct tm t_time;
ss >> std::get_time(&t_time, "%d/%m/%Y-%H:%M:%S");
time_t num_time=timelocal(&t_time) 

但我不允许使用boostget_time

目的是获得 time_t

我的想法是手动将sscanf这些值用于struct tm。 但问题是

struct tm {
   int tm_sec;   // seconds of minutes from 0 to 61
   int tm_min;   // minutes of hour from 0 to 59
   int tm_hour;  // hours of day from 0 to 24
   int tm_mday;  // day of month from 1 to 31
   int tm_mon;   // month of year from 0 to 11
   int tm_year;  // year since 1900
   int tm_wday;  // days since sunday
   int tm_yday;  // days since January 1st
   int tm_isdst; // hours of daylight savings time
}

tm_sectm_mon的项目并不困难。但是我如何做后续的呢? (例如tm_wdaytm_yday

1 个答案:

答案 0 :(得分:1)

mktime会忽略tm_wdaytm_yday。你必须知道这一年。至于tm_isdst,如果您不知道,请设置为-1,mktime将尝试根据已安装的时区数据自动确定。