std::time_get<CharT,InputIt>::get_time, std::time_get<CharT,InputIt>::do_get_time
來自 cppreference.com
定義於標頭檔案 <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)
讀取連續字元,並按照與函式 std::get_time、time_get::get 和 POSIX 函式 strptime()
所使用的格式說明符 "%H:%M:%S" 相同的規則解析時間值。- 解析後的時間儲存在引數 t 指向的 std::tm 結構的相應欄位中。
- 如果在讀取有效時間之前到達結束迭代器,函式會在 err 中設定 std::ios_base::eofbit。如果遇到解析錯誤,函式會在 err 中設定 std::ios_base::failbit。
目錄 |
[編輯] 引數
beg | - | 指定要解析序列起始的迭代器 |
end | - | 要解析序列的結束迭代器之後一個位置 |
str | - | 此函式在需要時用於獲取區域設定面的流物件,例如 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 248 | C++98 | 到達結束迭代器時未設定 eofbit |
如果未讀取有效時間,則設定 eofbit |
LWG 461 | C++98 | do_get_time 需要解析本地化時間表示 |
使用 "%H:%M:%S" 格式進行解析 |
[編輯] 參閱
(C++11) |
解析指定格式的日期/時間值 (函式模板) |