M2103-TP2-Exo-5-Corrigé

Date.h

/**
 *
 * \file    Date.h
 *
 * \authors M. Laporte, D. Mathieu
 *
 * \date    01/02/2008
 *
 * \version V1.0
 *
 * \brief   Declaration de la classe Date
 *
 **/
#ifndef __DATE_H__
#define __DATE_H__

namespace nsUtil
{
    class Date 
    {
        unsigned myDay;
        unsigned myMonth;
        unsigned myYear;

        bool     isBissextile;

      public :
        Date (unsigned day = 0, unsigned month = 0, unsigned year = 0);

        void     reset        (void);
        unsigned getQuantieme (void)                 const;
        Date     getLaVeille  (void)                 const;
        void     display      (bool textuel = false) const;


    }; // Date 
    
} // namespace nsUtil

#endif /* __DATE_H__ */

Date.cpp

/**
 *
 * \file    Date.cpp
 *
 * \uthors M. Laporte, D. Mathieu
 *
 * \ate    07/12/2011
 *
 * \ersion V1.0
 *
 * \rief   Definition des methodes de la classe Date 
 *
 **/
#include <iostream>
#include <iomanip>       // setw()
#include <string>

#include "Date.h"

using namespace std;

namespace
{
    const string libelleMonth [] =
                   { "janvier", "février", "mars", "avril",
                     "mai", "juin", "juillet", "août" ,
                     "septembre", "octobre", "novembre", "décembre" };

    const unsigned tabNbDays [] = {31, 28, 31, 30, 31, 30, 31,
                                   31, 30, 31, 30, 31, 30, 31};

    unsigned getNbDaysInMonth (unsigned month, bool _isBissextile) 
    {
        return (_isBissextile && month == 2) ? 29 : tabNbDays [month - 1];

    } // getNbDaysInMonth()

} // namespace

#define DATE nsUtil::Date

void DATE::reset (void) 
{
    myDay = myMonth = myYear = 0;
    isBissextile  = false;

} // reset()

DATE::Date (unsigned day  /* = 0 */, unsigned month /* = 0 */,
              unsigned year /* = 0 */) 
    : myDay (day), myMonth (month), myYear (year)
{
    if ((day == 0 || month == 0 || year == 0) ||
        (year < 1900 || year > 2010)         ||
        (month < 1    || month  > 12)           ||
        (day   < 1))
    {
        reset();
        return;
    }
    myIsBissextile =  (year % 400 == 0) ||
                     ((year % 4 == 0) && (year % 100 != 0));
    if (day > getNbDaysInMonth (month, isBissextile))
    {
        reset();
        return;
    }

} // Date()

unsigned DATE::getQuantieme (void) const 
{
    if (myYear == 0) return 0;

    unsigned nb = myDay;
    for (unsigned i (1); i < myMonth; ++i) 
        nb += getNbDaysInMonth (i, isBissextile);

    return nb;

} // GetQuantieme()

DATE DATE::getLaVeille (void) const 
{
    if (myYear == 0) return Date();

    Date yesterday (*this);

    if (myDay > 1) 
    {
        --yesterday.myDay;
        return yesterday;
    }
    if (myMonth > 1)
    {
        --yesterday.myMonth;
        yesterday.myDay = getNbDaysInMonth (yesterday.myMonth, 
                                            yesterday.isBissextile);
        return yesterday;
    }
    if (myYear > 1900)
    {
        --yesterday.myYear;
        yesterday.isBissextile =  (yesterday.myYear % 400 == 0) ||
                                   ((yesterday.myYear % 4 == 0) &&
                                    (yesterday.myYear % 100 != 0));
        yesterday.myMonth = 12;
        yesterday.myDay = 31;
        
        return yesterday;
    }
    yesterday.reset();

    return yesterday;

} // getLaVeille()

void DATE::display (bool textuel /* = false */) const
{
    if (myYear == 0)
    {
        cout << "Date invalide";
        return;
    }
    if (Textuel)
        cout << myDay << ' ' << libelleMonth [myMonth - 1] << ' '
             << myYear;
    else
        cout << setfill ('0') << setw(2)
             << myDay << '/'  << setw(2) <<  myMonth << '/' << myYear
             << setfill (' ');

} // display()

#undef DATE