std::time_get<CharT,InputIt>::get_weekday, std::time_get<CharT,InputIt>::do_get_weekday
來自 cppreference.com
定義於標頭檔案 <locale> |
||
public: iter_type get_weekday( iter_type beg, iter_type end, std::ios_base& str, |
(1) | |
protected: virtual iter_type do_get_weekday( iter_type beg, iter_type end, std::ios_base& str, |
(2) | |
1) 公有成員函式,呼叫最派生類的保護虛成員函式
do_get_weekday
。2) 從序列
[
beg,
end)
讀取連續字元,並解析出星期幾的名稱(可能縮寫),使用此區域設定期望的星期幾的預設格式,該格式與函式 std::get_time、time_get::get 和 POSIX 函式 strptime()
中使用的 "%a" 格式相同。如果它找到縮寫名稱,後跟對全名有效的字元,它會繼續讀取,直到它消耗掉全名的所有字元,或者找到一個不期望的字元,在這種情況下,即使前幾個字元是有效的縮寫,解析也會失敗。
解析後的星期幾儲存在 std::tm 欄位 t->tm_wday 中。
如果在讀取有效的星期幾名稱之前到達結束迭代器,則函式會在 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 <initializer_list> #include <iostream> #include <iterator> #include <locale> #include <sstream> #include <string_view> void try_get_wday(std::string_view s) { std::cout << "Parsing the weekday out of '" << s << "' in the locale " << std::locale().name() << '\n'; std::istringstream str{s.data()}; 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_weekday({str}, {}, str, err, &t); str.setstate(err); std::istreambuf_iterator<char> last{}; if (str) { std::cout << "Successfully parsed, weekday number is " << t.tm_wday; if (ret != last) { std::cout << " Remaining content: "; std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout)); } else std::cout << " the input was fully consumed"; } else { std::cout << "Parse failed. Unparsed string: "; std::copy(ret, last, std::ostreambuf_iterator<char>(std::cout)); } std::cout << '\n'; } void demo(std::string_view locale_name, std::initializer_list<std::string_view>&& data) { try { std::locale::global(std::locale(locale_name.data())); } catch (std::runtime_error const& ex) { std::cout << "Cannot setup locale: " << locale_name << "\n" "Exception: " << ex.what() << '\n'; return; } for (std::string_view const weekday : data) try_get_wday(weekday); } int main() { demo("lt_LT.utf8", {"Št", "Šeštadienis"}); demo("en_US.utf8", {"SATELLITE"}); demo("ja_JP.utf8", {"土曜日"}); }
可能的輸出
Parsing the weekday out of 'Št' in the locale lt_LT.utf8 Successfully parsed, weekday number is 6 the input was fully consumed Parsing the weekday out of 'Šeštadienis' in the locale lt_LT.utf8 Successfully parsed, weekday number is 6 the input was fully consumed Parsing the weekday out of 'SATELLITE' in the locale en_US.utf8 Successfully parsed, weekday number is 6 Remaining content: ELLITE Parsing the weekday out of '土曜日' in the locale ja_JP.utf8 Successfully parsed, weekday number is 6 the input was fully consumed
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 248 | C++98 | 到達結束迭代器時未設定 eofbit |
如果未讀取到有效的星期幾名稱,則設定 eofbit |
[編輯] 另請參閱
(C++11) |
解析指定格式的日期/時間值 (函式模板) |