我的代码也应该在linux和windlows上运行。
我想以YYYY-MM-DD HH24:MI:SS格式获取当前时间。默认时区是UTC+08,我的系统可以位于任何时区。
如果你能帮助我使用c++代码(我没有c++11,14编译器),那将是很大的帮助
我看到了一个解决方案-使用时间以UTC格式获取当前时间,然后将TZ环境变量操作到目标时区。然后使用本地时间转换为该时区的本地时间。
但不知道如何用c++来实现这一点,它将在Windows和linux上运行。

最佳答案

我建议查看boost库。
从那里你可以简单地得到当前的当地时间,如下所示:

boost::posix_time::ptime curr_time = boost::posix_time::microsec_clock::local_time();

它还可以根据需要将其转换为字符串:
std::string curr_time_str = to_simple_string(curr_time);

回到ptime对象:
curr_time = boost::posix_time::time_from_string(curr_time_str);

等。
http://www.boost.org/doc/libs/1_61_0/doc/html/date_time/posix_time.html

10-07 19:28
查看更多