| 1 | #include "./period.h" |
| 2 | |
| 3 | namespace CppUtilities { |
| 4 | |
| 5 | /*! |
| 6 | * \class Period |
| 7 | * \brief Represents a period of time. |
| 8 | * |
| 9 | * In contrast to the TimeSpan class, a Period represents a duration expressed in years, months |
| 10 | * and days between a concrete starting DateTime and end DateTime. Without that context, the exact |
| 11 | * duration represented by a Period is not clearly defined as years and month have different lengths. |
| 12 | * |
| 13 | * This is also the reason for the absence of the TimeSpan::totalYears() and TimeSpan::totalMonth() |
| 14 | * methods as the TimeSpan class is meant to express a duration independently of the concrete starting |
| 15 | * DateTime and end DateTime. |
| 16 | * |
| 17 | * The Period class, on the other hand, expresses the duration between a *concrete* starting DateTime |
| 18 | * and end DateTime as the number of years, month and days which have been passed **in that particular |
| 19 | * order**. The accuracy is one day, so the DateTime::timeOfDay() is lost. |
| 20 | * |
| 21 | * \remarks The order really matters. For example, the Period between DateTime::fromDateAndTime(1994, 7, 18) |
| 22 | * and DateTime::fromDateAndTime(2017, 12, 2) is 23 years, 4 month and 14 days. That means |
| 23 | * 23 years have been passed, then 4 month and finally 14 days. Adding the 14 days first and then |
| 24 | * the 4 month would make a difference of one day because July has 31 days and November only 30. |
| 25 | */ |
| 26 | |
| 27 | /*! |
| 28 | * \brief Constructs a new Period defined by a start DateTime and an end DateTime. |
| 29 | * |
| 30 | * The resulting Period will contain the number of years, month and days which have been passed |
| 31 | * between \a begin and \a end. |
| 32 | */ |
| 33 | Period::Period(DateTime begin, DateTime end) |
| 34 | { |
| 35 | m_years = end.year() - begin.year(); |
| 36 | m_months = end.month() - begin.month(); |
| 37 | if (m_months < 0) { |
| 38 | m_months += 12; |
| 39 | --m_years; |
| 40 | } |
| 41 | m_days = end.day() - begin.day(); |
| 42 | if (m_days < 0) { |
| 43 | m_days += end.month() > 1 ? DateTime::daysInMonth(end.year(), end.month() - 1) : 31; |
| 44 | --m_months; |
| 45 | } |
| 46 | if (m_months < 0) { |
| 47 | m_months += 12; |
| 48 | --m_years; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /*! |
| 53 | * \brief Adds the specified \a period to the specified date. |
| 54 | * \throws Might throw ConversionException if resulting DateTime would be out-of-range. |
| 55 | * \remarks |
| 56 | * - The order in which the years(), month() and days() are added matters. See the overall class description. |
| 57 | * - Since the accuracy of Period is only one day, the DateTime::timeOfDay() of the result always equals begin.timeOfDay(). |
| 58 | */ |
| 59 | DateTime operator+(DateTime begin, Period period) |
| 60 | { |
| 61 | auto year = begin.year() + period.years(); |
| 62 | auto month = begin.month() + period.months(); |
| 63 | if (month > 12) { |
| 64 | month -= 12; |
| 65 | ++year; |
| 66 | } |
| 67 | auto day = begin.day() + period.days(); |
| 68 | const auto maxDays = DateTime::daysInMonth(year, month); |
| 69 | if (day > maxDays) { |
| 70 | day -= maxDays; |
| 71 | ++month; |
| 72 | } |
| 73 | if (month > 12) { |
| 74 | month -= 12; |
| 75 | ++year; |
| 76 | } |
| 77 | return DateTime::fromDate(year, month, day) + begin.timeOfDay(); |
| 78 | } |
| 79 | |
| 80 | } // namespace CppUtilities |