std::time_get<CharT,InputIt>::get_time, std::time_get<CharT,InputIt>::do_get_time
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <locale> で定義
|
||
public: iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const; |
(1) | |
protected: virtual iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const; |
(2) | |
1) public メンバ関数。 最も派生したクラスの protected virtual メンバ関数
do_get_time を呼びます。2) シーケンス
[beg, end) から連続する文字を読み込み、関数 std::get_time、 time_get::get および POSIX の関数 strptime() によって使用される以下の書式指定子と同じルールに従って時間の値を解析します。
'%X' |
(C++11未満) |
"%H:%M:%S" |
(C++11以上) |
解析された時間は引数
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 を変更しません。
例
Run this code
#include <iostream>
#include <locale>
#include <sstream>
#include <iterator>
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::istreambuf_iterator<char> ret =
std::use_facet<std::time_get<char>>(str.getloc()).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++11) |
指定された書式の日付/時刻の値をパースします (関数テンプレート) |