30 lines
877 B
C++
30 lines
877 B
C++
#include "Date.h"
|
|
|
|
int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
|
|
|
Date::Date(unsigned int day, unsigned int month, unsigned int year) : day(day), month(month), year(year) {}
|
|
|
|
std::string Date::toString() const {
|
|
return std::to_string(day) + "." + std::to_string(month) + "." + std::to_string(year);
|
|
}
|
|
|
|
Date Date::getDateFromString(std::string date) {
|
|
|
|
for (int i = 0; i < date.size(); ++i) {
|
|
if ((date[i] < '0' or date[i] > '9') and date[i] != '.') throw UnparseableDateException(date);
|
|
}
|
|
|
|
std::string tmp;
|
|
unsigned int dd[3] = {0,0,0};
|
|
std::stringstream stram(date);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
getline (stram,tmp, '.');
|
|
dd[i] = std::stoi(tmp);
|
|
}
|
|
|
|
if(dd[0] > months[dd[1] - 1] or dd[1] > 12) throw InvalidDateException(date);
|
|
|
|
return {dd[0], dd[1], dd[2]};
|
|
}
|