名稱空間
變體
操作

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

來自 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 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) 從序列 [begend) 讀取連續字元,並解析出月份名稱(可能縮寫),使用此區域設定預期的預設月份名稱格式,該格式與函式 std::get_timetime_get::get 和 POSIX 函式 strptime() 中使用的 "%b" 格式相同。

如果它找到縮寫名稱,後跟對完整名稱有效的字元,它會繼續讀取,直到消耗掉完整名稱的所有字元,或者找到一個不預期的字元,在這種情況下,即使前幾個字元是有效的縮寫,解析也會失敗。

解析出的月份儲存在 std::tm 欄位 t->tm_mon 中。

如果在讀取有效的月份名稱之前到達結束迭代器,則函式會在 err 中設定 std::ios_base::eofbit。如果遇到解析錯誤,則函式會在 err 中設定 std::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>
#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++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 248 C++98 到達結束迭代器時未設定 eofbit 如果未讀取到有效的月份名稱,則設定 eofbit

[編輯] 另請參閱

(C++11)
解析指定格式的日期/時間值
(函式模板) [編輯]