我得到类似 2015-04-29 15:36:16.5761891 +03:00
的字符串。我可以使用 std::get_time
轻松提取日期。
std::tm time;
std::string stringTime = "2015-04-29 15:36:16.5761891 +03:00";
std::istringstream stringStream(stringTime);
stringStream >> std::get_time(&time, "%Y-%m-%d %H:%M:%S");
cout << time.tm_year << endl;
cout << time.tm_mon << endl;
cout << time.tm_mday << endl;
cout << time.tm_hour << endl;
cout << time.tm_min << endl;
cout << time.tm_sec << endl;
它对我来说很好用。现在如何从此字符串中提取 UTC 偏移量?
最佳答案
你可以这样继续阅读:
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
int main()
{
std::tm time;
std::string stringTime = "2015-04-29 15:36:16.5761891 +03:00";
std::istringstream stringStream(stringTime);
std::string decimals;
std::string offset;
stringStream >> std::get_time(&time, "%Y-%m-%d %H:%M:%S") >> decimals >> offset;
std::cout << time.tm_year << '\n';
std::cout << time.tm_mon << '\n';
std::cout << time.tm_mday << '\n';
std::cout << time.tm_hour << '\n';
std::cout << time.tm_min << '\n';
std::cout << time.tm_sec << '\n';
std::cout << decimals << '\n';
std::cout << offset << '\n';
}
输出:
115
3
29
15
36
16
.5761891
+03:00
关于c++ - 如何从字符串中获取 UTC 偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30577762/