名前空間
変種
操作

std::time_get<CharT,InputIt>::get_monthname、std::time_get<CharT,InputIt>::do_get_monthname

From cppreference.com
< cpp‎ | locale‎ | time get
 
 
 
 
 
ヘッダー <locale> で定義
public:

iter_type get_monthname( iter_type beg, iter_type end, std::ios_base& str,

                         std::ios_base::iostate& err, std::tm* t ) const;
(1)
protected:

virtual do_get_monthname( iter_type beg, iter_type end, std::ios_base& str,

                                    std::ios_base::iostate& err, std::tm* t ) const;
(2)
1) Public member function, calls the protected virtual member function do_get_monthname of the most derived class.
2) シーケンス [begend) から successive characters を読み込み、このロケールで期待される月名のデフォルトフォーマット("%b" と同じフォーマット)を使用して、月名(省略形の場合もある)を解析します。これは、std::get_timetime_get::get、および POSIX 関数 strptime() で使用されるものと同じです。

省略形名に続いて、完全名として有効な文字が見つかった場合、完全名のすべての文字を消費するか、予期しない文字が見つかるまで読み込みを続けます。後者の場合、最初の数文字が有効な省略形であったとしても、解析は失敗します。

解析された月は、std::tm のフィールド t->tm_mon に格納されます。

有効な月名が読み込まれる前に end iterator に到達した場合、関数は errstd::ios_base::eofbit を設定します。解析エラーが発生した場合、関数は errstd::ios_base::failbit を設定します。

目次

[edit] Parameters

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

[edit] Return value

有効な月名の一部として認識された、[begend) 内の最後の文字の次のイテレータ。

[edit] Notes

この関数は通常、大文字小文字を区別しません。

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

[edit] Example

#include <ctime>
#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
#include <string_view>
 
void try_get_mon(std::string_view locale_name, std::string_view source)
{
    try
    {
        std::locale::global(std::locale(locale_name.data()));
    }
    catch (std::runtime_error const& ex)
    {
        std::cout << "Cannot setup locale: " << locale_name << "\n"
                     "Exception: " << ex.what() << '\n';
        return;
    }
 
    std::cout << "Parsing the month out of '" << source
              << "' in the locale " << std::locale().name() << '\n';
    std::istringstream str{source.data()};
    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_monthname({str}, {}, str, err, &t);
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
 
    if (str)
    {
        std::cout << "Successfully parsed, month number is " << t.tm_mon;
 
        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()
{
    try_get_mon("ja_JP.utf8", "2月");
    try_get_mon("th_TH.utf8", "กุมภาพันธ์");
    try_get_mon("el_GR.utf8", "Φεβ");
    try_get_mon("el_GR.utf8", "Φεβρουάριος");
    try_get_mon("en_US.utf8", "Febrile");
}

実行結果の例

Parsing the month out of '2月' in the locale ja_JP.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'กุมภาพันธ์' in the locale th_TH.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβ' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβρουάριος' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Febrile' in the locale en_US.utf8
Parse failed. Unparsed string: ile

[edit] Defect reports

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 248 C++98 end iterator に到達したときに eofbit が設定されなかった 有効な月名が読み込まれなかった場合に eofbit を設定する

[edit] See also

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