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 结构的相应字段中。
如果在读取有效日期之前到达结束迭代器,函数将 std::ios_base::eofbit 设置到 err 中。如果遇到解析错误,函数将 std::ios_base::failbit 设置到 err 中。
目录 |
[编辑部分:参数] 参数
beg | - | 指定要解析序列起始的迭代器 |
end | - | 要解析序列的结束迭代器之后一个位置 |
str | - | 一个流对象,当需要时,此函数用于获取区域设置刻面,例如 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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 248 | C++98 | 到达结束迭代器时未设置 eofbit |
如果未读取有效日期,则设置 eofbit |
LWG 461 | C++98 | do_get_date 需要解析本地化日期表示 |
使用由 date_order() 确定的格式进行解析 |
[编辑部分:另请参阅] 另请参阅
(C++11) |
解析指定格式的日期/时间值 (函数模板) |