C++ 时间操作

将时间格式转换为1970年至今的秒数:

time_t convert_str_to_tm(char * str_time)
{
	struct tm tt;
	memset(&tt, 0, sizeof(tt));
	tt.tm_year = atoi(str_time) - 1900;
	tt.tm_mon = atoi(str_time + 5) - 1;
	tt.tm_mday = atoi(str_time + 8);
	tt.tm_hour = atoi(str_time + 11);
	tt.tm_min = atoi(str_time + 14);
	tt.tm_sec = atoi(str_time + 17);
	return mktime(&tt); //+ 28800;//28800是一个偏差。。加上这个。。刚好等于PHP的strtotime,但是在linux,vs中就不能加上28800这个时间差
}

注:

  入参str_time格式为:2010-02-03 00:00:00

判断string 类型的字符串中有几个相同的字符“;”

string str:str_time = "20190618;20190620;";

int   rre_num(string    str, string    sub)
{
    int   nRet = 0, nStart = 0;
    while (-1 != (nStart = str.find(sub, nStart)))
    {
        nStart += sub.length();
        ++nRet;
    }
    return   nRet;
}

完整代码为:

函数入口为:

    string  str_time = "20190618;20190620;";
    //str_time = allos_ops->date_1111;
    test_datetime(str_time);

time_t convert_str_to_tm(char * str_time)
{
	struct tm tt;
	memset(&tt, 0, sizeof(tt));
	tt.tm_year = atoi(str_time) - 1900;
	tt.tm_mon = atoi(str_time + 5) - 1;
	tt.tm_mday = atoi(str_time + 8);
	tt.tm_hour = atoi(str_time + 11);
	tt.tm_min = atoi(str_time + 14);
	tt.tm_sec = atoi(str_time + 17);
	return mktime(&tt); //+ 28800;//28800是一个偏差。。加上这个。。刚好等于PHP的strtotime
}

vector<string> datetime_vec; //因为客户选的日期是不固定的几天,所以做成vec;
vector<long>   datevaluesc_vec;//记录所选日期的秒数
vector<long>   maxdatevaluesc_vec;//记录所选日期 24:00 的秒数
int   rre_num(string    str, string    sub)
{
	int   nRet = 0, nStart = 0;
	while (-1 != (nStart = str.find(sub, nStart)))
	{
		nStart += sub.length();
		++nRet;
	}
	return   nRet;
}
vector<int>week_days;
map<int, int> date_map;//第几天,周几(从 0开始)
void CaculateWeekDay(int y, int m, int d)
{
	static int num = 0;
	if (m == 1) m = 13;
	if (m == 2) m = 14;
	int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
	int value_day = 0;
	switch (week)
	{
	case 1: value_day = 0; break;
	case 2: value_day = 1; break;
	case 3: value_day = 2; break;
	case 4: value_day = 3; break;
	case 5: value_day = 4; break;
	case 6: value_day = 5; break;
	case 7: value_day = 6; break;
	}
	cout << "CaculateWeekDay  num+++++++++++++++++++++++++++" << num << endl;
	week_days.push_back(value_day);
	date_map.insert(pair<int, int>(num, value_day));
	num++;
}
void test_datetime(string str)
{

	string ss_cycle = "";

	string time_a_str = "";
	string  str_time = "";
	//str_time = "20190415;20190416;";//从0位开始 8 17  24003 测试
	//                   8        17		  26
	str_time = "20190618;20190620;";
	int ascnum = rre_num(str_time, ";");
	cout << "ascnum: = " << ascnum << endl;
	for (int i = 0; i < ascnum; i++)
	{
		int num = 0;
		num = i * 8 + i;
		time_a_str = str_time.substr(num, 8);
		cout << "++++++++++++++++++++++++++  ---datetime: " << time_a_str.c_str() << endl;
		datetime_vec.push_back(time_a_str);

		char ss[128];
		memset(ss, 0, sizeof(ss));
		//sprintf();
		string year_value = time_a_str.substr(0, 4);
		string month_value = time_a_str.substr(4, 2);
		string day_value = time_a_str.substr(6, 2);
		string datestr = year_value + "-" + month_value + "-" + day_value + " 00:00:00";
		cout << "=++++++++++++++datestr:" << datestr << endl;
		//sprintf(ss,"%s",datestr);
		strcpy(ss, datestr.c_str());
		cout << "=++++++++++++++ss:" << ss << endl;
		long  value_seconds = convert_str_to_tm(ss);
		cout << "value_seconds " << value_seconds << "     i: " << i << endl;
		datevaluesc_vec.push_back(value_seconds);
		cout << "20190415 value_seconds==========" << value_seconds << endl;
		int year = atoi(year_value.c_str());
		int month = atoi(month_value.c_str());
		int day = atoi(day_value.c_str());
		CaculateWeekDay(year, month, day);

	}
	int num_date = datevaluesc_vec.size();
	for (int i = 0; i<num_date; i++)
	{
		long date1_maxsconds = datevaluesc_vec[i] + 24 * 60 * 60;
		maxdatevaluesc_vec.push_back(date1_maxsconds);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值