Solved

(difference between two date)  to number of days

Posted on 2003-12-11
7
459 Views
Last Modified: 2010-04-01
Hy boys
help me to solve this problem...

I need an example that from two date, calculates the difference.And then calculate the number of days of this difference.
This is my class date:

// date.cpp

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


//numero di giorni per ogni mese
const int Date::days[] = {  0, 31, 28, 31, 30, 31, 30,
                           31, 31, 30, 31, 30, 31 };

//costruttore
Date::Date( int m, int d, int y ) { setDate( m, d, y ); }


//set del giorno con controlli
void Date::setDay(int dd)
{

     giornoCheck=1;

     if ( month == 2 && leapYear( year ) )
        { if   ( dd >= 1 && dd <= 29 ) day=dd;
       
          else { day=1; giornoCheck=0;}
      }
   
    else
      { if  ( dd >= 1 && dd <= days[ month ] ) day=dd;
           
        else {day=1; giornoCheck=0;}    
   
      }
}

//set del mese con controlli
void Date::setMonth(int mm)
{
     meseCheck=1;

     if ( mm >= 1 && mm <= 12 ) month= mm;
     else {month= 1; meseCheck=0; }

}

//set dell'anno con controlli
void Date::setYear(int yy)
{
     annoCheck=1;

     if ( yy >= 1900 && yy <= 2100) year = yy;
     else {year = 2002; annoCheck=0; }
}

// Set della data
void Date::setDate( int mm, int dd, int yy )
{
   setMonth(mm);
   setYear(yy);
   setDay(dd);
   
}

// le 3 get

int Date::getMonth() const {
 
  return month;
}

int Date::getDay() const {

return day;
}

int Date::getYear() const {

return year;
}





//per usare l'operatore = anche con le date
bool Date::operator==(const Date & d) const {
 
 return (d.month==month)&&(d.day==day)&&(d.year==year);
 
}

//per usare l'operatore < anche con le date
bool Date::operator<(const Date & d) const {
   
    return (day+ ( month * days[month])+  (365*year) ) < ( d.day + (d.month * d.days[month]) + (365* d.year) );
}

//per usare l'operatore + anche con le date
Date Date::operator+( int g) const {
   
   return Date( month , day+g , year );
}

//per usare l'operatore - anche con le date
Date Date::operator-( int g) const {

 return Date( month , day-g , year );
 
}

//resoconto di validità totale
bool Date::isValid()
{
   
    if ( giornoCheck==1 && annoCheck==1 && meseCheck==1)    return true;
   
    else return false;

}


//singoli resoconti di validità

bool Date::isDayValid()
{
  return giornoCheck;
}

bool Date::isMonthValid()
{
  return meseCheck;
}

bool Date::isYearValid()
{
  return annoCheck;
}



// Operatore di Preincremento
Date & Date::operator++()
{
   helpIncrement();
   return *this;
}

// Operatore di postincremento

Date Date::operator++( int )
{
   Date temp = *this;
   helpIncrement();

   // oggetto temporaneo salvato e non incrementato
   return temp;
}

// aggiunge dei giorni ad una data
const Date &Date::operator+=( int additionalDays )
{
   for ( int i = 0; i < additionalDays; i++ )
      helpIncrement();

   return *this;
}

//Controllo se bisestile o no

bool Date::leapYear( int y ) const
{
   if ( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) )
      return true;   // bisestile
   else
      return false;  // non bisestile
}

//controlla che il giorno sia compatibile col mese
bool Date::endOfMonth( int d ) const
{
   if ( month == 2 && leapYear( year ) )
      return d == 29; // per febbraio
   else
      return d == days[ month ];
}

//incrementa la data
void Date::helpIncrement()
{
   if ( endOfMonth( day ) && month == 12 ) {  // finito l'anno
      day = 1;
      month = 1;
      ++year;
   }
   else if ( endOfMonth( day ) ) {            // finito il mese
      day = 1;
      ++month;
   }
   else       // tutto ok si può incrementare
      ++day;
}

// per la stampa, overload dell'operatore <<
ostream &operator<<( ostream &output, const Date &d )
{
   static char *monthName[ 13 ] = { "", "Gennaio",
      "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno",
      "Luglio", "Agosto", "Settembre", "Ottobre",
      "Novembre", "Dicembre" };

   output << d.getDay() << ' '
          << monthName[ d.getMonth() ] << ' '<<d.getYear();

   return output;   //operatore a cascata
}

//per l'acquisizione, overload dell'operator >>
istream &operator>>( istream &input, Date &d )
{

  int aux;

  input >> aux;
  d.setDay(aux);
  input.ignore();
  input >> aux;
  d.setMonth(aux);
  input.ignore();
  input >> aux;
  d.setYear(aux);

  return input;

}            


//legge la data del pc
Date Date::today()
{
  time_t tempo_cal( time(NULL) );
  struct tm * tempo_oggi ( localtime(&tempo_cal) );

  return Date( tempo_oggi->tm_mon +1, tempo_oggi->tm_mday,tempo_oggi->tm_year +1900 );
}                    

0
Comment
Question by:giggiaus
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 9921228
try this

int Date::julianDay() const
{
    int mon    = month - 1;
    int leap   = (mon > 1 && leapYear())? 1 : 0;
    int days[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, };
    return days[mon] + leap + day;
}



long Date::getDateInDays() const
{
    int years       = year - 1;
    int centurys    = years / 100;
    int centurys4   = years / 400;
    int years4      = years / 4;

    return centurys4 - centurys + years4 + (years * 365) + julianDay();
}


int Date::dateDiff(const Date& dt)
{
      return dt.getDateInDays() - getDateInDays();
}


Regards Alex

0
 
LVL 13

Expert Comment

by:SteH
ID: 9921241
Yes, I see this question. And I mentioned that problem already in the old Q. I see that you changed the operator+= accordingly. The function helpincrement () is for the increment operator useful but I thnik you should reconsider the operator+=. Why all this if your question was completly different? The approach of operator+= is very inefficient when you add a large amount of days. If for example additionaldays = 1000 (~ 3 years) you need a whole lot of calls to helpincrement(). The easier way would be to represent the day in a way where you can use normal additions (and substractions, hint) and then convert it back to its representation it has. Does this bring you on a way? If not I can ive some more hints. But it seems to be something like homework/assignment so I won't tell you the solution.

0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9921261
It would help to see your Date.h file.  Also have you tried to solve this problem yourself at all ?  Post any of your attempts aswell please.

:)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 19

Expert Comment

by:mrwad99
ID: 9921269
>> But it seems to be something like homework/assignment so I won't tell you the solution.


Rock on SteH
0
 

Author Comment

by:giggiaus
ID: 9921362
I don't understand if i have to add this code to this class or not.
I need something to add in my code.....
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 20 total points
ID: 9921453
add these 3 declarations to your class Date in date.h

class Date
{
     ....
public:
     int julianDay() const;
     long getDateInDays() const;
     int dateDiff(const Date& dt);
     ....



};


take the code from above

and add it somewhere to date.cpp

You get the difference in days for example

void main
{
     Date date1 (12, 31, 2002);
     Date date2 (3, 31, 2003);

     int days = date1.dateDiff(date2);
}

Regards, Alex
0
 
LVL 13

Expert Comment

by:SteH
ID: 9921462
Yes, you should add the code from itsmeandnobodyelse to your class. And then you can be happ and use it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 128
typedef std::deque<BYTE> ByteVector is broken in vs2012 23 88
Safe conversion? 4 72
Android development question 2 54
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question