1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| /*Pour ne pas compiler avec un compilateur C++*/
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
/* Include ============================================================= */
#include <stdio.h> /* printf ...*/
#include <stdlib.h> /* define ...*/
#include <string.h>
#include <time.h>
/* macros ============================================================== */
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char monthnames [12] [10] = {"Janvier", "Fevrier", "Mars", "Avril", "Mais", "Juin",
"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre"};
char daynames[7] [3]= {"Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"};
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */
/* private functions =================================================== */
/* Regle d'usage du programme */
int use (char name[])
{
printf ("Utilisation : %s\n", name);
printf ("Utilisation : %s [day] [month] [year]\n", name);
printf ("L'année ne doit pas être en dessous de 1970\n");
return 0;
}
/* Test si l'année est bissextile */
int isbissextil (int y)
{
int out = 0;
if(!((y % 4 && y % 100 && y % 400) || (y % 4 && !(y % 100))))
{
days[2] = 29;
out = 1;
}
return out;
}
/* Affiche la date d'aujourd'hui
par Emmanuel Delahaye */
int showtime (void)
{
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char s[64];
strftime (s, sizeof s, "%d/%m/%Y", &tm_now);
printf ("Aujourd'hui : %s\n", s);
return 0;
}
/* Quel jour correspond à la date */
int mydays(int d, int m, int y)
{
int jour;
jour = 367*y-7*(y+(m+9)/12)/4-3*((y+(m-9)/7)/100+1)/4+275*m/9+d+1721029;
jour++;
printf ("La date est %s %d %s %d\n", daynames[jour % 7], d, monthnames[m-1], y);
return 0;
}
/* affichage du calendrier en console */
/* Todo actuellement incomplete */
int affichage (int d, int m, int y)
{
int j;
printf ("Le jour J est %d\n", d);
printf ("--- %s %d ---\n", monthnames[m-1], y);
for (j=0; j<8; j++)
{
printf ("%s ", daynames[j]);
}
printf ("\n");
for (j = 1; j <= days[m]; j++)
{
printf ("%d ", j);
if(!(j%7))
{
printf("\n");
}
}
return 0;
}
/* internal public functions =========================================== */
/* entry points ======================================================== */
/* public variables ==================================================== */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
unsigned int year, month, day;
int err = 0;
/* Test si il y a le bon nombre d'argument */
if (argc > 4 || argc == 2)
{
err = 1;
}
if (argc == 1)
{
showtime();
return 0;
}
year = atoi (argv[3]);
month = atoi (argv[2]);
day = atoi (argv[1]);
/* Test si mois est bien compris entre 1 et 12
et si l'année est strictement supérieur a 1971*/
if (month > 12 || month < 1 || year < 1971 || day > 31 || day < 1)
{
err = 1;
}
if (!err)
{
#if 0
/* L'année est-elle bissextile */
if(isbissextil (year))
{
printf ("L'année est bissextile\n");
}
else
{
printf ("L'année n'est pas bissextile\n");
}
#endif
isbissextil (year);
mydays(day, month, year);
affichage (day, month, year);
}
if (err)
{
use(argv[0]);
}
return 0;
} |
Partager