名稱空間
變體
操作

std::time_get<CharT,InputIt>::get_year, std::time_get<CharT,InputIt>::do_get_year

來自 cppreference.com
< cpp‎ | locale‎ | time get
 
 
 
 
 
定義於標頭檔案 <locale>
public:

iter_type get_year( iter_type s, iter_type end, std::ios_base& str,

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

virtual iter_type do_get_year( iter_type s, iter_type end, std::ios_base& str,

                               std::ios_base::iostate& err, std::tm* t ) const;
(2)
1) 公有成員函式,呼叫派生程度最高的類中的受保護虛成員函式 do_get_year
2) 從序列 [begend) 讀取連續字元,並使用某個實現定義格式解析出年份。根據區域設定,可能接受兩位數年份,並且兩位數年份所屬的世紀是實現定義的。

解析的年份儲存在 std::tm 結構體欄位 t->tm_year 中。

若在讀取到有效年份前到達結束迭代器,則函式在 err 中設定 std::ios_base::eofbit。若遇到解析錯誤,則函式在 err 中設定 std::ios_base::failbit

目錄

[編輯] 引數

beg - 指定要解析序列起始的迭代器
end - 要解析序列的結束迭代器之後一個位置
str - 此函式在需要時用於獲取區域設定刻面的流物件,例如 std::ctype 用於跳過空白符或 std::collate 用於比較字串。
err - 由該函式修改以指示錯誤的流錯誤標誌物件
t - 指向將儲存此函式呼叫結果的 std::tm 物件的指標

[編輯] 返回值

指向 [begend) 中被識別為有效年份的一部分的最後一個字元之後一個位置的迭代器。

[編輯] 注意

對於兩位數輸入值,許多實現使用與 std::get_timestd::time_get::get() 和 POSIX 函式 strptime() 所用的轉換說明符 '%y' 相同的解析規則:預期兩位整數,範圍 [6999] 中的值導致 1969 到 1999 年,範圍 [0068] 中的值導致 2000 到 2068 年。四位輸入通常按原樣接受。

如果遇到解析錯誤,此函式的大多數實現會使 *t 保持不變。

[編輯] 示例

#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
 
void try_get_year(const std::string& s)
{
    std::cout << "Parsing the year 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::time_get<char> const& facet = std::use_facet<std::time_get<char>>(str.getloc());
    std::istreambuf_iterator<char> ret = facet.get_year({str}, {}, str, err, &t);
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
 
    if (str)
    {
        std::cout << "Successfully parsed, year is " << 1900 + t.tm_year;
 
        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()
{
    std::locale::global(std::locale("en_US.utf8"));
    try_get_year("13");
    try_get_year("2013");
 
    std::locale::global(std::locale("ja_JP.utf8"));
    try_get_year("2013年");
}

可能的輸出

Parsing the year out of '13' in the locale en_US.utf8
Successfully parsed, year is 2013 the input was fully consumed
Parsing the year out of '2013' in the locale en_US.utf8
Successfully parsed, year is 2013 the input was fully consumed
Parsing the year out of '2013年' in the locale ja_JP.utf8
Successfully parsed, year is 2013 Remaining content: 年

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

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

[編輯] 參閱

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