日期的C 秒

C++ Seconds in Date

本文关键字:日期      更新时间:2023-10-16

我正在尝试编写一个我想以" 03.02.2019"和一个类似4的整数以及我想要的代码的款项。要做的就是给我的结果:" 07.02.2019"。我认为我在这里遇到的问题是,我没有正确地分配指针,因为我可以看到代码做对了。它确实计算了" 07.02.2019",但我无法让我回馈结果;-)。请在下面找到代码:

#include <iostream>
#include "Calendar.h"
#include <ctime>
using namespace std;

int main()
{
    Calendar MyDate("03.02.2019");
    Calendar NewDate;
    NewDate = MyDate + 4;
    cout << endl << NewDate.print() << endl;
    cin.get();
    getchar();
    return 0;
}

calender.h:

#pragma once
#include <iostream>
#include <vector>
#include <ctime>
#include <string>
using namespace std;
class Calendar
{
public:
    Calendar();
    Calendar(string thedate);
    /*
    Calendar operator-(const Calendar &TheDate);
    Calendar operator-(const int &numberOfDays);
    */
    Calendar operator+(const int &numberOfDays);
    string print(void);
    void set_Date(string dertag);
    void set_Seconds();
    ~Calendar();
private:
    vector<string>AllTheDates;
    string TheDate;
    int TheYear;
    int TheMonth;
    int TheDay;
    unsigned int YearSeconds;
    unsigned int MonthSeconds;
    unsigned int DaySeconds;
    unsigned int TotalSeconds;
    string *theNewDate2print_ptr=new string;
    //string theNewDate2print;
    bool isdate;
};

calendar.cpp:

#include "Calendar.h"
#include <ctime>
#include <string>
#include <sstream>
using namespace std;
Calendar::Calender()
{
}
Calendar::Calendar(string thedate)
{
    this->set_Date(thedate);
}
/*
Calendar Calendar::operator-(const Calendar &TheDate)
{
}
Calendar Calendar::operator-(const int &numberOfDays)
{
}
}*/
Calendar Calendar::operator+(const int &numberOfDays)
{
    //Calendar NewDate;
    unsigned int TotalSeconds_ = TotalSeconds + (numberOfDays * 24 * 60 * 60);
    time_t timer = TotalSeconds_;
    tm *ltm = localtime(&timer);
    stringstream NewDate_;
    string TheNewDate;
    stringstream theDay;
    stringstream theMonth;
    stringstream theYear;
    string theDay_;
    string theMonth_;
    string theYear_;
    //theNewDate2print_ptr = new string;
    theDay << ltm->tm_mday;
    theMonth << 1+ltm->tm_mon;
    theYear << 1830+ltm->tm_year;
    theDay_ = theDay.str();
    theMonth_ = theMonth.str();
    theYear_ = theYear.str();
    if (theDay_.length() == 1)
        theDay_ = "0" + theDay_;
    if (theMonth_.length() == 1)
        theMonth_ = "0" + theMonth_;
    //NewDate_ << ltm->tm_mday << "." << 1+ltm->tm_mon << "." << 1830+ltm->tm_year;
    TheNewDate = theDay_ + "." + theMonth_ + "." + theYear_;
    //Calendar NewDate(TheNewDate);
    *theNewDate2print_ptr = TheNewDate;
    return TheNewDate;
}
void Calendar::set_Date(string dertag)  
{
    TheDate = dertag;
    TheDay = stoi(dertag.substr(0, 2));
    TheMonth = stoi(dertag.substr(3,2));
    TheYear = stoi(dertag.substr(6,4));
    if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
        isdate = true;
    /*if(isdate)
    throw exception!*/
    set_Seconds();
}
void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}
string Calendar::print()
{
    return (*theNewDate2print_ptr);
}
Calendar::~Calendar()
{
}

感谢您的任何帮助。非常感谢!!!

问候,Marco

Calendar Calendar::operator+(const int &numberOfDays)
{
    ...
    string TheNewDate;
    ...
    return TheNewDate;
}

这将从TheNewDate字符串中构造Calendar对象,该对象将调用构造函数:

Calendar::Calendar(string thedate)
{
    this->set_Date(thedate);
}

set_Date

void Calendar::set_Date(string dertag)  
{
    TheDate = dertag;
    TheDay = stoi(dertag.substr(0, 2));
    TheMonth = stoi(dertag.substr(3,2));
    TheYear = stoi(dertag.substr(6,4));
    if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
        isdate = true;
    /*if(isdate)
    throw exception!*/
    set_Seconds();
}

set_Seconds()

void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}

在构造期间,*theNewDate2print_ptr未分配任何值,字符串为空 - 默认是在condort int new string调用之前初始化的。您需要在任何地方分配它。前任。在set_seconds中:

void Calendar::set_Seconds()
{
    //Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde. 
    YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
    MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
    DaySeconds = TheDay * 24 * 60 * 60;
    TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
    *theNewDate2print_ptr = std::to_string(DaySeconds) + "-" + ....;
}

您的类泄漏内存。它可以分配新对象,并且在任何地方都无法将其释放。还要编写复制构造函数以防止内存泄漏。您应该释放分配的记忆:

Calendar::~Calendar()
{
    delete theNewDate2print_ptr;
}

成员:

    vector<string>AllTheDates;
    string TheDate;
    int TheYear;
    int TheMonth;
    int TheDay;
    unsigned int YearSeconds;
    unsigned int MonthSeconds;
    unsigned int DaySeconds;
    unsigned int TotalSeconds;
    string *theNewDate2print_ptr=new string;
    //string theNewDate2print;
    bool isdate;

看起来像许多重复的变量,它们以不同的格式保持相同的值。要干净。不要重复变量。不要浪费记忆。不要困惑自己。存储一个变量一次。不需要YearSecondsTheYear。如果将日期存储为整数,则无需TheDate ...写转换功能,设置和Geters功能:

    unsigned int TheYear;
    unsigned int YearSeconds() { 
            // TODO: leap years
            return TheYear * number_of_seconds_in_a_year;
    }
    void SetTheYearFromSeconds(unsigned int seconds) {
            // TODO: leap years
            TheYear = seconds / number_of_seconds_in_a_year;
    }
    // etc...
    unsigned int TotalSeconds() { 
            return YearSeconds() + .... ;
    }