Content


NTL
Core
CInterval

CInterval



This helper class is used to represent date and time intervals. Although it is correct to use any second/minute/hour/day/month count in interval it is strongly recommended to use higher order values. For example, instead of specifying interval as 86400 seconds it is recommended to specify it as 1 day instead.

Years are considered as non-leap ones. For example:

CDateTime d1{ 2003, CDateTime::Fabruary, 28, 0, 0, 0 };
d1 += CInterval{ 1, 0, 0, 0, 0, 0 };
std::cout << d1 << std::endl; // It will output "2004-02-28 00:00:00".

CDateTime d2{ 2004, CDateTime::Fabruary, 28, 0, 0, 0 };
d2 += CInterval{ 1, 0, 0, 0, 0, 0 };
std::cout << d2 << std::endl; // It will output "2005-02-27 00:00:00" because of the year 2004 has one extra day.

When using non zero month count in intervals they are considered as days in corresponding month of a non-leap year. For example:

CDateTime d1{ 2004, CDateTime::Fabruary, 28, 0, 0, 0 };
d1 += CInterval{ 0, 1, 0, 0, 0, 0 };
std::cout << d1 << std::endl; // It will output "2004-03-30 00:00:00" because of adding 31 day(month count in interval is 1).

So if you specify 2 months in interval it will mean 31 + 28 days, 3 months mean 31 + 28 + 31 days, and so on.

This class is usually used togather with CDateTime class.

You can find more information in comments below.

class CInterval
{
public:
    CInterval(); // Create empty interval
    CInterval(const int years, const int months, const int days, const int hours, const int minutes, const int seconds); // Create interval with specified parameters
    CInterval(const long long seconds); // Create interval with specified number of seconds
    CInterval(const CInterval &other) = default;
    CInterval(CInterval &&other) = default;

    int getYears() const; // Return years
    int getMonths() const; // Return months
    int getDays() const; // Return days
    int getHours() const; // Return hours
    int getMinutes() const; // Return minutes
    int getSeconds() const; // Return seconds

    bool setYears(const int value); // Set years
    bool setMonths(const int value); // Set months
    bool setDays(const int value); // Set days
    bool setHours(const int value); // Set hours
    bool setMinutes(const int value); // Set minutes
    bool setSeconds(const int value); // Set seconds

    CInterval &operator=(const CInterval &other) = default;
    CInterval &operator=(CInterval &&other) = default;

    explicit operator long long() const; // Convert to time interval in seconds
};
Namespace: nitisa
Include: Nitisa/Core/Interval.h