#include <time.h>
//判断两个日期 参考: http://bbs.chinaunix.net/thread-1027013-1-1.html
void chekTime()
{
static time_t oldTime = time(NULL);
time_t newTime = time(NULL);
if (oldTime / 86400 != newTime / 86400)
{
printf("diffrent day");
}
else
{
printf("same day");
}
}
//判断是否是闰年
bool isRunyear(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
return true;
}
else {
return false;
}
}
//获取某年第n天是几月几号
struct MonthDay {
int Day;
int month;
};
bool getYearMonth(int year, int day, MonthDay& retYD) {
int iArr[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int count = 0, count1 = 0;
if (isRunyear(year)) //表明是闰年
{
iArr[1] = 29;
}
for (int i = 0; i < 11; i++)
{
count1 = count;
count += iArr[i];
if (day >= count1 && day <= count) {
retYD.month = i + 1;
retYD.Day = day - count1;
break;
}
}
return true;
}
//获取某年某月是第几天
int getYearDay(int year, int month, int day)
{
int sum = 0;
//计算某月以前月份的天数
switch (month) {
case 1:sum = 0;break;
case 2:sum = 31;break;
case 3:sum = 59;break;
case 4:sum = 90;break;
case 5:sum = 120;break;
case 6:sum = 151;break;
case 7:sum = 181;break;
case 8:sum = 212;break;
case 9:sum = 243;break;
case 10:sum = 273;break;
case 11:sum = 304;break;
case 12:sum = 334;break;
default:
return -1;
break;
}
//月份天数加上这个月的日数
sum = sum + day;
if (month>2 && isRunyear(year))sum = sum + 1;
return sum;
}
//获取某年某月的周期
std::string getWeekstring(const std::string strTime="2018-8-16") //返回08.13~08.19
{
int year, month, day, hour = 0, minute = 0, second = 0;
sscanf(strTime.c_str(), "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second);
int sumDay = getYearDay(year, month, day);
int inWeekDay = sumDay % 7;
int lastWeekDay = sumDay + 7 - inWeekDay;
if (inWeekDay == 0)
{
inWeekDay = 7;
lastWeekDay = sumDay;
}
int headWeekDay = sumDay - inWeekDay + 1;
std::string retMDstr = "";
MonthDay td, td2;
getYearMonth(year, headWeekDay, td);
getYearMonth(year, lastWeekDay, td2);
char temp[32];
sprintf(temp, "%02d.%02d~%02d%02d", td.month, td.Day, td2.month, td2.Day);
retMDstr = temp;
return retMDstr;
}
//获取当前时间距离0点还有多少秒
int getToZeroTick()
{
return (86400 - time(NULL)% 86400 - 8 * 3600) > 0 ? 86400 - time(NULL) % 86400 - 8 * 3600 : 86400*2 - time(NULL) % 86400 - 8 * 3600;
}