std::time_get<CharT,InputIt>::get_monthname, std::time_get<CharT,InputIt>::do_get_monthname
来自 cppreference.cn
定义于头文件 <locale> |
||
public: iter_type get_monthname( iter_type beg, iter_type end, std::ios_base& str, |
(1) | |
protected: virtual iter_type do_get_monthname( iter_type beg, iter_type end, std::ios_base& str, |
(2) | |
1) 公有成员函数,调用最派生类的受保护虚成员函数
do_get_monthname
。2) 从序列
[
beg,
end)
读取连续字符并解析出月份名称(可能为缩写形式),使用此区域设置期望的月份名称的默认格式,该格式与函数 std::get_time、time_get::get 和 POSIX 函数 strptime()
使用的 "%b" 格式相同。如果找到缩写名称,后跟对完整名称有效的字符,它将继续读取,直到它消耗掉完整名称的所有字符或找到一个意外字符,在这种情况下,即使前几个字符是有效的缩写,解析也会失败。
解析后的月份存储在 std::tm 字段 t->tm_mon 中。
如果在读取有效月份名称之前到达末尾迭代器,则该函数在 err 中设置 std::ios_base::eofbit。 如果遇到解析错误,则该函数在 err 中设置 std::ios_base::failbit。
目录 |
[edit] 参数
beg | - | 指示要解析序列开始位置的迭代器 |
end | - | 指示要解析序列末尾后一个位置的迭代器 |
str | - | 流对象,此函数使用它在需要时获取区域设置 facet,例如 std::ctype 以跳过空格或 std::collate 以比较字符串 |
err | - | 流错误标志对象,此函数会修改它以指示错误 |
t | - | 指向 std::tm 对象的指针,该对象将保存此函数调用的结果 |
[edit] 返回值
迭代器,指向 [
beg,
end)
中被识别为有效月份名称一部分的最后一个字符的后一个位置。
[edit] 注解
此函数通常不区分大小写。
如果遇到解析错误,则此函数的大多数实现都会保持 *t 不被修改。
[edit] 示例
运行此代码
#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
[edit] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 248 | C++98 | 到达末尾迭代器时未设置 eofbit |
如果尚未读取有效的月份名称,则设置 eofbit |
[edit] 参见
(C++11) |
解析指定格式的日期/时间值 (函数模板) |