File: period.cpp

package info (click to toggle)
martchus-cpp-utilities 5.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,352 kB
  • sloc: cpp: 12,471; awk: 18; ansic: 12; makefile: 10
file content (81 lines) | stat: -rw-r--r-- 3,082 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "./period.h"

namespace CppUtilities {

/*!
 * \class Period
 * \brief Represents a period of time.
 *
 * In contrast to the TimeSpan class, a Period represents a duration between a concrete
 * starting DateTime and end DateTime. Without that context, a Period instance is useless.
 *
 * Note the absence of the TimeSpan::totalYears() and TimeSpan::totalMonth() methods.
 * The reason for this limitation of the TimeSpan class is that the TimeSpan is meant to express
 * a duration independently of the concrete starting DateTime and end DateTime.
 * However, the concrete calendar interval is neccassary for expressing a duration in terms of years
 * and months because not all years and months have the same length.
 *
 * The Period class, on the other hand, expresses the duration between a *concrete* starting DateTime
 * and end DateTime as the number of years, month and days which have been passed **in that particular
 * order**. The accuracy is one day, so the DateTime::timeOfDay() is lost.
 *
 * \remarks The order really matters. For example, the Period between DateTime::fromDateAndTime(1994, 7, 18)
 *          and DateTime::fromDateAndTime(2017, 12, 2) is 23 years, 4 month and 14 days. That means
 *          23 years have been passed, then 4 month and finally 14 days. Adding the 14 days first and then
 *          the 4 month would make a difference of one day because July has 31 days and November only 30.
 */

/*!
 * \brief Constructs a new Period defined by a start DateTime and an end DateTime.
 *
 * The resulting Period will contain the number of years, month and days which have been passed
 * between \a begin and \a end.
 */
Period::Period(DateTime begin, DateTime end)
{
    m_years = end.year() - begin.year();
    m_months = end.month() - begin.month();
    if (m_months < 0) {
        m_months += 12;
        --m_years;
    }
    m_days = end.day() - begin.day();
    if (m_days < 0) {
        m_days += end.month() > 1 ? DateTime::daysInMonth(end.year(), end.month() - 1) : 31;
        --m_months;
    }
    if (m_months < 0) {
        m_months += 12;
        --m_years;
    }
}

/*!
 * \brief Adds the specified \a period to the specified date.
 * \throws Might throw ConversionException if resulting DateTime would be out-of-range.
 * \remarks
 * - The order in which the years(), month() and days() are added matters. See the overall class description.
 * - Since the accuracy of Period is only one day, the DateTime::timeOfDay() of the result always equals begin.timeOfDay().
 */
DateTime operator+(DateTime begin, Period period)
{
    auto year = begin.year() + period.years();
    auto month = begin.month() + period.months();
    if (month > 12) {
        month -= 12;
        ++year;
    }
    auto day = begin.day() + period.days();
    const auto maxDays = DateTime::daysInMonth(year, month);
    if (day > maxDays) {
        day -= maxDays;
        ++month;
    }
    if (month > 12) {
        month -= 12;
        ++year;
    }
    return DateTime::fromDate(year, month, day) + begin.timeOfDay();
}

} // namespace CppUtilities