命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | locale‎ | time get
 
 
 
 
 
定义于头文件 <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) 从序列 [begend) 读取连续字符,并解析出月份名称(可能缩写),使用此区域设置预期的默认月份名称格式,该格式与函数 std::get_timetime_get::get 和 POSIX 函数 strptime() 中使用的 "%b" 格式相同。

如果它找到缩写名称,后跟对完整名称有效的字符,它会继续读取,直到消耗掉完整名称的所有字符,或者找到一个不预期的字符,在这种情况下,即使前几个字符是有效的缩写,解析也会失败。

解析出的月份存储在 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 对象的指针

[编辑] 返回值

指向 [begend) 中被识别为有效月份名称一部分的最后一个字符之后一个位置的迭代器。

[编辑] 注意

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

如果遇到解析错误,此函数的大多数实现会使 *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)
解析指定格式的日期/时间值
(函数模板) [编辑]