CppDS.com

C++ 98 11 14 17 20 手册

std::time_get<CharT,InputIt>::get_monthname, std::time_get<CharT,InputIt>::do_get_monthname

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

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

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

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

存储分析而得的月名于 std::tmt->tm_mon

若在读到合法月名之前抵达尾迭代器,则函数设置 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>
#include <ctime>
 
void try_get_mon(const std::string& s)
{
    std::cout << "Parsing the month 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_monthname(
            {str}, {}, str, err, &t
        );
    str.setstate(err);
    std::istreambuf_iterator<char> last{};
    if(str) {
        std::cout << "Successfully parsed, month number is " << t.tm_mon;
        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("ja_JP.utf8"));
    try_get_mon("2月");
    std::locale::global(std::locale("th_TH.utf8"));
    try_get_mon("กุมภาพันธ์");
    std::locale::global(std::locale("el_GR.utf8"));
    try_get_mon("Φεβ");
    try_get_mon("Φεβρουάριος");
    std::locale::global(std::locale("en_US.utf8"));
    try_get_mon("Febrile");
}

输出:

Parsing the month out of '2月' in the locale ja_JP.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'กุมภาพันธ์' in the locale th_TH.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβ' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Φεβρουάριος' in the locale el_GR.utf8
Successfully parsed, month number is 1. The input was fully consumed
Parsing the month out of 'Febrile' in the locale en_US.utf8
Parse failed. Unparsed string: ile

参阅

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