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

std::time_get::do_get_date

Defined in header <locale>

?

?

public: iter_type get_date( 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_date( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t ) const;

(2)

?

1%29公共成员函数,调用受保护的虚拟成员函数do_get_date()最派生的类。

2%29从序列中读取连续字符。[beg, end)并使用此区域设置所期望的默认格式解析日历日期值,该格式与

"%x"

(until C++11)

"%d/%m/%y", "%m/%d/%y", "%y/%m/%d", and "%y/%d/%m", depending on date_order()

(since C++11)

函数所使用的std::get_time(),,,get(),以及POSIX函数strptime()

已解析的日期存储在std::tm结构由论点指出t...

如果在读取有效日期之前到达结束迭代器,则函数设置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)这被确认为有效日期的一部分。

注记

对于默认日期格式%28(如果有%29)的字母部分,此函数通常不区分大小写。

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

除了标准所要求的日期格式之外,该实现还可以支持其他日期格式。

二次

代码语言:javascript
复制
#include <iostream>
#include <locale>
#include <sstream>
#include <iterator>
#include <ctime>
 
void try_get_date(const std::string& s)
{
    std::cout << "Parsing the date 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_date(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            str, err, &t
        );
    str.setstate(err);
    if(str) {
        std::cout << "Day: "   << t.tm_mday << ' '
                  << "Month: " << t.tm_mon + 1 << ' '
                  << "Year: "  << t.tm_year + 1900 << '\n';
    } 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_date("02/01/2013");
    try_get_date("02-01-2013");
 
    std::locale::global(std::locale("ja_JP.utf8"));
    try_get_date("2013年02月01日");
}

二次

产出:

二次

代码语言:javascript
复制
Parsing the date out of '02/01/2013' in the locale en_US.utf8
Day: 1 Month: 2 Year: 2013
Parsing the date out of '02-01-2013' in the locale en_US.utf8
Parse failed. Unparsed string: -01-2013
Parsing the date out of '2013年02月01日' in the locale ja_JP.utf8
Day: 1 Month: 2 Year: 2013

二次

另见

get_time (C++11)

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com