std::time_get<CharT,InputIt>::get_date, std::time_get<CharT,InputIt>::do_get_date
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <locale> で定義
|
||
public: iter_type get_date( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t ) const; |
(1) | |
protected: virtual iter_type do_get_date( 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_date() を呼びます。2) シーケンス
[beg, end) から連続する文字を読み込み、このロケールによって期待されるデフォルトの書式を用いて、カレンダーの日付の値を解析します。 デフォルトの書式は関数 std::get_time()、 get() および POSIX の関数 strptime() によって使用される以下の書式と同じです。
"%x" |
(C++11未満) |
date_order() に応じて "%d/%m/%y"、 "%m/%d/%y"、 "%y/%m/%d" および "%y/%d/%m" |
(C++11以上) |
解析された日付は引数
t の指す std::tm 構造体の対応するフィールドに格納されます。 有効な日付が読み込まれる前に終端イテレータに達した場合、この関数は
err に std::ios_base::eofbit をセットします。 解析エラーに遭遇した場合、この関数は err に std::ios_base::failbit をセットします。引数
| beg | - | 解析するシーケンスの開始を指定するイテレータ |
| end | - | 解析するシーケンスの終端イテレータ |
| str | - | 必要なときにロケールのファセットを取得するためのこの関数が使用するストリームオブジェクト (例えばホワイトスペースをスキップするための std::ctype や文字列を比較するための std::collate) |
| err | - | エラーを示すためのこの関数によって変更されるストリームエラーフラグオブジェクト |
| t | - | この関数呼び出しの結果を保持する std::tm オブジェクトへのポインタ |
戻り値
有効な日付の一部として認識された [beg, end) 内の最後の文字の次を指すイテレータ。
ノート
デフォルトの日付の書式のアルファベットの部分 (もしあれば) について、この関数は通常、大文字小文字を区別しません。
解析エラーに遭遇した場合、この関数のほとんどの実装は *t を変更しません。
処理系は標準が要求する以外の日付の書式をサポートするかもしれません。
例
Run this code
#include <iostream>
#include <locale>
#include <sstream>
#include <iterator>
#include <ctime>
void try_get_date(const std::string& s)
{
std::cout << "Parsing the date 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_date(
{str}, {}, str, err, &t
);
str.setstate(err);
if(str) {
std::cout << "Day: " << t.tm_mday << ' '
<< "Month: " << t.tm_mon + 1 << ' '
<< "Year: " << t.tm_year + 1900 << '\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("en_US.utf8"));
try_get_date("02/01/2013");
try_get_date("02-01-2013");
std::locale::global(std::locale("ja_JP.utf8"));
try_get_date("2013年02月01日");
}
出力:
Parsing the date out of '02/01/2013' in the locale en_US.utf8
Day: 1 Month: 2 Year: 2013
Parsing the date out of '02-01-2013' in the locale en_US.utf8
Parse failed. Unparsed string: -01-2013
Parsing the date out of '2013年02月01日' in the locale ja_JP.utf8
Day: 1 Month: 2 Year: 2013
関連項目
(C++11) |
指定された書式の日付/時刻の値をパースします (関数テンプレート) |