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");
}
以下 为 std::get_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 | Explanation | Writes to fields |
---|---|---|
% | matches a literal %. The full conversion specification must be %%. | (none) |
t | matches any whitespace. | (none) |
n | matches any whitespace. | (none) |
Year | ||
Y | parses full year as a 4 digit decimal number, leading zeroes permitted but not required | tm_year |
EY | parses year in the alternative representation, e.g.平成23年 (year Heisei 23) which writes 2011 to tm_year in ja_JP locale | tm_year |
y | parses 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 |
Oy | parses last 2 digits of year using the alternative numeric system, e.g. 十一 is parsed as 11 in ja_JP locale | tm_year |
Ey | parses year as offset from locale's alternative calendar period %EC | tm_year |
C | parses the first 2 digits of year as a decimal number (range [00,99]) | tm_year |
EC | parses the name of the base year (period) in the locale's alternative representation, e.g. 平成 (Heisei era) in ja_JP | tm_year |
Month | ||
b | parses the month name, either full or abbreviated, e.g. Oct | tm_mon |
h | synonym of b | tm_mon |
B | synonym of b | tm_mon |
m | parses the month as a decimal number (range [01,12]), leading zeroes permitted but not required | tm_mon |
Om | parses the month using the alternative numeric system, e.g. 十二 parses as 12 in ja_JP locale | tm_mon |
Week | ||
U | parses 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 |
OU | parses 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 |
W | parses 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 |
OW | parses 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 | ||
j | parses day of the year as a decimal number (range [001,366]), leading zeroes permitted but not required | tm_yday |
d | parses the day of the month as a decimal number (range [01,31]), leading zeroes permitted but not required | tm_mday |
Od | parses the day of the month using the alternative numeric system, e.g 二十七 parses as 23 in ja_JP locale, leading zeroes permitted but not required | tm_mday |
e | synonym of d | tm_mday |
Oe | synonym of Od | tm_mday |
Day of the week | ||
a | parses the name of the day of the week, either full or abbreviated, e.g. Fri | tm_wday |
A | synonym of a | tm_wday |
w | parses weekday as a decimal number, where Sunday is 0 (range [0-6]) | tm_wday |
Ow | parses 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 | ||
H | parses the hour as a decimal number, 24 hour clock (range [00-23]), leading zeroes permitted but not required | tm_hour |
OH | parses hour from 24-hour clock using the alternative numeric system, e.g. 十八 parses as 18 in ja_JP locale | tm_hour |
I | parses hour as a decimal number, 12 hour clock (range [01,12]), leading zeroes permitted but not required | tm_hour |
OI | parses hour from 12-hour clock using the alternative numeric system, e.g. 六 reads as 06 in ja_JP locale | tm_hour |
M | parses minute as a decimal number (range [00,59]), leading zeroes permitted but not required | tm_min |
OM | parses minute using the alternative numeric system, e.g. 二十五 parses as 25 in ja_JP locale | tm_min |
S | parses second as a decimal number (range [00,60]), leading zeroes permitted but not required | tm_sec |
OS | parses second using the alternative numeric system, e.g. 二十四 parses as 24 in ja_JP locale | tm_sec |
Other | ||
c | parses the locale's standard date and time string format, e.g. Sun Oct 17 04:41:13 2010 (locale dependent) | all |
Ec | parses the locale's alternative date and time string format, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale | all |
x | parses the locale's standard date representation | all |
Ex | parses the locale's alternative date representation, e.g. expecting 平成23年 (year Heisei 23) instead of 2011年 (year 2011) in ja_JP locale | all |
X | parses the locale's standard time representation | all |
EX | parses the locale's alternative time representation | all |
D | equivalent to "%m / %d / %y " | tm_mon,tm_mday,tm_year |
r | parses locale's standard 12-hour clock time (in POSIX, "%I : %M : %S %p") | tm_hour,tm_min,tm_sec |
R | equivalent to "%H : %M" | tm_hour,tm_min |
T | equivalent to "%H : %M : %S" | tm_hour,tm_min,tm_sec |
p | parses the locale's equivalent of a.m. or p.m. | tm_hour |
相关文章推荐
- C++和Java中文Api
- c++基础总结一
- C语言——字符串函数
- 二叉树反转
- 学习C++反汇编-多继承
- C++中const修饰二级指针(从类型‘int**’到类型‘const int**’的转换无效)
- C语言实现使用动态数组来构造栈结构
- CAF(C++ actor framework)使用随笔(延迟发送,消息转发,消息优先级)(四)
- C/C++基础-001
- C++primer plus 编程练习10.10
- C语言中的static 详细分析
- C/C++学习之基础-001
- C++11中值得关注的几大变化
- c++中的继承和组合
- c++拷贝构造函数
- 【HPU】[1015]【C语言训练】列出最简真分数序列
- c++初始化函数列表
- C/C++ 内存对齐
- #define和typedef的区别
- C++扬帆远航——1