c++11 标准模板(STL)本地化库 - 平面类别(time_get) - 从输入字符序列中解析时间/日期值到 std::tm 中(七)

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

平面类别

从输入字符序列中解析时间/日期值到 std::tm 中

std::time_get
template<

    class CharT,
    class InputIt = std::istreambuf_iterator<CharT>

> class time_get;

类模板 std::time_get 封装日期和时间分析规则。 I/O 操纵符 std::get_time 用 I/O 流的 locale 的 std::time_get 平面转换文本输入为 std::tm 对象。

继承图

类型要求

- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

特化

标准库提供二个孤立(独立于本地环境的)全特化和二个部分特化:

定义于头文件 <locale>

std::time_get<char>分析日期和时间的窄字符串表示
std::time_get<wchar_t>分析日期和时间的宽字符串表示
std::time_get<char, InputIt>用定制输入迭代器分析日期和时间的窄字符串表示
std::time_get<wchar_t, InputIt>用定制输入迭代器分析日期和时间的宽字符串表示

另外, C++ 程序中构造的每个 locale 对象都实装这些特化的其自身(本地环境限定)版本。

成员类型

成员类型定义
char_typeCharT
iter_typeInputIt

调用 do_get_year & 从输入流释出年份

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

iter_type do_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) 公开成员函数,调用最终导出类的受保护虚成员函数 do_get_year

2) 读取来自序列 [beg, end) 的相继字符,用某实现定义格式分析出年。取决于 locale ,可能接受二位的年,而它们属于哪个世纪是实现定义的。

存储分析而得的年于 std::tm 结构体域 t->tm_year 。

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

参数

beg-指代要分析的序列起始的迭代器
end-要分析的序列的尾后一位置迭代器
str-此函数在需要时用以获得 locale 平面的流对象,例如用 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 不修改。

调用示例 windows

#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>
#include <Windows.h>

std::vector<std::wstring> locals;

BOOL CALLBACK MyFuncLocaleEx(LPWSTR pStr, DWORD dwFlags, LPARAM lparam)
{
    locals.push_back(pStr);
    return TRUE;
}

std::string stows(const std::wstring& ws)
{
    std::string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
    setlocale(LC_ALL, "chs");
    const wchar_t* _Source = ws.c_str();
    size_t _Dsize = 2 * ws.size() + 1;
    char *_Dest = new char[_Dsize];
    memset(_Dest, 0, _Dsize);
    wcstombs(_Dest, _Source, _Dsize);
    std::string result = _Dest;
    delete[]_Dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}

void try_get_year(const std::string& str)
{
    std::cout << "Parsing the year out of '" << str <<
              "' in the locale " << std::locale().name() << ' ';
    std::istringstream istringstream(str);
    std::ios_base::iostate err = std::ios_base::goodbit;

    std::tm tm;
    std::istreambuf_iterator<char> ret =
        std::use_facet<std::time_get<char>>(istringstream.getloc()).get_year(
    {istringstream}, {}, istringstream, err, &tm);

    istringstream.setstate(err);
    std::istreambuf_iterator<char> last{};

    if (istringstream)
    {
        std::cout << "Successfully parsed, year is " << 1900 + tm.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 << std::endl;
}

int main()
{
    EnumSystemLocalesEx(MyFuncLocaleEx, LOCALE_ALTERNATE_SORTS, NULL, NULL);

    for (std::vector<std::wstring>::const_iterator str = locals.begin();
            str != locals.end(); ++str)
    {
        std::locale::global(std::locale(stows(*str)));
        try_get_year("24");
        try_get_year("2024年");
    }

    return 0;
}

输出

Parsing the year out of '24' in the locale de-DE_phoneb Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024?ê' in the locale de-DE_phoneb Successfully parsed, year is 2024 Remaining content: ?ê
Parsing the year out of '24' in the locale es-ES_tradnl Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024?ê' in the locale es-ES_tradnl Successfully parsed, year is 2024 Remaining content: ?ê
Parsing the year out of '24' in the locale hu-HU_technl Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024??' in the locale hu-HU_technl Successfully parsed, year is 2024 Remaining content: ??
Parsing the year out of '24' in the locale ja-JP_radstr Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '24' in the locale ka-GE_modern Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024年' in the locale ka-GE_modern Successfully parsed, year is 2024 Remaining content: 年
Parsing the year out of '24' in the locale x-IV_mathan Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024?ê' in the locale x-IV_mathan Successfully parsed, year is 2024 Remaining content: ?ê
Parsing the year out of '24' in the locale zh-CN_phoneb Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024年' in the locale zh-CN_phoneb Successfully parsed, year is 2024 Remaining content: 年
Parsing the year out of '24' in the locale zh-CN_stroke Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024年' in the locale zh-CN_stroke Successfully parsed, year is 2024 Remaining content: 年
Parsing the year out of '24' in the locale zh-HK_radstr Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024爛' in the locale zh-HK_radstr Successfully parsed, year is 2024 Remaining content: 爛
Parsing the year out of '24' in the locale zh-MO_radstr Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024爛' in the locale zh-MO_radstr Successfully parsed, year is 2024 Remaining content: 爛
Parsing the year out of '24' in the locale zh-MO_stroke Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024爛' in the locale zh-MO_stroke Successfully parsed, year is 2024 Remaining content: 爛
Parsing the year out of '24' in the locale zh-SG_phoneb Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024年' in the locale zh-SG_phoneb Successfully parsed, year is 2024 Remaining content: 年
Parsing the year out of '24' in the locale zh-SG_stroke Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024年' in the locale zh-SG_stroke Successfully parsed, year is 2024 Remaining content: 年
Parsing the year out of '24' in the locale zh-TW_pronun Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024爛' in the locale zh-TW_pronun Successfully parsed, year is 2024 Remaining content: 爛
Parsing the year out of '24' in the locale zh-TW_radstr Successfully parsed, year is 1924 the input was fully consumed
Parsing the year out of '2024爛' in the locale zh-TW_radstr Successfully parsed, year is 2024 Remaining content: 爛

  • 22
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值