ESGRAF48/source/Age/Age.cpp

67 lines
1016 B
C++
Raw Permalink Normal View History

2018-06-09 11:13:00 +00:00
#include "Age.h"
#include <QDebug>
2018-06-14 17:52:19 +00:00
#include <sstream>
2018-06-09 11:13:00 +00:00
Age::Age(unsigned int years, unsigned int months)
2018-11-25 13:30:45 +00:00
: m_years(years)
, m_months(months)
2018-06-09 11:13:00 +00:00
{
}
Age::Age(const QDate &birth, const QDate &reference)
{
if (reference < birth)
{
qDebug() << "test (" << reference << ") before birth (" << birth << ")";
m_years = 0;
m_months = 0;
return;
}
int years = reference.year() - birth.year();
int months = reference.month() - birth.month();
if (months == 0 && reference.day() < birth.day())
{
months--;
}
if (months < 0)
{
years--;
months = (months + 12) % 12;
}
m_years = years;
m_months = months;
}
2018-11-25 13:30:45 +00:00
bool Age::operator<(const Age &cmp) const
{
if (m_years == cmp.m_years)
{
return m_months < cmp.m_months;
}
return m_years < cmp.m_years;
}
2018-11-25 13:30:45 +00:00
2018-06-09 11:13:00 +00:00
unsigned int Age::years() const
{
return m_years;
}
unsigned int Age::months() const
{
return m_months;
}
2018-11-25 13:30:45 +00:00
2018-06-14 17:52:19 +00:00
std::string Age::toString() const
{
std::ostringstream result;
result << m_years << ";" << m_months;
return result.str();
}