Code :
/* -ed- headers manquants */ #include <stdio.h> #include <time.h> /* -ed- Fonction non exportee (pas de prototype separe). Ajoute 'static' Le bon type retour est time_t. Accepter les chaines non modifiables */ static time_t datetostr(char const *str) { /* -ed- mieux vaut initialiser tous les champs. */ struct tm tt = { 0 }; int day, year, months; /* -ed- manque test du code retour. */ int n = sscanf(str, "%d/%d/%d", &day, &months, &year); if (n == 3) { /* -ed- rendu inutiles par l'init tt.tm_sec = 0; tt.tm_min = 0; tt.tm_hour = 0; */ tt.tm_mday = day; /* -ed- bien vu. */ tt.tm_mon = months - 1; if (year >= 1970) { tt.tm_year = year - 1900; /* -ed- tt.tm_isdst = -1; non. mktime() s'occupe de tout. */ /* -ed- manque un '\n'*/ printf("%d %d %d\n", day, months, year ); } else { "The correct format is YYYY with year >= 1970\n" , str); } } else { printf("input error: '%s'\n", str ); } return mktime(&tt); } int main (void) { time_t epoch = datetostr("25/12/2005" ); printf ("epoch = %ld\n", (long int) epoch ); return 0; }
|