This repository has been archived on 2024-12-15. You can view files and clone it, but cannot push or open issues or pull requests.
ESGRAF48/source/Age/Age.cpp

67 lines
1.1 KiB
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
{
}
2019-10-05 14:14:45 +00:00
Age::Age(const QDate& birth, const QDate& reference)
2018-06-09 11:13:00 +00:00
{
2019-10-05 14:14:45 +00:00
if (reference < birth)
{
qDebug() << "test (" << reference << ") before birth (" << birth << ")";
m_years = 0;
m_months = 0;
2018-06-09 11:13:00 +00:00
2019-10-05 14:14:45 +00:00
return;
}
2018-06-09 11:13:00 +00:00
2019-10-05 14:14:45 +00:00
int years = reference.year() - birth.year();
int months = reference.month() - birth.month();
2018-06-09 11:13:00 +00:00
2019-10-05 14:14:45 +00:00
if (months == 0 && reference.day() < birth.day())
{
months--;
}
2018-06-09 11:13:00 +00:00
2019-10-05 14:14:45 +00:00
if (months < 0)
{
years--;
months = (months + 12) % 12;
}
2018-06-09 11:13:00 +00:00
2019-10-05 14:14:45 +00:00
m_years = years;
m_months = months;
2018-06-09 11:13:00 +00:00
}
2018-11-25 13:30:45 +00:00
2019-10-05 14:14:45 +00:00
bool Age::operator<(const Age& cmp) const
{
2019-10-05 14:14:45 +00:00
if (m_years == cmp.m_years)
{
return m_months < cmp.m_months;
}
2019-10-05 14:14:45 +00:00
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
{
2019-10-05 14:14:45 +00:00
return m_years;
2018-06-09 11:13:00 +00:00
}
unsigned int Age::months() const
{
2019-10-05 14:14:45 +00:00
return m_months;
2018-06-09 11:13:00 +00:00
}
2018-11-25 13:30:45 +00:00
2018-06-14 17:52:19 +00:00
std::string Age::toString() const
{
2019-10-05 14:14:45 +00:00
std::ostringstream result;
result << m_years << ";" << m_months;
return result.str();
2018-06-14 17:52:19 +00:00
}