使用结构体tm计算两个日期的时间差

目录

  • 写在前面
  • 应用
  • 演示
  • 源码

写在前面

前段时间用到了C++中的时间和日期,当时就想着,学以致用,写个计算时间差的功能。时间仓促,可能代码中还有一些不足的地方,欢迎大家批评指正~

应用

  • sscanf
  • tm
  • mktime

演示

在这里插入图片描述

源码

TimeDiff.cpp

#include 
#include 
#include 
#include 

int TimeDiff(std::string pStart, std::string pEnd);

int main(int argc, char *argv[]){
     std::string str1,str2,str3,str4;
    if (argc < 5){
        printf("用法: ./TimeDiff yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss\n");
        printf("usage: ./TimeDiff 1970-01-01 08:00:00 2020-5-15 08:00:00\n");
        return -1;
    }
     str1 = argv[1];
     str2 = argv[2];
     str3 = argv[3];
     str4 = argv[4];

    TimeDiff(str1 + " " + str2,str3 + " " + str4);
    
    return 0;
}

/**
 * 计算两个日期的时间差
 * 日期格式yyyy-MM-dd HH:mm:ss
 * eg:
 *    input:
 *    2020-5-11 14:00:00
 *    2020-5-15 10:00:00
 *    output:
 *    [2020-5-15 10:00:00] [2020-5-11 14:00:00]相差3天20小时0分钟0秒
 * @param pStart 开始时间
 * @param pEnd 结束时间
 */
int TimeDiff(std::string pStart, std::string pEnd) {
    struct tm start = { 0 }, end = { 0 };

    int s_year, s_mon, s_day, s_hour, s_min, s_sec;
    int e_year, e_mon, e_day, e_hour, e_min, e_sec;
    int ret = 0;
    ret = sscanf(pStart.c_str(), "%d-%d-%d %d:%d:%d", &s_year, &s_mon, &s_day,&s_hour, &s_min, &s_sec);
    //printf("sscanf of start time:%d\n",ret);
    if (ret != 6){
        printf("wrong time format! please check your start time\n");
        return -1;
    }
    ret = sscanf(pEnd.c_str(), "%d-%d-%d %d:%d:%d", &e_year, &e_mon, &e_day, &e_hour,&e_min, &e_sec);
    //printf("sscanf of end time:%d\n",ret);
    if (ret != 6){
        printf("wrong time format! please check your end time\n");
        return -2;
    }

    start.tm_year = s_year - 1900;
    start.tm_mon = s_mon - 1; // 月份0~11
    start.tm_mday = s_day;
    start.tm_hour = s_hour;
    start.tm_min = s_min;
    start.tm_sec = s_sec;

    time_t t1 = mktime(&start); //mktime:测试可用最小值为1970-01-01 08:00:00
    if (t1 < 0){
        printf("start time before 1970-01-01 08:00:00,time is out of range!\n");
        return -3;
    }
    //printf("t1 = %ld\n", t1);

    end.tm_year = e_year - 1900;
    end.tm_mon = e_mon - 1;
    end.tm_mday = e_day;
    end.tm_hour = e_hour;
    end.tm_min = e_min;
    end.tm_sec = e_sec;
    time_t t2 = mktime(&end); // mktime:在32位编译器或操作系统上,time_t最大值为2038-1-19 03:14:07
    //printf("t2 = %ld\n", t2);

    long int diff = t2 - t1;
    if (diff < 0){
        printf("error parameter! check your start time and end time\n");
        return -4;
    }
    //printf("diff = %ld\n", diff);

    int day, hour, min, sec;
    if (diff >= 24 * 60 * 60) { // more than one day
        day = diff / 86400; // 1day = 86400s
        hour = (diff - day * 86400) / 3600;
        min = (diff - hour * 3600 - day * 86400) / 60;
        sec = diff - day * 86400 - hour * 3600 - min * 60;

        while (sec >= 60){
            min += 1;
            sec -=60;
        }

        while (min >= 60){ // hour+1
            hour += 1;
            min -= 60;
        }

        while (hour >= 24){ // day+1
            day += 1;
            hour -= 24;
        }
        printf("[%s] [%s]相差%d天%d小时%d分钟%d秒\n",pEnd.c_str(),pStart.c_str(),day,hour,min,sec);
    } else {
        day = 0;
        hour = diff / 3600;
        min = (diff - hour * 3600) / 60;
        sec = diff - hour * 3600 - min * 60;
        printf("[%s] [%s]相差%d天%d小时%d分钟%d秒\n",pEnd.c_str(),pStart.c_str(),day,hour,min,sec);
    }
    return 0;

}

你可能感兴趣的:(C/C++,Linux,c++,c语言)