C++ Utilities 5.27.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
Loading...
Searching...
No Matches
period.cpp
Go to the documentation of this file.
1#include "./period.h"
2
3namespace CppUtilities {
4
35{
36 m_years = end.year() - begin.year();
37 m_months = end.month() - begin.month();
38 if (m_months < 0) {
39 m_months += 12;
40 --m_years;
41 }
42 m_days = end.day() - begin.day();
43 if (m_days < 0) {
44 m_days += end.month() > 1 ? DateTime::daysInMonth(end.year(), end.month() - 1) : 31;
45 --m_months;
46 }
47 if (m_months < 0) {
48 m_months += 12;
49 --m_years;
50 }
51}
52
61{
62 auto year = begin.year() + period.years();
63 auto month = begin.month() + period.months();
64 if (month > 12) {
65 month -= 12;
66 ++year;
67 }
68 auto day = begin.day() + period.days();
69 const auto maxDays = DateTime::daysInMonth(year, month);
70 if (day > maxDays) {
71 day -= maxDays;
72 ++month;
73 }
74 if (month > 12) {
75 month -= 12;
76 ++year;
77 }
78 return DateTime::fromDate(year, month, day) + begin.timeOfDay();
79}
80
81} // namespace CppUtilities
Represents an instant in time, typically expressed as a date and time of day.
Definition datetime.h:55
int day() const
Returns the day component of the date represented by this instance.
Definition datetime.h:334
int month() const
Returns the month component of the date represented by this instance.
Definition datetime.h:326
static DateTime fromDate(int year=1, int month=1, int day=1)
Constructs a DateTime to the specified year, month, and day.
Definition datetime.h:211
constexpr TimeSpan timeOfDay() const
Returns the time of day as TimeSpan for this instance.
Definition datetime.h:418
static int daysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Definition datetime.h:450
int year() const
Returns the year component of the date represented by this instance.
Definition datetime.h:318
Represents a period of time.
Definition period.h:8
constexpr int days() const
Returns the days component of the period represented by the current instance.
Definition period.h:48
constexpr int years() const
Returns the years component of the period represented by the current instance.
Definition period.h:32
constexpr int months() const
Returns the months component of the period represented by the current instance.
Definition period.h:40
constexpr Period()
Definition period.h:22
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT DateTime operator+(DateTime begin, Period period)
Adds the specified period to the specified date.
Definition period.cpp:60