C++之国际化(6) --- 日期时间格式化
2014-03-03 13:33
363 查看
转载自:http://hi.baidu.com/nicker2010/item/be82f696283d88d31a49dfeb
日期和时间的解析和格式化工作由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迭代器
日期和时间的解析和格式化工作由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迭代器
相关文章推荐
- 【转】C/C++中的日期和时间
- C/C++中的日期和时间 time_t与struct tm变换
- [转载] C/C++中怎样获取日期和时间
- 获取当前时间日期并格式化--JS
- SQLSERVER格式化日期时间
- C/C++中的日期和时间
- CRM JS 日期格式化及时间设置
- C#日期时间格式化 日期转化一
- C/C++中的日期和时间 time_t与struct tm
- PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
- 【转】Js获取当前日期时间及格式化操作
- js jq插件 显示中文时间戳 刚刚 N分钟前 N小时前 今天 上午 下午 日期格式化
- JavaScript 格式化日期,转换时间日期格式
- C#格式化日期时间
- hibernate jpa 注解 @Temporal(TemporalType.DATE) 格式化时间日期,页面直接得到格式化类型的值
- 格式化日期和时间
- 在Jquery里格式化Date日期时间数据
- El表达式的判断字符串的长度和截取,日期时间的格式化
- MySQL取得当前时间的函数是什么 格式化日期的函数是什么