计算两个时间点间相差的时间间隔
2017-07-06 16:08
281 查看
给出两个时间点,计算它们之间相差的时间间隔:
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
using namespace std;
int standard_to_stamp(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
return (int)mktime(&stm);
}
string num_to_str(int i)
{
stringstream ss;
ss << i;
return ss.str();
}
void time_diff(char *str_time1, char *str_time2)
{
int time1 = standard_to_stamp(str_time1);
int time2 = standard_to_stamp(str_time2);
int diff = time2 - time1;
int hour = diff / 3600;
int minute = diff / 60 % 60;
int second = diff % 60;
string descStr = "两个时间段相差为:";
std::string timestr = "";
if (hour > 0)
{
timestr += num_to_str(hour);
timestr += "小时";
}
if (minute > 0)
{
timestr += num_to_str(minute);
timestr += "分钟";
}
if (second > 0)
{
timestr += num_to_str(second);
timestr += "秒";
}
cout << descStr + timestr << endl;
}
int main()
{
time_diff("2017:07:06 15:20:00", "2017:07:06 21:50:55");
return 0;
}
运行结果如下:
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string>
using namespace std;
int standard_to_stamp(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
return (int)mktime(&stm);
}
string num_to_str(int i)
{
stringstream ss;
ss << i;
return ss.str();
}
void time_diff(char *str_time1, char *str_time2)
{
int time1 = standard_to_stamp(str_time1);
int time2 = standard_to_stamp(str_time2);
int diff = time2 - time1;
int hour = diff / 3600;
int minute = diff / 60 % 60;
int second = diff % 60;
string descStr = "两个时间段相差为:";
std::string timestr = "";
if (hour > 0)
{
timestr += num_to_str(hour);
timestr += "小时";
}
if (minute > 0)
{
timestr += num_to_str(minute);
timestr += "分钟";
}
if (second > 0)
{
timestr += num_to_str(second);
timestr += "秒";
}
cout << descStr + timestr << endl;
}
int main()
{
time_diff("2017:07:06 15:20:00", "2017:07:06 21:50:55");
return 0;
}
运行结果如下:
相关文章推荐
- c#如何计算两个日期相差几年几个月?C#日期间隔 c#时间间隔
- python的N个小功能(文本字段对应数值,经纬度计算距离,两个时间点计算时间间隔)
- php计算两个日期的相差天数,处理时间间隔
- java计算两个日期之间的时间间隔
- 计算两个时间相差多少年月日的sql算法
- 函数计算两个时间间隔年数,月数,天数
- 计算两个日期的时间间隔
- SQL计算实际工作日(天)及两个时间(工作日)间隔(小时)!
- java计算两个时间相差(天、小时、分钟、秒)
- 使用TIMESTAMPDIFF计算两个时间戳之间的时间间隔需要注意的细节
- java 计算两个时间之间的间隔
- 计算两个时间的相差天数。。
- sqlserver 触发器/计算两个时间的间隔天数/查询倒数第二条数据
- 计算两个日期的时间间隔
- 计算两个日期的时间间隔
- iPhone开发--计算两个日期的时间间隔 年 月 日
- C# 计算两个时间的间隔
- C#日期间隔:如何计算两个日期相差几年几个月?
- 计算两个日期的时间间隔
- mysql 计算两个时间的间隔