您的位置:首页 > 编程语言 > C语言/C++

C++ 的 get_time 和 put_time 函数

2016-03-06 16:06 507 查看
为了快速的时间格式转换. C++ 引进了 get_time 和 put_time 函数

#include <iostream>
#include<iomanip>
#include<ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
std::cout.imbue(std::locale("CHS"));
std::cout <<std::put_time(&tm,"%Y-%m-%d %H:%M:%S") << '\n';
// std::cout.imbue(std::locale("ja_JP.utf8"));
// std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
system("pause");
}

下面是时间函数格式串的一些特定转义参数:

注::VS2010 不支持C++11的格式

E前缀的表示在当地语言环境中的表示...

O前缀的表示使用当地语言环境的计数文字

常用

时:分:秒 %H:%M:%S <==> %T (C++11标准)

当地时间标准 %X

十二小时制 %I (%p <==> a.m|p.m 或者当地写法)

年-月-日 %Y:%m:%d <==> %F (C++11标准)

当期日期标准 %x

一年的第几天 %j

第几周 %U (00-53) 周日为第一天

%W (00-53) 周一为第一天

%V (01-53) 周一为第一天 (C++11标准)

周一为星期的第一天

每年的第一星期为包含1月4号的那个星期.

或者包含第一个星期四那个星期

星期几 %w (0-6) %u(1-7)(C++11标准)

星期几 %a 单词简写 %A 单词全称 (当地写法)

Conversion

specifier
ExplanationUsed fields
%writes literal %. The full conversion specification must be %%.
n(C++11)writes newline character
t(C++11)writes horizontal tab character
Year
Ywrites year as a 4 digit decimal number XXXX 4位数年份tm_year
EY(C++11)writes year in the alternative representation, e.g.平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localetm_year
ywrites last 2 digits of year as a decimal number ( XX range [00,99]) 两位数年份tm_year
Oy(C++11)writes last 2 digits of year using the alternative numeric system, e.g. 十一 instead of 11 in ja_JP localetm_year
Ey(C++11)writes year as offset from locale's alternative calendar period %EC (locale-dependent)tm_year
C(C++11)writes first 2 digits of year as a decimal number (range [00,99]) 公历年的前两位数tm_year
EC(C++11)writes name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JPtm_year
G(C++11)writes ISO 8601 week-based year, i.e. the year that contains the specified week.

In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4
Includes first Thursday of the year

tm_year,tm_wday,tm_yday
g(C++11)writes last 2 digits of ISO 8601 week-based year, i.e. the year that contains the specified week (range[00,99]).

In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4
Includes first Thursday of the year

tm_year,tm_wday,tm_yday
Month
bwrites abbreviated month name, e.g. Oct (locale dependent) 月份英文简称tm_mon
h(C++11)synonym of b 代名词?tm_mon
Bwrites full month name, e.g. October (locale dependent) 月份英文全称tm_mon
mwrites month as a decimal number (range [01,12]) 月份两位数字tm_mon
Om(C++11)writes month using the alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale月份当地文字tm_mon
Week
Uwrites week of the year as a decimal number (Sunday is the first day of the week) (range[00,53]) 第几周(周日为第一天)tm_year,tm_wday,tm_yday
OU(C++11)writes week of the year, as by %U, using the alternative numeric system, e.g. 五十二 instead
of 52 in ja_JP locale
tm_year,tm_wday,tm_yday
Wwrites week of the year as a decimal number (Monday is the first day of the week) (range[00,53]) 第几周(周一为第一天)tm_year,tm_wday,tm_yday
OW(C++11)writes week of the year, as by %W, using the alternative numeric system, e.g. 五十二 instead
of 52 in ja_JP locale
tm_year,tm_wday,tm_yday
V(C++11)writes ISO 8601 week of the year (range [01,53]).

In IS0 8601 weeks begin with Monday and the first week of the year must satisfy the following requirements:

Includes January 4
Includes first Thursday of the year

