std::time_get<CharT,InputIt>::get_weekday, std::time_get<CharT,InputIt>::do_get_weekday
From 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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 248 | C++98 | end iterator に到達したときに eofbit が設定されなかった |
有効な曜日名が読み込まれなかった場合にeofbitを設定します。 |
[編集] 関連項目
| (C++11) |
指定されたフォーマットの日付/時刻の値を構文解析する (関数テンプレート) |