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 150 151 152 153 154 155 156 157 158 159 160 161 162
| /*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>
#include <limits.h> /* LONG_MIN et LONG_MAX */
#include <errno.h> /* gestion d'erreur strtol */
/* macros ============================================================== */
static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static char const monthnames [12] [10] = {"Janvier", "Fevrier", "Mars", "Avril", "Mais", "Juin",
"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre"};
static char const daynames[7] [3]= {"Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"};
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private variables =================================================== */
/* private functions =================================================== */
/* Regle d'usage du programme */
void 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");
}
/* Test si l'année est bissextile */
int isbissextil (int y)
{
int out = 0;
if ((( y % 4 == 0) && ( y % 100 != 0)) || (y % 400 == 0))
{
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 ("%2d ", j);
if(!(j%7))
{
printf("\n");
}
}
printf("\n");
return 0;
}
int changeparam (char *str)
{
int val;
int base = 10;
char *end;
errno = 0;
val = strtol (str, &end, base);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0))
{
perror ("strtol");
exit (EXIT_FAILURE);
}
if (end == str)
{
fprintf(stderr, "Pas de chiffre trouvé\n");
exit(EXIT_FAILURE);
}
return val;
}
/* internal public functions =========================================== */
/* entry points ======================================================== */
/* public variables ==================================================== */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int err = 0;
if (argc == 1)
{
showtime ();
}
else if (argc == 4)
{
int year = changeparam (argv[3]);
int month = changeparam (argv[2]);
int day = changeparam (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;
}
else
{
#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);
}
}
else
{
use (argv[0]);
}
return 0;
} |
Partager