c++ time应用


#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;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值