Content


NTL
Core
CDateTime

CDateTime


This helper class is used to represent date and time. It is much more powerfull than standard runtime functions and allows to specify any date even before first and after 3000 year. Pay attention on how years, months, and days are numbered. They all start from 1. On the other hand days of week are numbered starting from 0. In order to prevent errors we recommend to use class constants like CDateTime::June and CDateTime::Friday to specify months and days of week. Here are some examples of using CDateTime and CInterval classes.


CDateTime now;                                           // Create object with current date and time
CDateTime d{ 2001, CDateTime::February, 20, 15, 0, 0 };  // Create object storing 2001-02-20 15:00:00 date-time
CInterval i{ now - d };                                  // Find difference between two date-times
d += CInterval{ 0, 0, 5, 0, 0 };                         // Add 5 days to the d date-time object
if (d.getDayOfWeek() == CDateTime::Wednesday)            // Check whether d object day of week is wednesday
    std::cout << d.getDaysInMonth();                     // Print count of days in month of d object

You can find more information in comments below.

class CDateTime
{
public:
    // Months
    static const int January{ 1 };
    static const int February{ 2 };
    static const int March{ 3 };
    static const int April{ 4 };
    static const int May{ 5 };
    static const int June{ 6 };
    static const int July{ 7 };
    static const int August{ 8 };
    static const int September{ 9 };
    static const int October{ 10 };
    static const int November{ 11 };
    static const int December{ 12 };
    // Days of week
    static const int Monday{ 0 };
    static const int Tuesday{ 1 };
    static const int Wednesday{ 2 };
    static const int Thursday{ 3 };
    static const int Friday{ 4 };
    static const int Saturday{ 5 };
    static const int Sunday{ 6 };

    CDateTime(); // Create with current date and time
    CDateTime(const int year, const int month, const int day, const int hour, const int minute, const int second); // Create with specified date and time
    CDateTime(const CDateTime &other) = default;
    CDateTime(CDateTime &&other) = default;
    CDateTime(const long long datetime); // Create from specified time point

    int getYear() const; // Return year
    int getMonth() const; // Return month. In range 1-12
    int getDay() const; // Return day. In range 1-31
    int getHour() const; // Return hour. In range 0-23
    int getMinute() const; // Return minute. In range 0-59
    int getSecond() const; // Return second. In range 0-60
    int getDayOfWeek(); // Return day of week. In range 0-6
    int getDaysInMonth() const; // Return count of days in current object
    bool isLeap() const; // Return whether current object year is leap or not

    bool setYear(const int value); // Set year
    bool setMonth(const int value); // Set month
    bool setDay(const int value); // Set day
    bool setHour(const int value); // Set hour
    bool setMinute(const int value); // Set minute
    bool setSecond(const int value); // Set second

    static int getDaysInMonth(const int year, const int month); // Return count of days in specified year and month
    static bool isLeap(const int year); // Return whether specified year is leap or not

    bool setDate(const int year, const int month, const int day); // Set date
    bool setTime(const int hour, const int minute, const int second); // Set time
    bool setDateTime(const int year, const int month, const int day, const int hour, const int minute, const int second); // Set date and time

    bool operator==(const CDateTime &other) const; // Compare if this object date and time is equal to another one
    bool operator!=(const CDateTime &other) const; // Compare if this object date and time is not equal to another one
    bool operator>(CDateTime &other); // Compare if this object date-time is greater than in another one
    bool operator>=(CDateTime &other); // Compare if this object date-time is greater or equal to another one
    bool operator<(CDateTime &other); // Compare if this object date-time is less than in another one
    bool operator<=(CDateTime &other); // Compare if this object date-time is less or equal to another one
    CDateTime operator+(const CInterval &value); // Return date-time object equal to the current object date-time increasaed by specified interval
    CDateTime &operator+=(const CInterval &value); // Add specified interval to the current object and return this object
    CInterval operator-(CDateTime &other); // Return difference between this and another date-time objects
    CDateTime operator-(const CInterval &value); // Return date-time object equal to the current object date-time decreasaed by specified interval
    CDateTime &operator-=(const CInterval &value); // Subtract specified interval from the current object and return this object
    CDateTime &operator=(const CDateTime &other) = default;
    CDateTime &operator=(CDateTime &&other) = default;

    explicit operator long long(); // Convert to time point(long long)
};

Following operators are also available.

std::wostream &operator<<(std::wostream &stream, const CDateTime &a); // Output as source code
Namespace: nitisa
Include: Nitisa/Core/DateTime.h