Namespaces
Variants

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

From cppreference.net
ヘッダーで定義 <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 iter_type do_get_monthname ( iter_type beg, iter_type end, std:: ios_base & str,

std:: ios_base :: iostate & err, std:: tm * t ) const ;
(2)
1) パブリックメンバー関数。最も派生したクラスの保護された仮想メンバー関数 do_get_monthname を呼び出す。
2) シーケンスから連続する文字を読み込み [ beg , end ) の範囲で月名(省略形も含む)を解析します。このロケールが期待するデフォルトの月名フォーマットを使用し、これは "%b" と同じフォーマットです。このフォーマットは std::get_time time_get::get 関数、およびPOSIX関数 strptime() で使用されるものと同じです。

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

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

有効な月名が読み込まれる前に終端イテレータに到達した場合、この関数は std::ios_base::eofbit err に設定します。パースエラーが発生した場合、この関数は std::ios_base::failbit err に設定します。

目次

パラメータ

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

戻り値

[ beg , end ) の範囲内で有効な月名の一部として認識された最後の文字の次を指すイテレータ。

注記

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

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

#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

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 248 C++98 eofbit は終端イテレータに到達しても設定されなかった 有効な月名が読み込まれなかった場合に eofbit を設定する

関連項目

(C++11)
指定されたフォーマットの日付/時刻値を解析する
(関数テンプレート)