【发布时间】:2012-02-24 13:44:44
【问题描述】:
在 Boost 日期时间库中,是否有用于将月份短字符串(例如 Jan、Feb、Mar、Apr)转换为 boost::gregorian::greg_month 类型的实用函数?该库的文档不是很好,我在标题中看不到任何内容。
【问题讨论】:
标签: c++ boost-date-time
在 Boost 日期时间库中,是否有用于将月份短字符串(例如 Jan、Feb、Mar、Apr)转换为 boost::gregorian::greg_month 类型的实用函数?该库的文档不是很好,我在标题中看不到任何内容。
【问题讨论】:
标签: c++ boost-date-time
一个 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<>
【讨论】:
是的,有一些提升日期时间方面可用于创建语言环境并放入流中。
请注意,如果您要打印或解析大量日期和时间,则不会为您解析的每个日期和时间创建构面和语言环境。
查看here 获取有关输入日期的文档。他们的一些示例使用短月份名称,似乎将%b 作为其format specifier
【讨论】: