Ajout getAge tortue
This commit is contained in:
parent
585f568930
commit
9dd4721d1d
2 changed files with 41 additions and 8 deletions
|
@ -30,7 +30,7 @@ public:
|
|||
|
||||
static int getNbTortue();
|
||||
std::string getNaissance() const;
|
||||
int getAge() const;
|
||||
std::string getAge() const;
|
||||
const std::string &getSexe() const;
|
||||
|
||||
std::string getInfo();
|
||||
|
|
|
@ -14,13 +14,46 @@ Tortue::Tortue(const string &nom, const string &sexe) : nom(nom), sexe(sexe) {
|
|||
nbTortue ++;
|
||||
}
|
||||
|
||||
/*int Tortue::getAge() const {
|
||||
int ageSec = std::time(nullptr) - naissance;
|
||||
int ageHour = ageSec/(60*60);
|
||||
int ageDay = ageHour/24;
|
||||
int age = ageDay / 365.25;
|
||||
return age;
|
||||
}*/
|
||||
std::string Tortue::getAge() const {
|
||||
|
||||
time_t rawtime;
|
||||
|
||||
struct tm * timeinfo ;
|
||||
std::string date = naissance;
|
||||
std::string message ="";
|
||||
|
||||
int annee = std::stoi(date.substr(6,9));
|
||||
int mois = std::stoi(date.substr(3,4));
|
||||
int jour = std::stoi(date.substr(0,2));
|
||||
int h = 0;
|
||||
int m = 0;
|
||||
int s = 0;
|
||||
|
||||
time(&rawtime);
|
||||
timeinfo = localtime(&rawtime);
|
||||
timeinfo->tm_year = annee - 1900;
|
||||
timeinfo->tm_mon= mois - 1;
|
||||
timeinfo->tm_mday = jour;
|
||||
timeinfo->tm_hour = h;
|
||||
timeinfo->tm_min = m;
|
||||
timeinfo->tm_sec = s;
|
||||
|
||||
|
||||
long naiss =std::mktime(timeinfo);
|
||||
//std::cout << naiss << std::endl;
|
||||
|
||||
|
||||
std::time_t now = std::time(nullptr);
|
||||
long time = now - naiss;
|
||||
//std::cout << time <<std::endl;
|
||||
|
||||
int agean = time / 31557600;
|
||||
time = time - (agean*31557600);
|
||||
int agemois = time / 2592000;
|
||||
|
||||
message = to_string(agean) + " ans et " + to_string(agemois) + " mois";
|
||||
return message;
|
||||
}
|
||||
int Tortue::nbTortue = 0;
|
||||
|
||||
const std::string &Tortue::getNom() const {
|
||||
|
|
Loading…
Reference in a new issue