MarcoH
MarcoH

Reputation: 7

C++ Seconds in Date

I'm trying to write a class in which I would like to give as input a Date in the format "03.02.2019" and an integer like 4 and what I would like the code to do is the give me as a result: "07.02.2019". I think the problem that I have here is that I am not assigning the pointers correctly because I can see that the code is doing something right. It does calculate "07.02.2019" but I can't make it to give me back the result ;-). Please find the code below:

#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()
{

}

Thank you for any help. Much appreciated!!!

Regards, Marco

Upvotes: 0

Views: 201

Answers (1)

KamilCuk
KamilCuk

Reputation: 140900

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

This will construct Calendar object from TheNewDate string, which will call the constructor:

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();
}

and 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;
}

During construction the *theNewDate2print_ptr is not assigned any value, the string is empty - it was default initialized before the constructor int the new string call. You need to assign it anywhere. Ex. in the 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) + "-" + ....;
}

Your class leaks memory. It allocates new object and does not free it anywhere. Also the copy constructor is to be written to protect against memory leaks. You should free the memory you allocated:

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

The members:

    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;

look like a lot of duplicated variables that are holding the same value in different formats. Be clean. Don't duplicate variables. Don't waste memory. Don't confuse yourself. Store one variable once. There is no need for YearSeconds and TheYear. There is no need to TheDate if you store the date as integers... Write conversion functions, setters and getters functions:

    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() + .... ;
    }

Upvotes: 1

Related Questions