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_type | CharT |
| iter_type | InputIt |

调用 do_get_weekday & 从输入流释出星期的日名

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

|---------------------------------------------------------------------------------------------------------------------------------------------------|-----|---|
| 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_time 、 time_get::get 和 POSIX 函数 strptime() 所用的 "%a" ,分析出星期名(可能为缩写)。

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

存储分析得到的星期于 std::tm 域 t->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 不修改。

调用示例 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_wday(const std::string& str)
{
    std::cout << "Parsing the weekday 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_weekday(
    {istringstream}, {}, istringstream, err, &tm);
    istringstream.setstate(err);

    std::istreambuf_iterator<char> last{};
    if (istringstream)
    {
        std::cout << "Successfully parsed, weekday number is " << tm.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 << 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_wday("Sunday");
        try_get_wday("星期五");
    }

    return 0;
}

输出

Parsing the weekday out of 'Sunday' in the locale de-DE_phoneb Parse failed. Unparsed string: unday
Parsing the weekday out of 'D??ú??' in the locale de-DE_phoneb Parse failed. Unparsed string: D??ú??
Parsing the weekday out of 'Sunday' in the locale es-ES_tradnl Parse failed. Unparsed string: Sunday
Parsing the weekday out of 'D??ú??' in the locale es-ES_tradnl Parse failed. Unparsed string: D??ú??
Parsing the weekday out of 'Sunday' in the locale hu-HU_technl Parse failed. Unparsed string: unday
Parsing the weekday out of '???ú??' in the locale hu-HU_technl Parse failed. Unparsed string: ???ú??
Parsing the weekday out of 'Sunday' in the locale ja-JP_radstr Parse failed. Unparsed string: Sunday
Parsing the weekday out of 'Sunday' in the locale ka-GE_modern Parse failed. Unparsed string: Sunday
Parsing the weekday out of '星期五' in the locale ka-GE_modern Parse failed. Unparsed string: 星期五
Parsing the weekday out of 'Sunday' in the locale x-IV_mathan Successfully parsed, weekday number is 0 the input was fully consumed
Parsing the weekday out of 'D??ú??' in the locale x-IV_mathan Parse failed. Unparsed string: D??ú??
Parsing the weekday out of 'Sunday' in the locale zh-CN_phoneb Parse failed. Unparsed string: Sunday
Parsing the weekday out of '星期五' in the locale zh-CN_phoneb Successfully parsed, weekday number is 5 the input was fully consumed
Parsing the weekday out of 'Sunday' in the locale zh-CN_stroke Parse failed. Unparsed string: Sunday
Parsing the weekday out of '星期五' in the locale zh-CN_stroke Successfully parsed, weekday number is 5 the input was fully consumed
Parsing the weekday out of 'Sunday' in the locale zh-HK_radstr Parse failed. Unparsed string: Sunday
Parsing the weekday out of '陎?拻' in the locale zh-HK_radstr Parse failed. Unparsed string: 陎?拻
Parsing the weekday out of 'Sunday' in the locale zh-MO_radstr Parse failed. Unparsed string: Sunday
Parsing the weekday out of '陎?拻' in the locale zh-MO_radstr Parse failed. Unparsed string: 陎?拻
Parsing the weekday out of 'Sunday' in the locale zh-MO_stroke Parse failed. Unparsed string: Sunday
Parsing the weekday out of '陎?拻' in the locale zh-MO_stroke Parse failed. Unparsed string: 陎?拻
Parsing the weekday out of 'Sunday' in the locale zh-SG_phoneb Parse failed. Unparsed string: Sunday
Parsing the weekday out of '星期五' in the locale zh-SG_phoneb Successfully parsed, weekday number is 5 the input was fully consumed
Parsing the weekday out of 'Sunday' in the locale zh-SG_stroke Parse failed. Unparsed string: Sunday
Parsing the weekday out of '星期五' in the locale zh-SG_stroke Successfully parsed, weekday number is 5 the input was fully consumed
Parsing the weekday out of 'Sunday' in the locale zh-TW_pronun Parse failed. Unparsed string: Sunday
Parsing the weekday out of '陎?拻' in the locale zh-TW_pronun Parse failed. Unparsed string: 陎?拻
Parsing the weekday out of 'Sunday' in the locale zh-TW_radstr Parse failed. Unparsed string: Sunday
Parsing the weekday out of '陎?拻' in the locale zh-TW_radstr Parse failed. Unparsed string: 陎?拻
相关推荐
Mr.Pascal2 分钟前
刚学php序列化/反序列化遇到的坑(攻防世界:Web_php_unserialize)
开发语言·安全·web安全·php
小尤笔记14 分钟前
利用Python编写简单登录系统
开发语言·python·数据分析·python基础
秦老师Q17 分钟前
Java基础第九章-Java集合框架(超详细)!!!
java·开发语言
计算机毕设源码qq-383653104119 分钟前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
无尽的大道30 分钟前
深入理解 Java 阻塞队列:使用场景、原理与性能优化
java·开发语言·性能优化
建群新人小猿35 分钟前
会员等级经验问题
android·开发语言·前端·javascript·php
Evand J1 小时前
集合卡尔曼滤波(Ensemble Kalman Filter),用于二维滤波(模拟平面上的目标跟踪),MATLAB代码
matlab·平面·目标跟踪
007php0071 小时前
GoZero 上传文件File到阿里云 OSS 报错及优化方案
服务器·开发语言·数据库·python·阿里云·架构·golang
数据小小爬虫1 小时前
如何利用Java爬虫获得1688店铺详情
java·开发语言
Tech Synapse1 小时前
Python网络爬虫实践案例:爬取猫眼电影Top100
开发语言·爬虫·python