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

std::time_get::get_time

Defined in header <locale>

public: iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const;

(1)

protected: virtual iter_type get_time( iter_type beg, iter_type end, std::ios_base& str, std::ios_base::iostate& err, std::tm* t) const;

(2)

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

2%29从序列中读取连续字符。[beg, end)并按照格式说明符的相同规则解析时间值。

'%X'

(until C++11)

"%H:%M:%S"

(since C++11)

函数所使用的std::get_time,,,time_get::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

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>
 
void try_get_time(const std::string& s)
{
    std::cout << "Parsing the time 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_time(
            std::istreambuf_iterator<char>(str),
            std::istreambuf_iterator<char>(),
            str, err, &t
        );
    str.setstate(err);
    if(str) {
        std::cout << "Hours: "   << t.tm_hour << ' '
                  << "Minutes: " << t.tm_min  << ' '
                  << "Seconds: " << t.tm_sec  << '\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("ru_RU.utf8"));
    try_get_time("21:40:11");
    try_get_time("21-40-11");
 
    std::locale::global(std::locale("ja_JP.utf8"));
    try_get_time("21時37分58秒");
}

二次

产出:

二次

代码语言:javascript
复制
Parsing the time out of '21:40:11' in the locale ru_RU.utf8
Hours: 21 Minutes: 40 Seconds: 11
Parsing the time out of '21-40-11' in the locale ru_RU.utf8
Parse failed. Unparsed string: -40-11
Parsing the time out of '21時37分58秒' in the locale ja_JP.utf8
Hours: 21 Minutes: 37 Seconds: 58

二次

另见

get_time (C++11)

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

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

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

扫码关注腾讯云开发者

领取腾讯云代金券