Namespaces
Variants

std::time_get<CharT,InputIt>:: get_monthname, std::time_get<CharT,InputIt>:: do_get_monthname

From cppreference.net
定义于头文件 <locale>
public :

iter_type get_monthname ( iter_type beg, iter_type end, std:: ios_base & str,

std:: ios_base :: iostate & err, std:: tm * t ) const ;
(1)
protected :

virtual iter_type do_get_monthname ( iter_type beg, iter_type end, std:: ios_base & str,

std:: ios_base :: iostate & err, std:: tm * t ) const ;
(2)
1) 公开成员函数,调用最终派生类的受保护虚成员函数 do_get_monthname
2) 从序列 [ beg , end ) 中连续读取字符并解析出月份名称(可能为缩写),使用该本地环境预期的默认月份名称格式,该格式与 "%b" 格式相同,被 std::get_time time_get::get 函数以及 POSIX 函数 strptime() 所使用。

如果它找到缩写名称,后跟适用于完整名称的有效字符,它会继续读取,直到消耗完完整名称的所有字符或找到不符合预期的字符,此时解析将失败,即使前几个字符是有效的缩写。

解析出的月份存储在 std::tm 字段 t - > tm_mon 中。

如果在读取到有效月份名称之前到达结束迭代器,该函数将在 err 中设置 std::ios_base::eofbit 。如果遇到解析错误,该函数将在 err 中设置 std::ios_base::failbit

目录

参数

beg - 指向待解析序列起始位置的迭代器
end - 指向待解析序列末尾后一位的迭代器
str - 流对象,本函数在需要时通过该对象获取本地化刻面,例如使用 std::ctype 跳过空白字符,或使用 std::collate 进行字符串比较
err - 流错误标志对象,本函数通过修改该对象来指示错误状态
t - 指向 std::tm 对象的指针,用于存储本函数调用的解析结果

返回值

指向在 [ beg , end ) 范围内被识别为有效月份名称的最后一个字符之后位置的迭代器。

注释

此函数通常不区分大小写。

如果遇到解析错误,此函数的大多数实现会保持 * t 不变。

示例

#include <ctime>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
#include <string_view>
void try_get_mon(std::string_view locale_name, std::string_view source)
{
    try
    {
        std::locale::global(std::locale(locale_name.data()));
    }
    catch (std::runtime_error const& ex)
    {
        std::cout << "Cannot setup locale: " << locale_name << "\n"
                     "Exception: " << ex.what() << '\n';
        return;
    }
    std::cout << "Parsing the month out of '" << source
              << "' in the locale " << std::locale().name() << '\n';
    std::istringstream str{source.data()};
    std::ios_base::iostate err = std::ios_base::goodbit;
    std::tm t;
    std::time_get<char> const& facet = std::use_facet<std::time_get<char>>(str.getloc());
    std::istreambuf_iterator<char> ret = facet.get_monthname({str}, {}, str, err, &t);
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
    if (str)
    {
        std::cout << "Successfully parsed, month number is " << t.tm_mon;
        if (ret != last)
        {
            std::cout << ". Remaining content: ";
            std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
        }
        else
            std::cout << ". The input was fully consumed";
    }
    else
    {
        std::cout << "Parse failed. Unparsed string: ";
        std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout));
    }
    std::cout << '\n';
}
int main()
{
    try_get_mon("ja_JP.utf8", "2月");
    try_get_mon("th_TH.utf8", "กุมภาพันธ์");
    try_get_mon("el_GR.utf8", "Φεβ");
    try_get_mon("el_GR.utf8", "Φεβρουάριος");
    try_get_mon("en_US.utf8", "Febrile");
}

可能的输出:

Parsing the month out of '2月' in the locale ja_JP.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'กุมภาพันธ์' in the locale th_TH.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβ' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβρουάριος' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Febrile' in the locale en_US.utf8
Parse failed. Unparsed string: ile

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 248 C++98 到达结束迭代器时未设置 eofbit 若未读取到有效月份名称则设置 eofbit

参见

(C++11)
解析指定格式的日期/时间值
(函数模板)