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;

 

### 关于 `time` 函数的介绍 在 C++ 中,`time` 函数用于获取当前日历时间。它定义在 `<ctime>` 头文件中,并返回自 1970 年 1 月 1 日以来经过的时间(以秒为单位)。如果无法检索到时间,则返回 `(time_t)(-1)`。 以下是 `time` 函数的标准签名: ```cpp time_t time(time_t* timer); ``` 参数说明如下: - 如果指针 `timer` 不为空,则函数会将当前时间存储到该位置。 - 返回值是一个 `time_t` 类型的数据,表示当前的日历时间。 下面提供了一个简单的例子展示如何使用 `time` 函数: ```cpp #include <iostream> #include <ctime> // 包含头文件以便使用 time() int main() { std::time_t now = std::time(nullptr); // 获取当前时间 char buffer[26]; // 将时间转换为字符串形式并存入缓冲区 ctime_s(buffer, sizeof(buffer), &now); std::cout << "Current date and time: " << buffer << std::endl; return 0; } ``` 上述代码片段展示了如何调用 `std::time` 来获得当前日期和时间,并通过标准库中的辅助方法将其打印出来[^4]。 需要注意的是,在现代 C++ 编程实践中,推荐优先考虑更高层次的时间处理工具,比如来自 `<chrono>` 的功能集。这些新特性提供了更安全、易读以及类型安全的方式来进行时间和日期操作。 ### 提供跨平台支持注意事项 当开发需要兼容多个操作系统环境的应用程序时,请注意不同系统的实现细节可能略有差异。尽管大多数主流编译器都遵循 POSIX 标准实现了类似的接口行为,但在特定场景下仍需查阅具体目标平台的相关文档确认其确切表现。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值