Namespaces
Variants

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

From cppreference.net
헤더에 정의됨 <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) Public 멤버 함수로서, 가장 파생된 클래스의 protected virtual 멤버 함수 do_get_year 를 호출합니다.
2) 시퀀스 [ beg , end ) 에서 연속적인 문자들을 읽어 구현체 정의 형식을 사용하여 연도를 파싱합니다. 로케일에 따라 두 자리 연도가 허용될 수 있으며, 어떤 세기에 속하는지는 구현체 정의입니다.

파싱된 연도는 std::tm 구조체 필드 t - > tm_year 에 저장됩니다.

유효한 연도를 읽기 전에 끝 반복자에 도달하면, 함수는 std::ios_base::eofbit err 에 설정합니다. 파싱 오류가 발생하면, 함수는 std::ios_base::failbit err 에 설정합니다.

목차

매개변수

beg - 파싱할 시퀀스의 시작을 지정하는 반복자
end - 파싱할 시퀀스의 끝 바로 다음을 가리키는 반복자
str - 이 함수가 필요할 때 로케일 패싯을 얻기 위해 사용하는 스트림 객체, 예를 들어 std::ctype 으로 공백을 건너뛰거나 std::collate 으로 문자열을 비교할 때 사용됨
err - 이 함수에 의해 오류를 표시하기 위해 수정되는 스트림 오류 플래그 객체
t - 이 함수 호출의 결과를 담을 std::tm 객체에 대한 포인터

반환값

유효한 연도의 일부로 인식된 문자 범위 [ beg , end ) 에서 마지막 문자 바로 다음을 가리키는 반복자.

참고 사항

두 자리 입력 값의 경우, 많은 구현체들이 std::get_time , std::time_get::get() , 그리고 POSIX 함수 strptime() 에서 사용하는 변환 지정자 '%y' 와 동일한 구문 분석 규칙을 사용합니다: 두 자리 정수가 예상되며, [ 69 , 99 ] 범위의 값은 1969년부터 1999년까지의 값을, [ 00 , 68 ] 범위의 값은 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++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 248 C++98 eofbit 가 끝 반복자에 도달할 때 설정되지 않음 유효한 연도를 읽지 못한 경우 eofbit 를 설정함

참고 항목

(C++11)
지정된 형식의 날짜/시간 값을 파싱합니다
(함수 템플릿)