std::time_get<CharT,InputIt>::get_year、std::time_get<CharT,InputIt>::do_get_year
From cppreference.com
| ヘッダー <locale> で定義 |
||
| public: iter_type get_year( iter_type s, iter_type end, std::ios_base& str, |
(1) | |
| protected: virtual iter_type do_get_year( iter_type s, iter_type end, std::ios_base& str, |
(2) | |
1) public メンバ関数。最も派生したクラスの保護された仮想メンバ関数
do_get_yearを呼び出します。2) シーケンス
[beg, end)から連続する文字を読み取り、何らかの実装定義フォーマットを使用して年を解析します。ロケールによっては、2桁の年が受け入れられる場合があり、どの世紀に属するかは実装定義です。解析された年はstd::tm構造体のフィールドt->tm_yearに格納されます。
有効な年が読み取られる前に end イテレータに達した場合、関数は err に std::ios_base::eofbit を設定します。解析エラーが発生した場合、関数は err に std::ios_base::failbit を設定します。
目次 |
[編集] パラメータ
| beg | - | 解析するシーケンスの開始を示すイテレータ |
| end | - | 解析するシーケンスの終了の次のイテレータ |
| str | - | 必要に応じてロケールファセット(例えば、空白をスキップするための std::ctype や文字列を比較するための std::collate)を取得するためにこの関数が使用するストリームオブジェクト |
| err | - | この関数によってエラーを示すために変更されるストリームエラーフラグオブジェクト |
| t | - | この関数の呼び出しの結果を保持する std::tm オブジェクトへのポインタ |
[編集] 戻り値
有効な年の一部として認識された、[beg, end) の最後の文字の次の位置を指すイテレータ。
[編集] 注記
2桁の入力値については、多くの実装では、std::get_time、std::time_get::get()、および POSIX 関数 strptime() で使用される変換指定子'%y'と同じ解析ルールを使用しています。2桁の整数が期待され、範囲[69, 99]の値は1969年から1999年になり、範囲[00, 68]の値は2000年から2068年になります。4桁の入力は通常そのまま受け入れられます。
解析エラーが発生した場合、この関数のほとんどの実装では *t は変更されません。
[編集] 例
このコードを実行
#include <iostream> #include <iterator> #include <locale> #include <sstream> void try_get_year(const std::string& s) { std::cout << "Parsing the year 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_year({str}, {}, str, err, &t); str.setstate(err); std::istreambuf_iterator<char> last{}; if (str) { std::cout << "Successfully parsed, year is " << 1900 + t.tm_year; 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'; } int main() { std::locale::global(std::locale("en_US.utf8")); try_get_year("13"); try_get_year("2013"); std::locale::global(std::locale("ja_JP.utf8")); try_get_year("2013年"); }
実行結果の例
Parsing the year out of '13' in the locale en_US.utf8 Successfully parsed, year is 2013 the input was fully consumed Parsing the year out of '2013' in the locale en_US.utf8 Successfully parsed, year is 2013 the input was fully consumed Parsing the year out of '2013年' in the locale ja_JP.utf8 Successfully parsed, year is 2013 Remaining content: 年
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 248 | C++98 | end iterator に到達したときに eofbit が設定されなかった |
有効な年が読み取られなかった場合にeofbitを設定します |
[編集] 関連項目
| (C++11) |
指定されたフォーマットの日付/時刻の値を構文解析する (関数テンプレート) |