std::time_get<CharT,InputIt>:: get_time, std::time_get<CharT,InputIt>:: do_get_time
|
定義先ヘッダ
<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) | |
do_get_time
を呼び出す。
[
beg
,
end
)
の範囲で、
"%H:%M:%S"
フォーマット指定子と同じルールに従って時間値を解析します。このルールは、
std::get_time
、
time_get::get
、およびPOSIX関数
strptime()
で使用されるものと同じです。
-
- 解析された時刻は、引数 t が指す std::tm 構造体の対応するフィールドに格納されます。
-
- 有効な時刻が読み込まれる前に終端イテレータに到達した場合、この関数は std::ios_base::eofbit を err に設定します。パースエラーが発生した場合、この関数は std::ios_base::failbit を err に設定します。
目次 |
パラメータ
| 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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 248 | C++98 |
eofbit
が終端イテレータ到達時に設定されなかった
|
有効な時刻が読み込まれなかった場合に
eofbit
を設定する
|
| LWG 461 | C++98 |
do_get_time
はローカライズされた時刻表現の解析が必要だった
|
"%H:%M:%S" フォーマットで解析する |
関連項目
|
(C++11)
|
指定されたフォーマットの日付/時刻値を解析する
(関数テンプレート) |