C/C++ 《常用函数》

paylove / 2024-08-09 / 原文

获取当前时间字符串

#include<iostream>
#include<time.h>
using namespace std;
std::string getLocalTimeString() {
	time_t rawtime;
	struct tm local_time;
	time(&rawtime);
	string retStr;
	if (localtime_s(&local_time, &rawtime) == 0) {
		retStr = to_string(1900 + local_time.tm_year) + '-' + to_string(1 + local_time.tm_mon) + '-' + to_string(local_time.tm_mday) + ' ' + to_string(local_time.tm_hour) + ':' + to_string(local_time.tm_min) + ':' + to_string(local_time.tm_sec);
	}
	return retStr;
}