std::time_get<CharT,InputIt>::get_date, std::time_get<CharT,InputIt>::do_get_date
来自 cppreference.cn
定义于头文件 <locale> |
||
public: iter_type get_date( iter_type beg, iter_type end, std::ios_base& str, |
(1) | |
protected: virtual iter_type do_get_date( iter_type beg, iter_type end, std::ios_base& str, |
(2) | |
1) 公有成员函数,调用最派生类的受保护虚成员函数
do_get_date
。2) 从序列
[
beg,
end)
中读取连续字符,并使用此区域设置期望的默认格式解析日历日期值,该格式确定为date_order() | 格式 |
---|---|
no_order
|
"%m/%d/%y" |
dmy
|
"%d/%m/%y" |
mdy
|
"%m/%d/%y" |
ymd
|
"%y/%m/%d" |
ydm
|
"%y/%d/%m" |
解析的日期存储在参数 t 指向的 std::tm 结构的相应字段中。
如果在读取有效日期之前到达末尾迭代器,则此函数在 err 中设置 std::ios_base::eofbit。 如果遇到解析错误,则此函数在 err 中设置 std::ios_base::failbit。
内容 |
[编辑] 参数
beg | - | 指示要解析的序列开始位置的迭代器 |
end | - | 要解析的序列的末尾迭代器之后的位置 |
str | - | 流对象,此函数使用它在需要时获取区域设置 facet,例如 std::ctype 以跳过空格或 std::collate 以比较字符串 |
err | - | 流错误标志对象,此函数会修改它以指示错误 |
t | - | 指向 std::tm 对象的指针,该对象将保存此函数调用的结果 |
[编辑] 返回值
迭代器,指向 [
beg,
end)
中被识别为有效日期一部分的最后一个字符之后的位置。
[编辑] 注解
对于默认日期格式的字母组件(如果有),此函数通常不区分大小写。
如果遇到解析错误,则此函数的大多数实现会将 *t 保留为未修改状态。
该实现可能支持标准要求的格式之外的其他日期格式。
[编辑] 示例
运行此代码
#include <ctime> #include <iostream> #include <iterator> #include <locale> #include <sstream> void try_get_date(const std::string& s) { std::cout << "Parsing the date out of '" << s << "' in the locale " << std::locale().name() << '\n'; std::istringstream str(s); std::ios_base::iostate err = std::ios_base::goodbit; std::tm t; const std::time_get<char>& facet = std::use_facet<std::time_get<char>>(str.getloc()); std::istreambuf_iterator<char> ret = facet.get_date({str}, {}, str, err, &t); str.setstate(err); if (str) { std::cout << "Day: " << t.tm_mday << ' ' << "Month: " << t.tm_mon + 1 << ' ' << "Year: " << t.tm_year + 1900 << '\n'; } else { std::cout << "Parse failed. Unparsed string: "; std::copy(ret, {}, std::ostreambuf_iterator<char>(std::cout)); std::cout << '\n'; } } int main() { std::locale::global(std::locale("en_US.utf8")); try_get_date("02/01/2013"); try_get_date("02-01-2013"); std::locale::global(std::locale("ja_JP.utf8")); try_get_date("2013年02月01日"); }
输出
Parsing the date out of '02/01/2013' in the locale en_US.utf8 Day: 1 Month: 2 Year: 2013 Parsing the date out of '02-01-2013' in the locale en_US.utf8 Parse failed. Unparsed string: -01-2013 Parsing the date out of '2013年02月01日' in the locale ja_JP.utf8 Day: 1 Month: 2 Year: 2013
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 248 | C++98 | 到达末尾迭代器时未设置 eofbit |
如果尚未读取有效日期,则设置 eofbit |
LWG 461 | C++98 | do_get_date 需要解析本地化的日期表示 |
使用由 date_order() 确定的格式进行解析 |
[编辑] 参见
(C++11) |
解析指定格式的日期/时间值 (函数模板) |