std::time_get<CharT,InputIt>::get_weekday, std::time_get<CharT,InputIt>::do_get_weekday
来自 cppreference.cn
定义于头文件 <locale> |
||
public: iter_type get_weekday( iter_type beg, iter_type end, std::ios_base& str, |
(1) | |
protected: virtual iter_type do_get_weekday( iter_type beg, iter_type end, std::ios_base& str, |
(2) | |
1) 公有成员函数,调用最派生类的受保护虚成员函数
do_get_weekday
。2) 从序列
[
beg,
end)
中读取连续字符,并解析出星期名称(可能为缩写形式),使用此区域设置预期的星期默认格式,该格式与函数 std::get_time, time_get::get, 和 POSIX 函数 strptime()
使用的 "%a"
格式相同。如果找到缩写名称,后跟对完整名称有效的字符,它将继续读取,直到消耗完整名称的所有字符或找到意外字符,在这种情况下,即使前几个字符是有效的缩写,解析也会失败。
解析后的星期几存储在 std::tm 字段 t->tm_wday
中。
如果在读取有效的星期名称之前到达结束迭代器,则该函数在 err 中设置 std::ios_base::eofbit。如果遇到解析错误,则该函数在 err 中设置 std::ios_base::failbit。
内容 |
[编辑] 参数
beg | - | 指示要解析的序列的开始迭代器 |
end | - | 要解析的序列的末尾迭代器之后的位置 |
str | - | 此函数在需要时用于获取区域设置 facets 的流对象,例如 std::ctype 用于跳过空格,或 std::collate 用于比较字符串 |
err | - | 流错误标志对象,此函数会修改它以指示错误 |
t | - | 指向 std::tm 对象的指针,该对象将保存此函数调用的结果 |
[编辑] 返回值
迭代器,指向 [
beg,
end)
中被识别为有效星期名称一部分的最后一个字符之后的位置。
[编辑] 注解
此函数通常不区分大小写。
如果遇到解析错误,则此函数的大多数实现会保持 *t
不被修改。
[编辑] 示例
运行此代码
#include <initializer_list> #include <iostream> #include <iterator> #include <locale> #include <sstream> #include <string_view> void try_get_wday(std::string_view s) { std::cout << "Parsing the weekday out of '" << s << "' in the locale " << std::locale().name() << '\n'; std::istringstream str{s.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_weekday({str}, {}, str, err, &t); str.setstate(err); std::istreambuf_iterator<char> last{}; if (str) { std::cout << "Successfully parsed, weekday number is " << t.tm_wday; 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'; } void demo(std::string_view locale_name, std::initializer_list<std::string_view>&& data) { 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; } for (std::string_view const weekday : data) try_get_wday(weekday); } int main() { demo("lt_LT.utf8", {"Št", "Šeštadienis"}); demo("en_US.utf8", {"SATELLITE"}); demo("ja_JP.utf8", {"土曜日"}); }
可能的输出
Parsing the weekday out of 'Št' in the locale lt_LT.utf8 Successfully parsed, weekday number is 6 the input was fully consumed Parsing the weekday out of 'Šeštadienis' in the locale lt_LT.utf8 Successfully parsed, weekday number is 6 the input was fully consumed Parsing the weekday out of 'SATELLITE' in the locale en_US.utf8 Successfully parsed, weekday number is 6 Remaining content: ELLITE Parsing the weekday out of '土曜日' in the locale ja_JP.utf8 Successfully parsed, weekday number is 6 the input was fully consumed
[编辑] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 248 | C++98 | 到达末尾迭代器时未设置 eofbit |
如果尚未读取有效的星期名称,则设置 eofbit |
[编辑] 参见
(C++11) |
解析指定格式的日期/时间值 (函数模板) |