C++学习笔记(20161114 ) 根据指定目标时间获得时间戳(秒数)

  • Post author:
  • Post category:其他


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);
}

int main()
{
	char str_time[64] = "2016-11-14 11:43:01";
	char str_time1[64] = "2016-12-1 11:43:01";
	printf("相差:%ds\n",convert_str_to_tm(str_time1)- convert_str_to_tm(str_time));
	system("pause");
    return 0;
}



版权声明:本文为liliangshuai原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。