#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// 时间字符串转时间戳
time_t TimeStringToTimestamp(string strTime)
{
struct tm tmTime;
sscanf(strTime.c_str(), "%4d-%2d-%2d %2d:%2d:%2d",
&tmTime.tm_year,
&tmTime.tm_mon,
&tmTime.tm_mday,
&tmTime.tm_hour,
&tmTime.tm_min,
&tmTime.tm_sec
);
tmTime.tm_year -= 1900;
tmTime.tm_mon--;
tmTime.tm_isdst = -1;
return mktime(&tmTime);
}
// 时间戳转时间字符串
string TimestampToTimeString(time_t time)
{
struct tm* tmTime = localtime(&time);
char szTime[128] = { 0 };
strftime(szTime, sizeof(szTime), "%Y-%m-%d %H:%M:%S", tmTime);
return szTime;
}
// 去除秒
void EraseSecond(time_t& time)
{
struct tm struTime;
localtime_s(&struTime, &time);
struTime.tm_sec = 0;
time = mktime(&struTime);
}
// 比较24小时部分的大小
int CompareDayTime(time_t time1, time_t time2)
{
struct tm tm1, tm2;
localtime_s(&tm1, &time1);
localtime_s(&tm2, &time2);
if (tm1.tm_hour > tm2.tm_hour)
{
return 1;
}
else if (tm1.tm_hour < tm2.tm_hour)
{
return -1;
}
if (tm1.tm_min > tm2.tm_min)
{
return 1;
}
else if (tm1.tm_min < tm2.tm_min)
{
return -1;
}
if (tm1.tm_sec > tm2.tm_sec)
{
return 1;
}
else if (tm1.tm_sec < tm2.tm_sec)
{
return -1;
}
return 0;
}