C++ - 将 AM/PM 时间字符串转换为 posix 时间 ptime

标签 c++ boost

我正在尝试将时间字符串转换为 boost::posix_time::ptime 对象,但转换无效。这是正在使用的函数。

std::string Parser::getFormattedDate(std::string datetime)
{
  std::stringstream date_strm, date_res;
  boost::posix_time::ptime pt;
  boost::posix_time::time_input_facet *facet = new boost::posix_time::time_input_facet( "%Y-%b-%d %H:%M:%S %p" );

  date_strm.imbue( std::locale( std::locale(), facet ));
  date_strm << datetime;
  date_strm >> pt;

  date_res << pt.date().year() << "-" << std::setw(2) << std::setfill('0') << pt.date().month().as_number()
           << "-" << std::setw(2) << std::setfill('0') << pt.date().day() << " "
           << pt.time_of_day().hours() << ":" << pt.time_of_day().minutes() << ":" << pt.time_of_day().seconds();

  return date_res.str();
}

输入时间字符串 2016-Feb-29 2:00:00 AM,此函数返回 Thu Dec 3 04:00:54 287564,即显然不正确。我如何从该输入中获取正确的日期时间?在这种情况下,正确的日期时间应该是 2016-02-29 02:00:00

此函数中用于所需转换的 time_input_facet"%Y-%b-%d %H:%M:%S %p"

最佳答案

documentation说:

enter image description here

感叹号的意思是:

The following tables list the all the flags available for both date_time IO as well as strftime. Format flags marked with a single asterisk (*) have a behavior unique to date_time. Those flags marked with an exclamation point (!) are not usable for input (at this time). The flags marked with a hash sign (#) are implemented by system locale and are known to be missing on some platforms. The first table is for dates, and the second table is for times.

因此,如果您必须仅使用 Boost Datetime 来支持它,则必须手动解析 am/pm 部分

也许您可以查看 Boost Locale 以完成此任务:http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/formatting_and_parsing.html

这对我有用:

#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/locale.hpp>

static std::locale s_loc = boost::locale::generator{}.generate("");

std::string getFormattedDate(std::string datetime) {
    boost::posix_time::ptime pt;
    using namespace boost::locale;

    std::stringstream ss(datetime);
    ss.imbue(s_loc);

    date_time dt;
    if (ss >> as::ftime("%Y-%b-%d %I:%M:%S %p") >> dt) {
        ss.str("");
        ss.clear();
        ss << as::ftime("%Y-%m-%d %H:%M:%S") << dt;
        return ss.str();
    }

    throw std::bad_cast();
}

int main() {
    std::locale::global(s_loc);
    for (auto s : { "2016-Feb-29 02:06:22 AM", "2016-Mar-29 02:06:22 PM" })
        std::cout << s << " -> " << getFormattedDate(s) << "\n";
    std::cout << "Bye\n";
}

打印

2016-Feb-29 02:06:22 AM -> 2016-02-29 02:06:22
2016-Mar-29 02:06:22 PM -> 2016-03-29 14:06:22
Bye

关于C++ - 将 AM/PM 时间字符串转换为 posix 时间 ptime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960069/

相关文章:

c++ - BOOST_SPIRIT_DEFINE 不明白

c++ - 使用 boost 日志时 Unresolved external 问题

c++ - 尝试使用 opencv 在 C++ 中实现一小部分 matlab 代码

c++ - 在 QGraphicsView/QGraphicsScene 中移动 QGraphicsProxyWidget 中的嵌入式小部件

C++:深度复制基类指针

c++ - 读取保存在内存中的 .txt 文件

c++ - Boost::Regex 在 C++ 中的用法

c++ - 正则表达式匹配

c++ - 如何使用现代 C++ 转置二维 vector ?

c++ - 不存在从 "const std::string"到 "time_t"的合适转换函数