tm_year,tm_wday,tm_yday
OV(C++11)writes week of the year, as by %V, using the alternative numeric system, e.g. 五十二 instead
of 52 in ja_JP locale
tm_year,tm_wday,tm_yday
Day of the year/month
jwrites day of the year as a decimal number (range [001,366]) 一年的第几天(XXX)tm_yday
dwrites day of the month as a decimal number (range [01,31]) 一月的第几天(XX)tm_mday
Od(C++11)writes zero-based day of the month using the alternative numeric system, e.g 二十七 instead of 23 in ja_JP locale

Single character is preceded by a space.
tm_mday
e(C++11)writes day of the month as a decimal number (range [1,31]).

Single digit is preceded by a space.一月的第几天(X|XX)
tm_mday
Oe(C++11)writes one-based day of the month using the alternative numeric system, e.g. 二十七 instead of 27 in ja_JP locale

Single character is preceded by a space.
tm_mday
Day of the week
awrites abbreviated weekday name, e.g. Fri (locale dependent) 星期几的简写tm_wday
Awrites full weekday name, e.g. Friday (locale dependent) 星期几的全称tm_wday
wwrites weekday as a decimal number, where Sunday is 0 (range [0-6]) 星期几数字表示(0-6)tm_wday
Ow(C++11)writes weekday, where Sunday is 0, using the alternative numeric system, e.g. 二 instead
of 2 in ja_JP locale
tm_wday
u(C++11)writes weekday as a decimal number, where Monday is 1 (ISO 8601 format) (range [1-7]) 星期几数字表示(1-7)tm_wday
Ou(C++11)writes weekday, where Monday is 1, using the alternative numeric system, e.g. 二 instead
of 2 in ja_JP locale
tm_wday
Hour, minute, second
Hwrites hour as a decimal number, 24 hour clock (range [00-23]) 24进制小时(XX)tm_hour
OH(C++11)writes hour from 24-hour clock using the alternative numeric system, e.g. 十八 instead of 18 in ja_JP localetm_hour
Iwrites hour as a decimal number, 12 hour clock (range [01,12]) 12进制小时(XX)tm_hour
OI(C++11)writes hour from 12-hour clock using the alternative numeric system, e.g. 六 instead of 06 in ja_JP localetm_hour
Mwrites minute as a decimal number (range [00,59]) 分钟(XX)tm_min
OM(C++11)writes minute using the alternative numeric system, e.g. 二十五 instead of 25 in ja_JP localetm_min
Swrites second as a decimal number (range [00,60]) 秒数(XX)- -|有60秒耶tm_sec
OS(C++11)writes second using the alternative numeric system, e.g. 二十四 instead of 24 in ja_JP localetm_sec
Other
cwrites standard date and time string, e.g. Sun Oct 17 04:41:13 2010(locale dependent) 基本日期格式all
Ec(C++11)writes alternative date and time string, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
xwrites localized date representation (locale dependent)all
Ex(C++11)writes alternative date representation, e.g. using 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
Xwrites localized time representation (locale dependent)all
EX(C++11)writes alternative time representation (locale dependent)all
D(C++11)equivalent to "%m/%d/%y"tm_mon,tm_mday,tm_year
F(C++11)equivalent to "%Y-%m-%d" (the ISO 8601 date format)tm_mon,tm_mday,tm_year
r(C++11)writes localized 12-hour clock time (locale dependent)tm_hour,tm_min,tm_sec
R(C++11)equivalent to "%H:%M"tm_hour,tm_min
T(C++11)equivalent to "%H:%M:%S" (the ISO 8601 time format)tm_hour,tm_min,tm_sec
pwrites localized a.m. or p.m. (locale dependent)tm_hour
z(C++11)writes offset from UTC in the ISO 8601 format (e.g. -0430), or no characters if the time
zone information is not available
tm_isdst
Zwrites time zone name or abbreviation, or no characters if the time zone information is not available (locale dependent)tm_isdst
以下 为 std::get_time 一些转义参数

Conversion

