如何从字符串中获取UTC偏移量

时间:2015-06-01 15:45:46

标签: c++

我收到了像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偏移量?

1 个答案:

答案 0 :(得分:2)

你可以继续阅读:

#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