std::time_get<CharT,InputIt>::get_date, std::time_get<CharT,InputIt>::do_get_date
來自 cppreference.com
定義於標頭檔案 <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) |
解析指定格式的日期/時間值 (函式模板) |