1、声明结构体 struct tm
2、time(0)
获取当前的时间戳
3、用时间戳来填充结构体:localtime_r(&时间戳 , &tm结构体)
4、用格式字符串来将tm结构体转换为指定格式的日期时间字符串:strftime(char数组,数组容量,格式字符串,&tm结构体)
#include <iostream>
#include <ctime>
int main(int argc,char* argv[])
{
struct tm tm;
time_t ts = time(0);
localtime_r(&ts,&tm);
char buf[128];
strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",&tm);
std::cout<<buf<<std::endl;
return 0;
}