std::time_get<CharT,InputIt>::get_time,std::time_get<CharT,InputIt>::do_get_time
来自 cppreference.cn
定义于头文件 <locale> |
||
public: iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, |
(1) | |
protected: virtual iter_type do_get_time( iter_type beg, iter_type end, std::ios_base& str, |
(2) | |
1) 公共成员函数,调用最派生类的受保护虚成员函数
do_get_time
。2) 从序列
[
beg,
end)
读取连续字符,并按照格式说明符 "%H:%M:%S" 相同的规则解析出时间值,该格式说明符由函数 std::get_time、time_get::get 和 POSIX 函数 strptime()
使用。- 解析后的时间存储在参数 t 指向的 std::tm 结构的相应字段中。
- 如果在读取有效时间之前到达末尾迭代器,则该函数在 err 中设置 std::ios_base::eofbit。如果遇到解析错误,则该函数在 err 中设置 std::ios_base::failbit。
目录 |
[编辑] 参数
beg | - | 指示要解析的序列开始的迭代器 |
end | - | 超过要解析的序列末尾的迭代器 |
str | - | 此函数用于在需要时获取区域设置 facet 的流对象,例如 std::ctype 以跳过空格 |
err | - | 流错误标志对象,此函数会修改该对象以指示错误 |
t | - | 指向 std::tm 对象的指针,该对象将保存此函数调用的结果 |
[编辑] 返回值
迭代器,指向 [
beg,
end)
中被识别为有效日期一部分的最后一个字符之后的位置。
[编辑] 注释
对于默认时间格式的字母组件(如果有),此函数通常不区分大小写。
如果遇到解析错误,此函数的大多数实现都不会修改 *t。
[编辑] 示例
运行此代码
#include <iostream> #include <iterator> #include <locale> #include <sstream> void try_get_time(const std::string& s) { std::cout << "Parsing the time 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; std::time_get<char> const& facet = std::use_facet<std::time_get<char>>(str.getloc()); std::istreambuf_iterator<char> ret = facet.get_time({str}, {}, str, err, &t); str.setstate(err); if (str) { std::cout << "Hours: " << t.tm_hour << ", " "Minutes: " << t.tm_min << ", " "Seconds: " << t.tm_sec << '\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("ru_RU.utf8")); try_get_time("21:40:11"); try_get_time("21-40-11"); std::locale::global(std::locale("ja_JP.utf8")); try_get_time("21時37分58秒"); }
输出
Parsing the time out of '21:40:11' in the locale ru_RU.utf8 Hours: 21, Minutes: 40, Seconds: 11 Parsing the time out of '21-40-11' in the locale ru_RU.utf8 Parse failed. Unparsed string: -40-11 Parsing the time out of '21時37分58秒' in the locale ja_JP.utf8 Hours: 21, Minutes: 37, Seconds: 58
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 248 | C++98 | 到达末尾迭代器时未设置 eofbit |
如果尚未读取有效时间,则设置 eofbit |
LWG 461 | C++98 | do_get_time 需要解析本地化时间表示 |
使用 "%H:%M:%S" 格式解析 |
[编辑] 参见
(C++11) |
解析指定格式的日期/时间值 (函数模板) |