首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::time_get::get_year

Defined in header <locale>

public: iter_type do_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%29公共成员函数,调用受保护的虚拟成员函数。do_get_year最派生的类。

2%29从序列中读取连续字符。[beg, end)并使用一些实现定义的格式分析了一年。根据地区的不同,可以接受两位数的年份,并且实现定义了它们属于哪个世纪。

分析的年份存储在std::tm结构场t->tm_year...

如果在读取有效日期之前到达结束迭代器,则函数设置std::ios_base::eofbiterr如果遇到解析错误,则函数将std::ios_base::failbiterr...

参数

beg

-

iterator designating the start of the sequence to parse

end

-

one past the end iterator for the sequence to parse

str

-

a stream object that this function uses to obtain locale facets when needed, e.g. std::ctype to skip whitespace or std::collate to compare strings

err

-

stream error flags object that is modified by this function to indicate errors

t

-

pointer to the std::tm object that will hold the result of this function call

返回值

中的最后一个字符[beg, end)这被确认为有效年份的一部分。

注记

对于两位数的输入值,许多实现使用与转换说明符相同的解析规则。'%y'std::get_time,,,std::time_get::get(),以及POSIX函数strptime()::两位数的整数,在范围内的值。69,99结果值为1969至1999,范围00682000至2068年度结果。四位数字输入通常被接受为-原样.

如果遇到解析错误,则此函数的大多数实现将离开。*t未经修改。

二次

代码语言:javascript
复制
#include <iostream>
#include <locale>
#include <sstream>
#include <iterator>
 
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::istreambuf_iterator<char> ret =
        std::use_facet<std::time_get<char>>(str.getloc()).get_year(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            str, err, &t
        );
    str.setstate(err);
    if (str) {
        std::cout << "Successfully parsed, year is " << 1900 + t.tm_year;
        if (ret != std::istreambuf_iterator<char>()) {
            std::cout << " Remaining content: ";
            std::copy(ret, std::istreambuf_iterator<char>(),
                      std::ostreambuf_iterator<char>(std::cout));
        } else {
            std::cout << " the input was fully consumed";
        }
    } else {
        std::cout << "Parse failed. Unparsed string: ";
        std::copy(ret, std::istreambuf_iterator<char>(),
                  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年");
}

二次

可能的产出:

二次

代码语言:javascript
复制
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: 年

二次

另见

get_time (C++11)

parses a date/time value of specified format (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券