为什么time()无法正确更新自定义时间?

时间:2019-04-24 04:13:24

标签: c++

我创建了一个DateTime类。如果它是今天的日期,它将给出正确的日期,并且更新就可以了。但是,当我自定义日期/时间时,并不能正确更新我的日期。我用一个简单的项目浓缩了这个大型的DateTime类,希望您可以很好地进行编译。要编译它,请使用一个空的C ++控制台项目。

我已经为此工作了几天。最初,该类具有自己的tm结构,而我摆脱了这一点。现在,该类仅存储原始时间。

DateTime.h

#pragma once

#include <iostream>
#include <ctime>  

class DateTime
{
private:
    time_t m_rawTime;
    bool m_customDateTime;
public: 
    DateTime();

    static DateTime now();
    void update();   
    std::string toLongDateString(); 
    std::string toLongTimeString();  
    void addDate(const time_t rawTimeDifference);  
    void setDate(const int month, const int day, const int year,
        const int hours24 = 0, const int minutes = 0, const int seconds = 0); 
}; 

DateTime.cpp

#include "DateTime.h"
#include <ctime>
#include <iostream>  

DateTime::DateTime()
{
    time(&m_rawTime);
    m_customDateTime = false;
}

DateTime DateTime::now()
{
    DateTime dt;

    time(&dt.m_rawTime);
    dt.m_customDateTime = false;

    return dt;
}

void DateTime::update()
{
    if (!m_customDateTime)
    {
        time(&m_rawTime);
    }
    else
    { 
        time_t diff = difftime(m_rawTime, now().m_rawTime);

        addDate(diff);
    }
}  

std::string DateTime::toLongDateString()
{
    char buffer[30];
    tm *timeInfo = localtime(&m_rawTime);

    strftime(buffer, 30, "%m/%d/%Y", timeInfo);

    return std::string(buffer, strlen(buffer));
}

std::string DateTime::toLongTimeString()
{
    char buffer[30];
    tm *timeInfo = localtime(&m_rawTime);

    strftime(buffer, 30, "%r", timeInfo);

    return std::string(buffer, strlen(buffer));
}  

void DateTime::addDate(const time_t rawTimeDifference)
{ 
    m_rawTime += rawTimeDifference; 
} 

void DateTime::setDate(const int month, const int day, const int year,
    const int hours24, const int minutes, const int seconds)
{
    tm *timeInfo = localtime(&m_rawTime);

    timeInfo->tm_mon = month - 1;
    timeInfo->tm_mday = day;
    timeInfo->tm_year = (year - 1900);
    timeInfo->tm_hour = hours24;
    timeInfo->tm_min = minutes;
    timeInfo->tm_sec = seconds;

    m_rawTime = mktime(timeInfo);

    m_customDateTime = true;
}

main.cpp

#include "DateTime.h" 
#include <iostream>
#include <conio.h>
#include <string>

int main()
{ 
    DateTime dt;

    // Set a future custom date
    dt.setDate(6, 1, 2019, 5, 0, 0);

    // Show the custom time: 06/01/2019 05:00:00 AM
    std::cout << dt.toLongDateString() << " " << dt.toLongTimeString() << std::endl;

    // Wait for five seconds
    std::cout << "Press a key to continue . . . ";
    getch();
    std::cout << std::endl;  

    // Update the custom time (add difference to the custom time so it's 5 seconds later)
    dt.update(); 

    // I'm getting 07/09/2019 12:58:50 PM
    std::cout << dt.toLongDateString() << " " << dt.toLongTimeString() << std::endl;

    getch();
    return 0;
}

自定义日期为06/01/2019 05:00:00 AM,并暂停了5秒钟,我希望输出为06/01/2019 05:00:05 AM。

1 个答案:

答案 0 :(得分:0)

发生的事情是,您的DateTime::update函数采用了设置日期(06/01/2019 05:00:00 AM)和now之间的差值,然后添加了该差值重新回到日期,直到很远的将来为止(6月1日与4月21日之间相差一个多月。