CppDS.com

C++ 98 11 14 17 20 手册

std::time_get<CharT,InputIt>::get_weekday, std::time_get<CharT,InputIt>::do_get_weekday

来自cppreference.com
< cpp‎ | locale‎ | time get
 
 
 
 
定义于头文件 <locale>
public:

iter_type get_weekday( 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_weekday( iter_type beg, iter_type end, std::ios_base& str,

                                  std::ios_base::iostate& err, std::tm* t) const;
(2)
1) 公开成员函数,调用最终导出类的受保护虚成员函数 do_get_weekday
2) 从字符序列 [beg, end) 读取出相继字符,用此 locale 所期待的星期默认格式,同函数 std::get_timetime_get::get 和 POSIX 函数 strptime() 所用的 "%a" ,分析出星期名(可能为缩写)。

若它找到缩写名,则它持续读取,直至消耗尽完整名的字符,或找到不为期待的字符,后一情况下即使首段字符为合法缩写也分析失败。

存储分析得到的星期于 std::tmt->tm_wday

若在读到合法星期名前抵达尾迭代器,则函数设置 err 中的 std::ios_base::eofbit 。若遇到分析错误,则函数设置 err 中的 std::ios_base::failbit

参数

beg - 指代要分析的序列起始的迭代器
end - 要分析的序列的尾后一位置迭代器
str - 此函数在需要时用以获得 locale 平面的流对象,例如用 std::ctype 跳过空白符或用 std::collate 比较字符串
err - 此函数所修改以指示错误的流错误标志对象
t - 指向 std::tm 对象的指针,该对象将保有此函数调用结果

返回值

指向 [beg, end) 中辨识为合法星期名一部分的末字符后一位置的迭代器。

注意

此函数通常不区别大小写

若遇到分析错误,则此函数的大多数实现保留 *t 不修改。

示例

#include <iostream>
#include <locale>
#include <sstream>
#include <iterator>
 
void try_get_wday(const std::string& s)
{
    std::cout << "Parsing the weekday 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_weekday(
            {str}, {}, str, err, &t
        );
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
    if(str) {
        std::cout << "Successfully parsed, weekday number is " << t.tm_wday;
        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("lt_LT.utf8"));
    try_get_wday("Št");
    try_get_wday("Šeštadienis");
 
    std::locale::global(std::locale("en_US.utf8"));
    try_get_wday("SATELLITE");
 
    std::locale::global(std::locale("ja_JP.utf8"));
    try_get_wday("土曜日");
}

输出:

Parsing the weekday out of 'Št' in the locale lt_LT.utf8
Successfully parsed, weekday number is 6 the input was fully consumed
Parsing the weekday out of 'Šeštadienis' in the locale lt_LT.utf8
Successfully parsed, weekday number is 6 the input was fully consumed
Parsing the weekday out of 'SATELLITE' in the locale en_US.utf8
Successfully parsed, weekday number is 6 Remaining content: ELLITE
Parsing the weekday out of '土曜日' in the locale ja_JP.utf8
Successfully parsed, weekday number is 6 the input was fully consumed

参阅

(C++11)
剖析指定格式的日期/时间值
(函数模板)
关闭