std::time_get<CharT,InputIt>::get_date, std::time_get<CharT,InputIt>::do_get_date
来自cppreference.com
| 在标头 <locale> 定义
|
||
| |
(1) | |
| |
(2) | |
1) 公开成员函数,调用最终派生类的受保护虚成员函数
do_get_date。2) 从序列
[beg, end) 读取相继字符,并按与为函数 std::get_time、time_get::get 和 POSIX 函数 strptime() 所用的此本地环境所期待的默认格式解析日历日期值。默认格式如下:
| 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 | - | 此函数在需要时用以获得本地环境平面的流对象,例如用 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 << "在本地环境 " << std::locale().name()
<< " 中从 '" << s << "' 解析日期\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 << "年:" << t.tm_year + 1900 << ' '
<< "月:" << t.tm_mon + 1 << ' '
<< "日:" << t.tm_mday << '\n';
}
else
{
std::cout << "解析失败。尚未解析的字符串:";
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日");
}
输出:
在本地环境 en_US.utf8 中从 '02/01/2013' 解析日期
年:2013 月:2 日:1
在本地环境 en_US.utf8 中从 '02-01-2013' 解析日期
解析失败。尚未解析的字符串:-01-2013
在本地环境 ja_JP.utf8 中从 '2013年02月01日' 解析日期
年:2013 月:2 日:1
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 248 | C++98 | 在抵达尾迭代器时不会设置 eofbit
|
在没有读取到合法日期的情况下会设置 eofbit
|
| LWG 461 | C++98 | do_get_date 需要解析本地化的日期表示
|
以通过 date_order() 决定的格式解析 |
参阅
(C++11) |
剖析指定格式的日期/时间值 (函数模板) |