| 1 | #cxx . . $ `<file>/chrono/period.cpp`/include "./period.h"cxx . . $ `<file>/chrono/period.h`/ |
| 2 | |
| 3 | namespace CppUtilitiescxx . . $ 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 | Periodcxx . . $ CppUtilities/Period#::Periodcxx . . $ CppUtilities/Period#Period(46fa059ed87d5dae).(DateTimecxx . . $ CppUtilities/DateTime# begin, DateTimecxx . . $ CppUtilities/DateTime# end) |
| 34 | { |
| 35 | m_yearscxx . . $ CppUtilities/Period#m_years. = end.yearcxx . . $ CppUtilities/DateTime#year(455f465bc33b4cdf).() - begin.yearcxx . . $ CppUtilities/DateTime#year(455f465bc33b4cdf).(); |
| 36 | m_monthscxx . . $ CppUtilities/Period#m_months. = end.monthcxx . . $ CppUtilities/DateTime#month(455f465bc33b4cdf).() - begin.monthcxx . . $ CppUtilities/DateTime#month(455f465bc33b4cdf).(); |
| 37 | if (m_monthscxx . . $ CppUtilities/Period#m_months. < 0) { |
| 38 | m_monthscxx . . $ CppUtilities/Period#m_months. += 12; |
| 39 | --m_yearscxx . . $ CppUtilities/Period#m_years.; |
| 40 | } |
| 41 | m_dayscxx . . $ CppUtilities/Period#m_days. = end.daycxx . . $ CppUtilities/DateTime#day(455f465bc33b4cdf).() - begin.daycxx . . $ CppUtilities/DateTime#day(455f465bc33b4cdf).(); |
| 42 | if (m_dayscxx . . $ CppUtilities/Period#m_days. < 0) { |
| 43 | m_dayscxx . . $ CppUtilities/Period#m_days. += end.monthcxx . . $ CppUtilities/DateTime#month(455f465bc33b4cdf).() > 1 ? DateTimecxx . . $ CppUtilities/DateTime#::daysInMonthcxx . . $ CppUtilities/DateTime#daysInMonth(9b79fb6aee4c0440).(end.yearcxx . . $ CppUtilities/DateTime#year(455f465bc33b4cdf).(), end.monthcxx . . $ CppUtilities/DateTime#month(455f465bc33b4cdf).() - 1) : 31; |
| 44 | --m_monthscxx . . $ CppUtilities/Period#m_months.; |
| 45 | } |
| 46 | if (m_monthscxx . . $ CppUtilities/Period#m_months. < 0) { |
| 47 | m_monthscxx . . $ CppUtilities/Period#m_months. += 12; |
| 48 | --m_yearscxx . . $ CppUtilities/Period#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 | DateTimecxx . . $ CppUtilities/DateTime# operatorcxx . . $ CppUtilities/operator+(aea77c219e118c30).+(DateTimecxx . . $ CppUtilities/DateTime# begin, Periodcxx . . $ CppUtilities/Period# period) |
| 60 | { |
| 61 | auto year = begin.yearcxx . . $ CppUtilities/DateTime#year(455f465bc33b4cdf).() + period.yearscxx . . $ CppUtilities/Period#years(455f465bc33b4cdf).(); |
| 62 | auto month = begin.monthcxx . . $ CppUtilities/DateTime#month(455f465bc33b4cdf).() + period.monthscxx . . $ CppUtilities/Period#months(455f465bc33b4cdf).(); |
| 63 | if (month > 12) { |
| 64 | month -= 12; |
| 65 | ++year; |
| 66 | } |
| 67 | auto day = begin.daycxx . . $ CppUtilities/DateTime#day(455f465bc33b4cdf).() + period.dayscxx . . $ CppUtilities/Period#days(455f465bc33b4cdf).(); |
| 68 | const auto maxDays = DateTimecxx . . $ CppUtilities/DateTime#::daysInMonthcxx . . $ CppUtilities/DateTime#daysInMonth(9b79fb6aee4c0440).(year, month); |
| 69 | if (day > maxDays) { |
| 70 | day -= maxDays; |
| 71 | ++month; |
| 72 | } |
| 73 | if (month > 12) { |
| 74 | month -= 12; |
| 75 | ++year; |
| 76 | } |
| 77 | return DateTimecxx . . $ CppUtilities/DateTime#::fromDatecxx . . $ CppUtilities/DateTime#fromDate(1d87702ad89c5e62).(year, month, day) +cxx . . $ CppUtilities/DateTime#operator+(c80c8e835accff8). begin.timeOfDaycxx . . $ CppUtilities/DateTime#timeOfDay(af1383a2c4e36306).(); |
| 78 | } |
| 79 | |
| 80 | } // namespace CppUtilities |