Sag*_*gar 3 c c++ mktime gmt unix-timestamp
我有一个我写的功能(如果有一个很好的标准替代品,请告诉我......)
time_t get_unix_time(string time_str) {
time_t loctime;
time(&loctime);
struct tm *given_time;
time_str = time_str.substr(0, time_str.find_first_of('.'));
replace(time_str.begin(), time_str.end(), ':', ',');
replace(time_str.begin(), time_str.end(), '-', ',');
replace(time_str.begin(), time_str.end(), '/', ',');
replace(time_str.begin(), time_str.end(), ' ', ',');
given_time = localtime(&loctime);
vector<string> trecord = split_string(time_str, ',');
given_time->tm_year = atoi(trecord.at(0).c_str()) - 1900;
given_time->tm_mon = atoi(trecord.at(1).c_str()) - 1;
given_time->tm_mday = atoi(trecord.at(2).c_str());
given_time->tm_hour = atoi(trecord.at(3).c_str());
given_time->tm_min = atoi(trecord.at(4).c_str());
given_time->tm_sec = atoi(trecord.at(5).c_str());
return mktime(given_time);
}
Run Code Online (Sandbox Code Playgroud)
该函数的输入(time_str)格式为1970-01-01 00:00:00.0.split_string()函数将字符串time_str拆分为包含以下内容的向量:
{1970,01,01,00,00,00}
用于填充given_time结构.
我编写了一个函数来测试它,并将其完全传递给了输入(epoch的开始).但是,它给我回来的时间是21600,即1970-01-01 06:00:00,或UTC + 6.预期输出为0(纪元开始).
注意:我在美国 - 中部时区,即UTC - 6.在1970年1月1日午夜CST,时间@ UTC将是1970年1月1日06:00:00.
我的功能中有什么东西使它特定于我的时区吗?我在这个函数中做错了什么,或者我可以做一些不同的事情来使它区域独立,或者至少总是UTC.