将字符串转换为boost::gregori::greg_month

Convert string to boost::gregorian::greg_month

本文关键字:gregori greg month boost 字符串 转换      更新时间:2023-10-16

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

一个棘手的解决方案可能是:

#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<>

是的,有一些提升日期-时间方面可以用于创建区域设置并放入流中。

但是要注意的是,如果要打印或解析大量的日期和时间,则不会为解析的每个日期和时间创建facet和locale。

请在此处查看有关输入日期的文档。他们的一些示例使用短月名称,该名称似乎以%b作为其格式说明符