#include <iostream>
#include "datehelper.h"
int GetDaysOfMonth(const int month, const int year) noexcept
{
if (month < 1 || month > 12) {
return 0;
}
int day_info[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day_info_leap_year[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const auto is_leap_year =
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
return is_leap_year ? day_info_leap_year[month - 1] : day_info[month - 1];
}
int main()
{
DateHelper test_data[] = {{1, 2013, 31}, {2, 2013, 28}, {3, 2013, 31},
{4, 2013, 30}, {5, 2013, 31}, {6, 2013, 30},
{2, 1940, 29}, {7, 2013, 31}, {8, 2013, 31},
{2, 2000, 29}, {9, 2013, 30}, {10, 2013, 31},
{2, 1700, 28}, {11, 2013, 30}, {12, 2013, 31}};
for (const auto& date : test_data) {
const auto days = GetDaysOfMonth(date.month, date.year);
if (days != date.days) {
std::cout << "ERROR: Year: " << date.year << ", Month: " << date.month
<< ", Days " << days << " (Expected " << date.days << ")\n";
}
else {
std::cout << "Year: " << date.year << ", Month: " << date.month
<< ", Days " << days << "\n";
}
}
return 0;
}
#ifndef DATEHELPER_H_
#define DATEHELPER_H_
struct DateHelper {
int month;
int year;
int days;
};
#endif // DATEHELPER_H_