【问题标题】:Convert string to boost::gregorian::greg_month将字符串转换为 boost::gregorian::greg_month
【发布时间】:2012-02-24 13:44:44
【问题描述】:

在 Boost 日期时间库中,是否有用于将月份短字符串(例如 Jan、Feb、Mar、Apr)转换为 boost::gregorian::greg_month 类型的实用函数?该库的文档不是很好,我在标题中看不到任何内容。

【问题讨论】:

    标签: c++ boost-date-time


    【解决方案1】:

    一个 hacky 解决方法可能是:

    #include <iostream>
    #include <boost/date_time/gregorian/gregorian.hpp>
    
    int main(void)
    {
      auto ptr = boost::gregorian::greg_month::get_month_map_ptr();
    
      if (ptr)
      {
        auto it = ptr->begin();
        for(; it != ptr->end(); ++it)
        {
          std::cout << it->first << " " << it->second << '\n';
        }
      }
    }
    

    此映射包含所有短/长名称与创建greg_month 实例所需的短名称之间的映射。只需要围绕它创建一个小包装......

    根据 Graeme 的发现,有一个方便的函数可以包装这个 boost::date_time::month_str_to_ushort&lt;&gt;

    【讨论】:

    • 刚刚发现一个函数已经完成了这个:boost::date_time::month_str_to_ushort。感谢您的帮助!
    • @Graeme,啊哈 - 它必须在某个地方 - 这是一个非常微不足道的功能...... :)
    【解决方案2】:

    是的,有一些提升日期时间方面可用于创建语言环境并放入流中。

    请注意,如果您要打印或解析大量日期和时间,则不会为您解析的每个日期和时间创建构面和语言环境。

    查看here 获取有关输入日期的文档。他们的一些示例使用短月份名称,似乎将%b 作为其format specifier

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多