C++ 的 get_time 和 put_time 函数

本文介绍了C++中用于快速时间格式转换的get_time和put_time函数。通过示例代码展示了如何利用这两个函数将时间戳转换为字符串,并输出标准的日期和时间格式。同时还提及了std::get_time的一些转义参数。
摘要由CSDN通过智能技术生成
为了快速的时间格式转换. 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
Explanation Used fields
% writes literal %. The full conversion specification must be %%.  
n(C++11) writes newline character  
t(C++11) writes horizontal tab character  
Year
Y writes 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 locale tm_year
y writes 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 locale tm_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_JP tm_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
b writes abbreviated month name, e.g. Oct (locale dependent) 月份英文简称 tm_mon
h(C++11) synonym of b 代名词? tm_mon
B writes full month name, e.g. October (locale dependent) 月份英文全称 tm_mon
m writes 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
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值