系统时间的获取和转换

获取系统字符串时间

std::string cuurTime()
{
	 char tmp[64];
	 time_t  ptime;
	 time(&ptime);
	 strftime(tmp,sizeof(tmp),"%Y-%m-%d %H:%M:%S",localtime(&ptime));
	 return tmp;//获得字符串
}
std::string cuurTime()char timeStr[64];
	time_t tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
	struct tm * ptminfo = localtime(&tt);
	strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", ptminfo);
	return tmp;
}

将字符串时间转换成秒数:

time_t  strToSecond(std::string time)// 20200204132000    年  月  日 时 分 秒 转 换 成 秒 
{
	struct tm timeTmp;
	time_t seconds;
	int sz = time.size();
	if (sz == 8)
	{
		timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
		timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
		timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());

		timeTmp.tm_hour = 0;
		timeTmp.tm_min = 0;
		timeTmp.tm_sec = 0;
	}
	else if (sz == 10)
	{
		timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
		timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
		timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());

		timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
		timeTmp.tm_min = 0;
		timeTmp.tm_sec =0;
	}
	else if (sz==12)
	{
		timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
		timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
		timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());

		timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
		timeTmp.tm_min = atoi(time.substr(10, 2).c_str());
		timeTmp.tm_sec = 0;
	}
	else if (sz == 14)
	{
		timeTmp.tm_year = atoi(time.substr(0, 4).c_str()) - 1900; //从1900 开始算起
		timeTmp.tm_mon = atoi(time.substr(4, 2).c_str());
		timeTmp.tm_mday = atoi(time.substr(6, 2).c_str());

		timeTmp.tm_hour = atoi(time.substr(8, 2).c_str());
		timeTmp.tm_min = atoi(time.substr(10, 2).c_str());
		timeTmp.tm_sec = atoi(time.substr(12, 2).c_str());
	}
	else
	{
		std::cout << "输入错误!" << std::endl;
	}
	seconds = mktime(&timeTmp);
	return seconds;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值