名前空間
変種
操作

std::time_get<CharT,InputIt>::get_date, std::time_get<CharT,InputIt>::do_get_date

From cppreference.com
< cpp‎ | locale‎ | time get
 
 
 
 
 
ヘッダー <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) 公開メンバ関数。最も派生したクラスの保護仮想メンバ関数do_get_dateを呼び出します。
2) シーケンス[begend)から連続する文字を読み込み、このロケールで期待されるデフォルトのフォーマットを使用してカレンダー日付値を解析します。これは次のように決定されます。
date_order() フォーマット
no_order "%m/%d/%y"
dmy "%d/%m/%y"
mdy "%m/%d/%y"
ymd "%y/%m/%d"
ydm "%y/%d/%m"
std::get_time()get()、およびPOSIX関数strptime()で使用されるものと同様です。
解析された日付は、引数tが指すstd::tm構造体の対応するフィールドに格納されます。
有効な日付が読み込まれる前に終端イテレータに到達した場合、関数はerrstd::ios_base::eofbitを設定します。解析エラーが発生した場合、関数はerrstd::ios_base::failbitを設定します。

目次

[編集] パラメータ

beg - 解析するシーケンスの開始を示すイテレータ
end - 解析するシーケンスの終了の次のイテレータ
str - 必要に応じてロケールファセット(例えば、空白をスキップするための std::ctype や文字列を比較するための std::collate)を取得するためにこの関数が使用するストリームオブジェクト
err - この関数によってエラーを示すために変更されるストリームエラーフラグオブジェクト
t - この関数の呼び出しの結果を保持する std::tm オブジェクトへのポインタ

[編集] 戻り値

有効な日付の一部として認識されたシーケンス[begend)の最後の文字の次の位置を指すイテレータ。

[編集] 注記

デフォルトの日付フォーマットのアルファベット成分(もしあれば)について、この関数は通常大文字小文字を区別しません。

解析エラーが発生した場合、この関数のほとんどの実装では *t は変更されません。

実装は、標準で要求されるもの以外のフォーマットもサポートする場合があります。

[編集]

#include <ctime>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
 
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;
    const std::time_get<char>& facet = std::use_facet<std::time_get<char>>(str.getloc());
    std::istreambuf_iterator<char> ret = facet.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++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 248 C++98 end iterator に到達したときに eofbit が設定されなかった 有効な日付が読み取られなかった場合にeofbitを設定する
LWG 461 C++98 ロケール化された日付表現を解析するためにdo_get_dateが必要 date_order()によって決定されるフォーマットで解析する

[編集] 関連項目

(C++11)
指定されたフォーマットの日付/時刻の値を構文解析する
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)