C++利用Unix时间戳计算任意时间点时间间隔

本文最后由 森林生灵 于 2017/03/10 18:16:37 编辑

文章目录 (?) [+]

            上节课老师留了一个问题,设计程序计算任意时间点间隔时间,想到每一年的月份天数不一样需要好多判断,感觉好复杂。但联想到前段时间写PHP时的时间处理用到了Unix时间戳,顿时来了灵感,利用Unix时间戳每一秒+1的特性,利用两个时间点的Unix时间戳的差来计算天数岂不是简单的多,而且可以精确到秒。

    cpp_demo

    /**
     * Calculate any point in time intervals(UTC/GMT)
     *
     * @about Use UnixEpoch to calculate the time interval
     * @version   1.4(Visual C++ 6.0)
     * @date      2016-03-31 13:38:56
     * @author    森林生灵 <admin@lanseyujie.com>
     * @link      https://lanseyujie.com
     * @copyright Copyright(c) 2014-2018, lanseyujie.com
     * @license   http://opensource.org/licenses/gpl-2.0.php GPL v2 or later
     */
    
    #include <iostream>
    #include <time.h>   
    #include <string.h>   
    #include <stdlib.h>   
    
    using namespace std;
    
    class UnixEpoch
    {
    private:
        int iY, iM, iD, iH, iMin, iS;
    public:
        long GetTick(char *str_time);
        void CalcTime();
    };
    
    //标准时间转换Unix时间戳
    /*说明:Unix时间戳的0按照ISO 8601规范为:1970-01-01T00:00:00Z,即起始
    时间只能从1970年1月1日0时0分0秒(UTC协调世界时/GMT格林尼治标准时间)
    开始计算经过的秒数,不考虑闰秒。目前大多数的Unix系统中Unix时间戳存储
    为32位,2038年会引发Y2038问题(千年虫危机)*/
    long UnixEpoch::GetTick(char *str_time)  
    {
        struct tm stm;
        memset(&stm, 0, sizeof(stm));
    
        iY = atoi(str_time);
        iM = atoi(str_time + 5);
        iD = atoi(str_time + 8);
        iH = atoi(str_time + 11);
        iMin = atoi(str_time + 14);
        iS = atoi(str_time + 17);
    
        stm.tm_year = iY - 1900;
        stm.tm_mon = iM - 1;
        stm.tm_mday = iD;
        stm.tm_hour = iH;
        stm.tm_min = iMin;
        stm.tm_sec = iS;
    
        return mktime(&stm);
    }
    
    //处理时间数据
    void UnixEpoch::CalcTime()
    {
        char str_time[19];
        long tm_start,interval_day, interval_hour, interval_minute, interval_second;
    
        cout << "Please Input the Start Time!(eg.2011-12-31 11:43:07)" << endl;
        gets(str_time);
        //获取起始时间
        cout << "UnixEpoch Start Time : " << GetTick(str_time) << endl;
        cout << "=====================================================" << endl;
        tm_start = GetTick(str_time) ;
    
        cout << "Please Input the End Time!(eg.2012-01-01 13:55:21)" << endl;
        gets(str_time);
        //获取截止时间
        cout << "UnixEpoch End Time : " << GetTick(str_time) << endl;
        cout << "=====================================================" << endl;
    
        interval_day = GetTick(str_time) - tm_start;
        //计算时间间隔的秒数
        cout << "Unix Time Interval : " << interval_day << endl;
    
        interval_second = (((interval_day % 86400) % 3600) % 60) / 1;
        //计算间隔时间余秒数
        interval_minute = ((interval_day % 86400) % 3600) / 60;
        //计算间隔时间余分钟数
        interval_hour = (interval_day % 86400) / 3600;
        //计算间隔时间余小时数
        interval_day /= 86400;
        //计算间隔时间天数86400s = 24h * 60m * 60s = 1Day
        //倒序计算天/时分/秒是为了防止interval_day执行/=时被重新赋值
    
        cout << "There Are " << interval_day << "Day(s)," << interval_hour << "Hour(s)," << interval_minute << "Minute(s)," << interval_second << "Second(s)" << " From The Time Point!" << endl;
    }
    
    int main()  
    {
        system("color 0a");
    
        UnixEpoch demo;
        demo.CalcTime();
    
        //system("pause");
        return 0;      
    }
    /**
     * Calculate any point in time intervals(UTC/GMT)
     *
     * @about Use UnixEpoch to calculate the time interval
     * @version   1.2(GNU GCC Compiler 6.2.0)
     * @date      2017-03-10 18:07:59
     * @author    森林生灵 <admin@lanseyujie.com>
     * @link      https://lanseyujie.com
     * @copyright Copyright(c) 2014-2018, lanseyujie.com
     * @license   http://opensource.org/licenses/gpl-2.0.php GPL v2 or later
     */
     
    #include <iostream>
    #include <sys/time.h>
    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <stdlib.h>
    
    using namespace std;
    
    class UnixEpoch
    {
        private:
            int iY, iM, iD, iH, iMin, iS;
        public:
            long GetTick(char *str_time);
            void CalcTime();
    };
    
    long UnixEpoch::GetTick(char *str_time)
    {
        struct tm stm;
        memset(&stm, 0, sizeof(stm));
    
        iY = atoi(str_time);
        iM = atoi(str_time + 5);
        iD = atoi(str_time + 8);
        iH = atoi(str_time + 11);
        iMin = atoi(str_time + 14);
        iS = atoi(str_time + 17);
    
        stm.tm_year = iY - 1900;
        stm.tm_mon = iM - 1;
        stm.tm_mday = iD;
        stm.tm_hour = iH;
        stm.tm_min = iMin;
        stm.tm_sec = iS;
    
        return mktime(&stm);
    }
    
    void UnixEpoch::CalcTime()
    {
        char str_time[19];
        long tm_start, tm_end, interval_day, interval_hour, interval_minute, interval_second;
    
        cout << "Please Input the Start Time (e.g.2011-12-31 11:43:05)!" << endl;
        cin.getline(str_time, 20, '\n');
        tm_start = GetTick(str_time);
        cout << "UnixEpoch Start Time:" << tm_start << endl;
        cout << "====================================================" << endl;
    
        cout << "Please Input the End Time (e.g.2014-10-02 09:37:35)!" << endl;
        cin.getline(str_time, 20, '\n');
        tm_end = GetTick(str_time);
        cout << "UnixEpoch End Time:" << tm_end << endl;
        cout << "====================================================" << endl;
    
        interval_day = tm_end - tm_start;
        interval_second = (((interval_day % 86400) % 3600) % 60) / 1;
        interval_minute = ((interval_day % 86400) % 3600) / 60;
        interval_hour = (interval_day % 86400) / 3600;
        interval_day /= 86400;
        cout << "There Are " << interval_day << "Day(s)," << interval_hour << "Hour(s)," << interval_minute << "Minute(s)," << interval_second << "Second(s)" << " From The Time Point!" << endl;
    }
    
    int main()
    {
        UnixEpoch demo;
        demo.CalcTime();
    
        return 0;
    }
    本文标题:C++利用Unix时间戳计算任意时间点时间间隔
    本文链接:https://www.lanseyujie.com/post/cpp-use-unix-stamp-calculate-time-interval.html
    版权声明:本文使用「署名 4.0 国际」创作共享协议,转载或使用请遵守署名协议。
    点赞 0 分享 0

    相关文章