C++11獲取時間

huangjiazhi_發表於2020-10-06

C++11獲取時間

const std::string GetCurrentSystemTime()
{
	auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
	struct tm* ptm = localtime(&t);
	char date[60] = { 0 };
	sprintf(date, "%d-%02d-%02d %02d:%02d:%02d",
		(int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday,
		(int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec);
	return move(std::string(date));
}

int main(int argc, char* argv[])
{
	std::string strCurTime = GetCurrentSystemTime();
	printf("當前時間: %s\n", strCurTime.c_str());
}

相關文章