博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++之国际化(6) --- 日期时间格式化
阅读量:4198 次
发布时间:2019-05-26

本文共 4419 字,大约阅读时间需要 14 分钟。

转载自:

 

    日期和时间的解析和格式化工作由time类型的两个facet完成:

    time_get
    time_put
    他们借助操作类型为tm的对象完成这些工作。
    tm为一个结构体,定义于<ctime>中
struct tm
{
 int tm_sec;  // Seconds: 0-59
 int tm_min;  // Minutes: 0-59
 int tm_hour; // Hours since midnight: 0-23
 int tm_mday; // Day of the month: 1-31
 int tm_mon;  // Months *since* january: 0-11
 int tm_year; // Years since 1900
 int tm_wday; // Days since Sunday (0-6)
 int tm_yday; // Days since Jan. 1: 0-365
 int tm_isdst; // +1 Daylight Savings Time, 0 No DST,  -1 don't know
};
time_get,time_put的行为都和函数strftime()有关系(定义于<ctime>),
此函数根据一个含有转换规格的字符串,从一个tm对象生成一个字符串。
声明如下:
_CRTIMP size_t __cdecl __MINGW_NOTHROW
strftime (char*, size_t, const char*, const struct tm*);
下面为其用到的转换规格
规格符号    意思                   范例
%a          Abbreviated weekday     Mon
%A          Full weekday            Monday
%b          Abbreviated month name  Jul
%B          Full month name         July
%c          Locale's preferred date and time representation  Jul 12 21:53:22 1998
%d          Day of the month        12
%H          Hour of the day using a 24-hour clock  21
%I          Hour of the day using a 12-hour clock  9
%j          Day of the year         193
%m          Month as decimal number 7
%M          Minutes                 53
%P          Morning or evening (am or pm)   pm
%S          Seconds                 22
%U          Week number starting with the first Sunday  28
%W          Week number starting with the first Monday  28
%w          Weekday as a number (Sunday == 0)  0
%x          Locale's preferred date representation  Jul 12 1998
%X          Locale's preferred time representation  21:53:22
%y          The year without the century  98
%Y          The year with the century  1998
%Z          The time zone  MEST
%%          The literal %  7.

===============================================================================================

time_get类,来源于MSDN:

template<class E, class InIt = istreambuf_iterator<E> >
class time_get : public locale::facet
{
public:
    typedef E char_type;
    typedef InIt iter_type;
    explicit time_get(size_t refs = 0);
    //返回facet中年月日的次序
    dateorder date_order() const;
    //解析first last之间的字符串用以表示时间,和strftime以%X一致
    iter_type get_time(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    //解析first last之间的字符串用以表示日期,和strftime以%x一致
    iter_type get_date(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    //解析first last之间的字符串用以表示星期
    iter_type get_weekday(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    //解析first last之间的字符串用以表示月份
    iter_type get_month(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    //解析first last之间的字符串用以表示年份,
    iter_type get_year(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    static locale::id id;
protected:
    ~time_get();
    virtual dateorder do_date_order() const;
    virtual iter_type do_get_time(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    virtual iter_type do_get_date(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    virtual iter_type do_get_weekday(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    virtual iter_type do_get_month(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    virtual iter_type do_get_year(iter_type first, iter_type last, ios_base& x, ios_base::iostate& st, tm *pt) const;
    };
time_get两个template参数:字符性别E和输入迭代器InIt(缺省为istreambuf_iterator<E>)
其中的功能函数除了date_order()外都是对字符串进行解析,并将结果存储于tm的一个指针中
如果解析错误,返回错误或者在tm对象中存储一个不确定的值
x决定解析过程中的格式
返回一个指向最后读取字符的下一个位置的迭代器
一旦解析结束或者失败,解析工作立即停止。

date_order()返回日期字符串中的年月日顺序:

返回值       意思
no_order No particular order (for example, a date may be in Julian format)
dmy          The order is day, month, year
mdy         The order is month, day, year
ymd         The order is year, month, day
ydm         The order is year, day, month
类time_base中定义了年月日次序的一个枚举:
class time_base
{
public:
    enum dateorder {no_order, dmy, mdy, ymd, ydm};
};

===============================================================================================

time_put类,来源于MSDN:

template<class E, class OutIt = ostreambuf_iterator<E> >

class time_put : public locale::facet
{
public:
    typedef E char_type;
    typedef OutIt iter_type;
    explicit time_put(size_t refs = 0);
    iter_type put(iter_type next, ios_base& x, tm *pt, char fmt, char mod = 0) const;
    iter_type put(iter_type next, ios_base& x, tm *pt, const E *first, const E *last) const;
    static locale::id id;
protected:
    ~time_put();
    virtual iter_type do_put(iter_type next, ios_base& x, tm *pt, char fmt, char mod = 0) const;
    };
time_put两个template参数:字符性别E和输出迭代器OutIt(缺省为ostreambuf_iterator<E>)
只有两个功能函数put(),用来将存储于tm对象中的日期信息转换为一个字符串序列,
并写到OutIt迭代器

 

你可能感兴趣的文章
mkyaffs2image 下载编译使用
查看>>
C程序 大数组:段错误 (核心已转储)
查看>>
socket 指定网口收发数据
查看>>
嵌入式samba功能的实现,linux下samba的移植
查看>>
海思hi3519v101 串口调试
查看>>
windows dos 常用命令行(完整)
查看>>
NetHogs - Linux进程实时统计网络带宽利用率
查看>>
hi3559v100 sdk 编译错误
查看>>
H264码流保存为YUV数据 通过H264Visa软件转换
查看>>
H265 Profile & Level & Tier 介绍
查看>>
windows 系统默认安装路径的修改
查看>>
hi3559av100 编译 Hi3559AV100ES_SDK_V2.0.2.0 软件包 遇到的问题
查看>>
linux 交叉编译 tcpdump以及tcpdump的使用
查看>>
海思平台GDB调试程序
查看>>
嵌入式系统中设置系统时区
查看>>
Windows系统中通过命令查看文件的MD5,SHA1,SHA256校验值
查看>>
x264编码 1pass 与 2pass 性能质量对比
查看>>
Docker 容器化技术介绍(一) 之 虚拟化技术
查看>>
Docker 容器化技术介绍(二) 之 Docker 简介
查看>>
Docker 容器化技术介绍(三) 之 Docker 组件
查看>>