specifier
ExplanationWrites to fields
%matches a literal %. The full conversion specification must be %%.(none)
tmatches any whitespace.(none)
nmatches any whitespace.(none)
Year
Yparses full year as a 4 digit decimal number, leading zeroes permitted but not requiredtm_year
EYparses year in the alternative representation, e.g.平成23年 (year Heisei 23) which writes 2011 to tm_year in ja_JP localetm_year
yparses last 2 digits of year as a decimal number. Range [69,99] results in values 1969 to
1999, range [00,68] results in 2000-2068
tm_year
Oyparses last 2 digits of year using the alternative numeric system, e.g. 十一 is parsed as 11 in ja_JP localetm_year
Eyparses year as offset from locale's alternative calendar period %ECtm_year
Cparses the first 2 digits of year as a decimal number (range [00,99])tm_year
ECparses the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JPtm_year
Month
bparses the month name, either full or abbreviated, e.g. Octtm_mon
hsynonym of btm_mon
Bsynonym of btm_mon
mparses the month as a decimal number (range [01,12]), leading zeroes permitted but not requiredtm_mon
Omparses the month using the alternative numeric system, e.g. 十二 parses as 12 in ja_JP localetm_mon
Week
Uparses the week of the year as a decimal number (Sunday is the first day of the week) (range[00,53]),
leading zeroes permitted but not required
tm_year,tm_wday,tm_yday
OUparses the week of the year, as by %U, using the alternative numeric system, e.g. 五十二 parses
as 52 in ja_JP locale
tm_year,tm_wday,tm_yday
Wparses the week of the year as a decimal number (Monday is the first day of the week) (range[00,53]),
leading zeroes permitted but not required
tm_year,tm_wday,tm_yday
OWparses the week of the year, as by %W, using the alternative numeric system, e.g. 五十二 parses
as 52 in ja_JP locale
tm_year,tm_wday,tm_yday
Day of the year/month
jparses day of the year as a decimal number (range [001,366]), leading zeroes permitted but
not required
tm_yday
dparses the day of the month as a decimal number (range [01,31]), leading zeroes permitted
but not required
tm_mday
Odparses the day of the month using the alternative numeric system, e.g 二十七 parses as 23 in ja_JP locale, leading zeroes permitted but not requiredtm_mday
esynonym of dtm_mday
Oesynonym of Odtm_mday
Day of the week
aparses the name of the day of the week, either full or abbreviated, e.g. Fritm_wday
Asynonym of atm_wday
wparses weekday as a decimal number, where Sunday is 0 (range [0-6])tm_wday
Owparses weekday as a decimal number, where Sunday is 0, using the alternative numeric system,
e.g. 二 parses as 2 in ja_JP locale
tm_wday
Hour, minute, second
Hparses the hour as a decimal number, 24 hour clock (range [00-23]), leading zeroes permitted
but not required
tm_hour
OHparses hour from 24-hour clock using the alternative numeric system, e.g. 十八 parses as 18 in ja_JP localetm_hour
Iparses hour as a decimal number, 12 hour clock (range [01,12]), leading zeroes permitted
but not required
tm_hour
OIparses hour from 12-hour clock using the alternative numeric system, e.g. 六 reads as 06 in ja_JP localetm_hour
Mparses minute as a decimal number (range [00,59]), leading zeroes permitted but not requiredtm_min
OMparses minute using the alternative numeric system, e.g. 二十五 parses as 25 in ja_JP localetm_min
Sparses second as a decimal number (range [00,60]), leading zeroes permitted but not requiredtm_sec
OSparses second using the alternative numeric system, e.g. 二十四 parses as 24 in ja_JP localetm_sec
Other
cparses the locale's standard date and time string format, e.g. Sun Oct 17 04:41:13 2010 (locale dependent)all
Ecparses the locale's alternative date and time string format, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
xparses the locale's standard date representationall
Exparses the locale's alternative date representation, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP localeall
Xparses the locale's standard time representationall
EXparses the locale's alternative time representationall
Dequivalent to "%m / %d / %y "tm_mon,tm_mday,tm_year
rparses locale's standard 12-hour clock time (in POSIX, "%I : %M : %S %p")tm_hour,tm_min,tm_sec
Requivalent to "%H : %M"tm_hour,tm_min
Tequivalent to "%H : %M : %S"tm_hour,tm_min,tm_sec
pparses the locale's equivalent of a.m. or p.m.tm_hour

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: