1.基础介绍
头文件:
#include
api:
(1)std::put_time
c++源码:
// TEMPLATE STRUCT _Timeobj
template
struct _Timeobj
{ // store reference to tm object and format
_Timeobj(_Ptr _Tptr_arg, const _Elem *_Fmt_arg)
: _Tptr(_Tptr_arg), _Fmtfirst(_Fmt_arg)
{ // construct from tm pointer and format pointer
for (_Fmtlast = _Fmtfirst; *_Fmtlast != 0; ++_Fmtlast)
; // find end of format string
}
_Ptr _Tptr; // the tm struct pointer
const _Elem *_Fmtfirst; // format string start
const _Elem *_Fmtlast; // format string end
};
// TEMPLATE FUNCTION put_time
template inline
_Timeobj<_Elem, const struct tm *>
put_time(const struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem, const struct tm *>(_Tptr_arg, _Fmt_arg));
}
template inline
basic_ostream<_Elem, _Traits>&
operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Timeobj<_Elem2, const struct tm *>& _Manip)
{ // put time information to output stream
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_put<_Elem2, _Iter> _Mytput;
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for put_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myos::sentry _Ok(_Ostr);
if (_Ok)
{ // state okay, insert monetary amount
const _Mytput& _Tput_fac = _USE(_Ostr.getloc(), _Mytput);
_TRY_IO_BEGIN
if (_Tput_fac.put(_Iter(_Ostr.rdbuf()), _Ostr, _Ostr.fill(),
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast).failed())
_State |= ios_base::badbit;
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
由源码可知std::put_time第一个形参是tm,第二个形参是输出格式,返回值是_Timeobj,而c++重载了"<<"支持_Timeobj,所以c++可直接输入类似于std::cout << std::put_time()...这样的操作
(2)std::get_time
c++源码:
template
struct _Timeobj
{ // store reference to tm object and format
_Timeobj(_Ptr _Tptr_arg, const _Elem *_Fmt_arg)
: _Tptr(_Tptr_arg), _Fmtfirst(_Fmt_arg)
{ // construct from tm pointer and format pointer
for (_Fmtlast = _Fmtfirst; *_Fmtlast != 0; ++_Fmtlast)
; // find end of format string
}
_Ptr _Tptr; // the tm struct pointer
const _Elem *_Fmtfirst; // format string start
const _Elem *_Fmtlast; // format string end
};
// TEMPLATE FUNCTION get_time
template inline
_Timeobj<_Elem, struct tm *>
get_time(struct tm *_Tptr_arg, const _Elem *_Fmt_arg)
{ // return a _Timeobj manipulator
return (_Timeobj<_Elem, struct tm *>(_Tptr_arg, _Fmt_arg));
}
template inline
basic_istream<_Elem, _Traits>&
operator>>(basic_istream<_Elem, _Traits>& _Istr,
const _Timeobj<_Elem2, struct tm *>& _Manip)
{ // get time information from input stream
typedef basic_istream<_Elem, _Traits> _Myis;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef time_get<_Elem2, _Iter> _Mytget;
static_assert(is_same<_Elem, _Elem2>::value,
"wrong character type for get_time");
ios_base::iostate _State = ios_base::goodbit;
const typename _Myis::sentry _Ok(_Istr);
if (_Ok)
{ // state okay, extract time amounts
const _Mytget& _Tget_fac = _USE(_Istr.getloc(), _Mytget);
_TRY_IO_BEGIN
_Tget_fac.get(_Iter(_Istr.rdbuf()), _Iter(0), _Istr, _State,
_Manip._Tptr, _Manip._Fmtfirst, _Manip._Fmtlast);
_CATCH_IO_(_Istr)
}
_Istr.setstate(_State);
return (_Istr);
}
由源码可知std::get_time第一个参数是tm,第二个同样是输入格式,返回值是_Timeobj,而c++也重载了">>"支持_Timeobj,所以
c++可直接输入类似std::cin >> std::get_time()...这样的操作
2.例子
#include
#include
#include //对应c语言time.h
#include //io操作
#include
int main()
{
//显示1例子
time_t rawtime;
struct tm info;
char buffer[80];
time(&rawtime);
//localtime_s(&info, &rawtime); //将time_t转为tm
//std::localtime非线程安全,使用localtime_r函数代替
localtime_s(&info, &rawtime);//将time_t转为tm
//ctime是非线程安全的,C标准也建议使用strftime,不要使用ctime、ctime_s
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &info);
printf("格式化的日期 & 时间 : |%s|\n", buffer);
//转换例子
time_t dd = std::mktime(&info);//将tm转为time_t
//显示2例子:使用std::put_time,std::get_time
std::cout << std::put_time(&info, "%Y-%m-%d %H:%M:%S");
//std::cout << std::put_time(&info, "%F %T\n");
//get_time例子
std::stringstream ss;
ss << std::put_time(&info, "%Y-%m-%d %H:%M:%S");
struct tm info2;
ss >> std::get_time(&info2, "%Y-%m-%d %H:%M:%S");
system("pause");
return 0;
}
注意:
(1)time(null)单位是s
(2)std::put_time、std::get_time中格式与strftime中格式是一致的,格式介绍如下:
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &info);
std::cout << std::put_time(&info, "%Y-%m-%d %H:%M:%S");
ss >> std::get_time(&info2, "%Y-%m-%d %H:%M:%S");
=>
%Y-%m-%d %H:%M:%S
%Y --年
%m --月
%d --日
%H --时
%M --分
%S --秒
更高级:
%F 等价于 %Y-%m-%d,
%T 等价于 %H:%M:%S
所以也可以写为
strftime(buffer, 80, "%F %T", &info);
std::cout << std::put_time(&info, "%F %T");
ss >> std::get_time(&info2, "%F %T");
更多格式详细介绍:https://zh.cppreference.com/w/cpp/io/manip/put_time