Page d'accueil Description du projet
/******************************************
 *
 *   Cedric Pradalier   2001
 *   mail : http://cedric.pradalier.free.fr/mail.html
 *
 *****************************************/



#include "output.h"
#include "date.h"
#include <string.h>

/*******************************************************

  Implementation de la representation des dates


********************************************************/

/* implantation physique des nom de jour */
char * DayStrings[7] = {"Lundi","Mardi","Mercredi","Jeudi",
            "Vendredi","Samedi","Dimanche"};
/* implantation physique des longueurs de mois */
char MonthLength[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

/* Renvoie le nom correspondant a un Jour J */
char * GetDayString(Jours j)
{
    return DayStrings[j];
}

/* Reciproquement : Identifie une chaine comme etant un jour 
   Si possible */
Jours GetDayofS(char * S)
{
    for (int i=0;i<7;i++)
        if (strcasecmp(S,DayStrings[i]) == 0)
            return (Jours) i;
    return NonJour;
}


/* Cette annee est elle bisextile */
int IsLeapYear(int year)
{
    if ((year % 400) == 0)
        return 1;
    if ((year % 100) == 0)
        return 0;
    if ((year % 4) == 0)
        return 1;
    return 0;
}

/* Renvoie le nb de jour d'un mois en fonction
   de l'annee : Acces direct au tableau sauf 
   pour fevrier
   M : mois entre 1 et 12 */
int GetMonthLength(int M,int Y)
{
    M--;
    if (M==1)
        return MonthLength[M] + IsLeapYear(Y);
    else
        return MonthLength[M];
}

/* Longeur de l'annee Y - Prise en compte
   des annees bisextile */
int GetYearLength(int Y)
{
    return 365 + IsLeapYear(Y);
}

/* Le jour correspondant au 1er janvier de l'annee */
Jours GetFirstDay(int Year)
{
    int i,N=0;
    if (Year > 2000)
    {   /* Juste un comptage modulo */
        N = (int) Samedi;
        for(i=2000;i<Year;i++)
            N+=GetYearLength(i);
        return (Jours)(N % 7);
    }
    if (Year < 2000)
    {   /* Dans ce cas, c'est de l'arithmetique bizarre
           mais ca marche */
        N = -(int) Samedi;
        for(i=1999;i>=Year;i--)
            N+=GetYearLength(i);
        return (Jours)(7 - (N % 7));
    }
    // (le dernier cas) if (Year == 2000)
    return Samedi;
}

CDate::CDate(const CDate & other)
{
    Day = other.Day;
    Month = other.Month;
    Year = other.Year;
}

CDate::CDate(char D,char M,int Y)
{
    Day = D;
    Month = M;
    Year = Y;
}


/* Date code sur le long YYYYMMDD */
CDate::CDate(long l)
{
    Year = l / 10000;
    l = l % 10000;
    Month = l / 100;
    Day = l % 100;
}

bool CDate::operator==(CDate D2) const
{
    return (Year == D2.Year) && (Month == D2.Month) && (Day == D2.Day);
}


bool CDate::operator<(CDate D2) const
{
    long l1,l2;
    l1 = Year * 10000 + Month * 100 + Day;
    l2 = D2.Year * 10000 + D2.Month * 100 + D2.Day;
    return l1 < l2;
    
}

bool CDate::IsBetween(CDate D1,CDate D2) const
{
    long l1,l2,l3;
    l1 = Year * 10000 + Month * 100 + Day;
    l2 = D1.Year * 10000 + D1.Month * 100 + D1.Day;
    l3 = D2.Year * 10000 + D2.Month * 100 + D2.Day;
    return (l1>=l2) && (l1<=l3);
}
    
            

void CDate::Print()
{
    sprintf(outbuf,"%02i/%02i/%04i",Day,Month,Year);FlushBuffer();
}

bool CDate::Read(FILE * fp)
{
    int Mth,Dy;
    fscanf(fp,"%d / %d / %d",&Dy,&Mth,&Year);
    Day = Dy;
    Month = Mth;
    return ((Year >= 0) && (Month > 0) && (Month <= 12)
        && (Day > 0) 
        && (Day <= GetMonthLength(Month,Year)));
}   

/* Le jour dans l'annee pour une date donnee,On compte */
int CDate::GetDayinYear() const
{
    int i,N=0;
    for (i=1;i<Month;i++)
        N+=GetMonthLength(i,Year);
    N=N + (int)Day;
    return N;
}



/* Le jour de la semaine pour une date donnee */
Jours CDate::GetDayinWeek() const
{
    Jours j = GetFirstDay(Year);
//  sprintf(outbuf,"\n Premier Jour : %s \n",GetDayString(j));FlushBuffer();
    return (Jours)(((int)j + GetDayinYear() - 1) % 7);
}
    
    
/*
int main()
{
    CDate D1(23,8,1978),D2(9,10,1978),D3(10,10,1996),D4(15,4,2001);
    sprintf(outbuf,"1- ");FlushBuffer();D1.Print();
    sprintf(" \n");FlushBuffer();
    sprintf(outbuf,"2- ");FlushBuffer();D2.Print();
    sprintf(" \n");FlushBuffer();
    sprintf(outbuf,"3- ");FlushBuffer();D3.Print();
    sprintf(" \n");FlushBuffer();
    sprintf(outbuf,"4- ");FlushBuffer();D4.Print();
    sprintf(" \n");FlushBuffer();
    sprintf(outbuf,"D4  : jour %s\n ",GetDayString(D4.GetDayinWeek()));FlushBuffer();
}
*/