× Go Back Go Back to Programming Section Menu Go Back to Home Page
Class_DateTime.h
/**********************************************************************************************/
/*               File Name: Class_DateTime               Written by: Koral Eren               */
/**********************************************************************************************/

// This class follows C++11 Standard. Please include [-std=c+11] for g++

#ifndef DATETIME_H_INCLUDED
#define DATETIME_H_INCLUDED

#include <iostream>
#include <chrono>
#include <ctime>
#include <sstream>
#include <string>
#include <iomanip>


using namespace std;
using namespace std::chrono;

class DateTime
{
    private:
        unsigned long long millisecondsSince_1January1970;
        unsigned long secondsSince_1January1970;
        unsigned int minutesSince_1January1970;
        unsigned int hoursSince_1January1970;
        unsigned int daysSince_1January1970;
        unsigned int monthsSince_1January1970;
        unsigned int yearsSince_1January1970;

        unsigned int thursdayWeeksSince_1January1970;
        unsigned int dayOfWeek;

        unsigned int millisecond;
        unsigned int second;
        unsigned int minute;
        unsigned int hour;
        unsigned int day;
        unsigned int month;
        unsigned int year;


        bool isLeapYear(unsigned int Year)
        {
            if (Year % 4 == 0)
            {
                if ((Year % 100 == 0) && (Year % 400 == 0))
                    return false;
                else
                    return true;
            }

            return false;
        }

        bool isLeapYear()
        {
            if (year % 4 == 0)
            {
                if ((year % 100 == 0) && (year % 400 == 0))
                    return false;
                else
                    return true;
            }

            return false;
        }

        unsigned int monthDays(unsigned int Month, unsigned int Year)
        {
            if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
                return 30;
            else if (Month == 2)
                return 28;
            else if (Month == 2 && isLeapYear(Year))
                return 29;
            else
                return 31;
        }

        unsigned int monthDays()
        {
            if (month == 4 || month == 6 || month == 9 || month == 11)
                return 30;
            else if (month == 2)
                return 28;
            else if (month == 2 && isLeapYear())
                return 29;
            else
                return 31;
        }

        void calculateYearMonth()
        {
            monthsSince_1January1970 = 0;
            yearsSince_1January1970 = 0;

            unsigned int i = 1;

            while(i< daysSince_1January1970)
            {
                if(isLeapYear(yearsSince_1January1970))
                {
                    i+= 366;
                    yearsSince_1January1970 ++;
                }
                else if (daysSince_1January1970 - i < 365)
                {
                    break;
                }
                else
                {
                    i+= 365;
                    yearsSince_1January1970 ++;
                }


            }

            unsigned int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

            if(isLeapYear(yearsSince_1January1970 + 1970))
                days[2]++;

            i = daysSince_1January1970 - i;

            unsigned int j = 1;
            unsigned int counter = 1;

            while ((i / days[j]) != 0)
            {
                i-=days[j];
                j++;
                counter ++;
            }

            monthsSince_1January1970 = yearsSince_1January1970 * 12 + counter;
        }

        void calculateDay ()
        {
            unsigned int sum = 0;

            for (unsigned int i = 1970; i 12 || day > 31 || month < 1 || day < 1 || year < 1970 || year > 2038 || hour > 23 || hour < 0 ||
                 minute > 59 || minute < 0 || second > 59 || second < 0 || millisecond > 999 || millisecond < 0)
            {
                cout << "\nCannot constuct specified object, constructing default object" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -1;
            }
            else if (monthDays(month, year) == 30 && (day > 30))
            {
                cout << "\nCannot constuct specified object, constructing default object" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -2;
            }
            else if (monthDays(month, year) == 29 && (day > 29))
            {
                cout << "\nCannot constuct specified object, constructing default object" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -3;
            }
            else if (monthDays(month, year) == 28 && (day > 28))
            {
                cout << "\nCannot constuct specified object, constructing default object" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -4;
            }

            return 1;
        }

        int parseSetDate (unsigned int year = 1970, unsigned int month = 1, unsigned int day = 0, unsigned int hour = 0, unsigned int minute = 0, unsigned int second = 0, unsigned int millisecond = 0)
        {
            if ( month > 12 || day > 31 || month < 1 || day < 1 || year < 1970 || year > 2038 || hour > 23 || hour < 0 ||
                 minute > 59 || minute < 0 || second > 59 || second < 0 || millisecond > 999 || millisecond < 0)
            {
                cout << "\nCannot set specified parameters, setting default parameters" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -1;
            }
            else if (monthDays(month, year) == 30 && (day > 30))
            {
                cout << "\nCannot set specified parameters, setting default parameters" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -2;
            }
            else if (monthDays(month, year) == 29 && (day > 29))
            {
                cout << "\nCannot set specified parameters, setting default parameters" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -3;
            }
            else if (monthDays(month, year) == 28 && (day > 28))
            {
                cout << "\nCannot set specified parameters, setting default parameters" << endl;
                millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
                setTimeVariables();
                return -4;
            }

            return 1;
        }


    public:
        DateTime()
        {
            millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
            setTimeVariables();
        }

        DateTime(float timeZone)
        {
            millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
            millisecondsSince_1January1970 += (int)(timeZone * 3600000);
            setTimeVariables();
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute)
        {
            if(parseDate(year,month,day,hour,minute)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000;
                setTimeVariables();
            }
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, float timeZone)
        {
            if(parseDate(year,month,day,hour,minute)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second)
        {
           if(parseDate(year,month,day,hour,minute,second)>0)
           {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000;
                setTimeVariables();
           }
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, float timeZone)
        {
           if(parseDate(year,month,day,hour,minute,second)>0)
           {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
           }
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond)
        {
            if(parseDate(year,month,day,hour,minute,second,millisecond)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000 + millisecond;
                setTimeVariables();
            }
        }

        DateTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond, float timeZone)
        {
            if(parseDate(year,month,day,hour,minute,second,millisecond)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000 + millisecond;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        ~DateTime()
        {

        }

        DateTime(const DateTime& dateTimeObject)
        {
            this->millisecondsSince_1January1970 = dateTimeObject.millisecondsSince_1January1970;
            setTimeVariables();
        }

        friend ostream& operator << (ostream& os, const DateTime& dateTimeObject)
        {

            stringstream convertStream;

            if (dateTimeObject.dayOfWeek == 0)
                convertStream << "Thu ";
            else if (dateTimeObject.dayOfWeek == 1)
                convertStream << "Fri ";
            else if (dateTimeObject.dayOfWeek == 2)
                convertStream << "Sat ";
            else if (dateTimeObject.dayOfWeek == 3)
                convertStream << "Sun ";
            else if (dateTimeObject.dayOfWeek == 4)
                convertStream << "Mon ";
            else if (dateTimeObject.dayOfWeek == 5)
                convertStream << "Tue ";
            else
                convertStream << "Wed ";

            if (dateTimeObject.month == 1)
                convertStream << "Jan ";
            else if (dateTimeObject.month == 2)
                convertStream << "Feb ";
            else if (dateTimeObject.month == 3)
                convertStream << "Mar ";
            else if (dateTimeObject.month == 4)
                convertStream << "Apr ";
            else if (dateTimeObject.month == 5)
                convertStream << "May ";
            else if (dateTimeObject.month == 6)
                convertStream << "Jun ";
            else if (dateTimeObject.month == 7)
                convertStream << "Jul ";
            else if (dateTimeObject.month == 8)
                convertStream << "Aug ";
            else if (dateTimeObject.month == 9)
                convertStream << "Sep ";
            else if (dateTimeObject.month == 10)
                convertStream << "Oct ";
            else if (dateTimeObject.month == 11)
                convertStream << "Nov ";
            else
                convertStream << "Dec ";

                convertStream << dateTimeObject.day << " ";

                convertStream << setfill('0') << setw(2) << dateTimeObject.hour << ":" << setfill('0') << setw(2) << dateTimeObject.minute << ":" << setfill('0') << setw(2) << dateTimeObject.second;

                convertStream << ":" << setfill('0') << setw(3) << dateTimeObject.millisecond << " ";

                convertStream << dateTimeObject.year << endl;

                os << convertStream.str();

            return os;
        }

        bool operator == (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 == dateTimeObject.millisecondsSince_1January1970)
                return true;
            else
                return false;
        }

        bool operator != (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 == dateTimeObject.millisecondsSince_1January1970)
                return false;
            else
                return true;
        }

         bool operator > (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 > dateTimeObject.millisecondsSince_1January1970)
                return true;
            else
                return false;
        }

        bool operator < (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 < dateTimeObject.millisecondsSince_1January1970)
                return true;
            else
                return false;
        }

        bool operator <= (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 <= dateTimeObject.millisecondsSince_1January1970)
                return true;
            else
                return false;
        }

        bool operator >= (const DateTime& dateTimeObject)
        {
            if (this->millisecondsSince_1January1970 <= dateTimeObject.millisecondsSince_1January1970)
                return true;
            else
                return false;
        }

        DateTime& operator + (const DateTime& dateTimeObject)
        {
            millisecondsSince_1January1970 = millisecondsSince_1January1970 + dateTimeObject.millisecondsSince_1January1970;
            if(millisecondsSince_1January1970 < 0)
            {
                cout << "Error using operator [+]: Resulting date must be smaller than 1 January 2038" << endl;
                millisecondsSince_1January1970 = 0;
            }
            else
                setTimeVariables();

            return *this;
        }

        DateTime& operator - (const DateTime& dateTimeObject)
        {
            millisecondsSince_1January1970 = millisecondsSince_1January1970 - dateTimeObject.millisecondsSince_1January1970;
            if(millisecondsSince_1January1970 < 0)
            {
                cout << "Error using operator [-]: Resulting date must be greater than 1 January 1970" << endl;
                millisecondsSince_1January1970 = 0;
            }
            else
                setTimeVariables();

            return *this;
        }

        void set_Date()
        {
            millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
            setTimeVariables();
        }

        void set_Date(unsigned int year)
        {
            unsigned int month = 1;
            unsigned int day = 0;

            if(parseSetDate(year)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                setTimeVariables();
            }

        }

        void set_Date(unsigned int year, unsigned int month)
        {
            unsigned int day = 0;

            if(parseSetDate(year,month)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day)
        {
            if(parseSetDate(year,month,day)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour)
        {
            if(parseSetDate(year,month,day,hour)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000;
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute)
        {
            if(parseSetDate(year,month,day,hour,minute)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000;
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second)
        {
            if(parseSetDate(year,month,day,hour,minute,second)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000;
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond)
        {
            if(parseSetDate(year,month,day,hour,minute,second,millisecond)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000 + millisecond;
                setTimeVariables();
            }
        }

        void set_Date(float timeZone)
        {
            millisecondsSince_1January1970 = duration_cast(system_clock::now().time_since_epoch()).count();
            millisecondsSince_1January1970 += (int)(timeZone * 3600000);
            setTimeVariables();
        }

        void set_Date(unsigned int year, float timeZone)
        {
            unsigned int month = 1;
            unsigned int day = 0;

            if(parseSetDate(year)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, float timeZone)
        {
            unsigned int day = 0;

            if(parseSetDate(year,month)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, float timeZone)
        {
            if(parseSetDate(year,month,day)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, float timeZone)
        {
            if(parseSetDate(year,month,day,hour)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, float timeZone)
        {
            if(parseSetDate(year,month,day,hour,minute)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, float timeZone)
        {
            if(parseSetDate(year,month,day,hour,minute,second)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Date(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond, float timeZone)
        {
            if(parseSetDate(year,month,day,hour,minute,second,millisecond)>0)
            {
                millisecondsSince_1January1970 = (unsigned long long)calculateDay(year, month, day) * 86400000  + hour * 3600000 + minute * 60000 + second * 1000 + millisecond;
                millisecondsSince_1January1970 += (int)(timeZone * 3600000);
                setTimeVariables();
            }
        }

        void set_Time(unsigned int hour)
        {
            if(parseSetDate(hour)>0)
            {
                millisecondsSince_1January1970 += (hour * 3600000 - this->hour * 3600000);
                setTimeVariables();
            }
        }

        void set_Time(unsigned int hour, unsigned int minute)
        {
            if(parseSetDate(hour,minute)>0)
            {
                millisecondsSince_1January1970 += ((hour * 3600000 + minute * 60000) - (this->hour * 3600000 + this->minute * 60000));
                setTimeVariables();
            }
        }

        void set_Time(unsigned int hour, unsigned int minute, unsigned int second)
        {
            if(parseSetDate(hour,minute,second)>0)
            {
                millisecondsSince_1January1970 += ((hour * 3600000 + minute * 60000 + second * 1000) - (this->hour * 3600000 + this->minute * 60000 + this->second *1000));
                setTimeVariables();
            }
        }

        void set_Time(unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond)
        {
            if(parseSetDate(hour,minute,second,millisecond)>0)
            {
                millisecondsSince_1January1970 += ((hour * 3600000 + minute * 60000 + second * 1000 + millisecond) - (this->hour * 3600000 + this->minute * 60000 + this->second *1000 + this->millisecond));
                setTimeVariables();
            }
        }

        string get_date()
        {
            stringstream convertStream;

            if (dayOfWeek == 0)
                convertStream << "Thu ";
            else if (dayOfWeek == 1)
                convertStream << "Fri ";
            else if (dayOfWeek == 2)
                convertStream << "Sat ";
            else if (dayOfWeek == 3)
                convertStream << "Sun ";
            else if (dayOfWeek == 4)
                convertStream << "Mon ";
            else if (dayOfWeek == 5)
                convertStream << "Tue ";
            else
                convertStream << "Wed ";

            if (month == 1)
                convertStream << "Jan ";
            else if (month == 2)
                convertStream << "Feb ";
            else if (month == 3)
                convertStream << "Mar ";
            else if (month == 4)
                convertStream << "Apr ";
            else if (month == 5)
                convertStream << "May ";
            else if (month == 6)
                convertStream << "Jun ";
            else if (month == 7)
                convertStream << "Jul ";
            else if (month == 8)
                convertStream << "Aug ";
            else if (month == 9)
                convertStream << "Sep ";
            else if (month == 10)
                convertStream << "Oct ";
            else if (month == 11)
                convertStream << "Nov ";
            else
                convertStream << "Dec ";

                convertStream << day << " ";

                convertStream << setfill('0') << setw(2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second;

                convertStream << ":" << setfill('0') << setw(3) << millisecond << " ";

                convertStream << year << endl;

                return convertStream.str();
        }

        string get_time()
        {
            stringstream convertStream;

            convertStream << setfill('0') << setw(2) << hour << ":" << setfill('0') << setw(2) << minute << ":" << setfill('0') << setw(2) << second;
            convertStream << ":" << setfill('0') << setw(3) << millisecond;

            return convertStream.str();
        }

        string get_dayString()
        {
            if (dayOfWeek == 0)
                return "Thursday";
            else if (dayOfWeek == 1)
                return "Friday";
            else if (dayOfWeek == 2)
                return "Saturday";
            else if (dayOfWeek == 3)
                return "Sunday";
            else if (dayOfWeek == 4)
                return "Monday";
            else if (dayOfWeek == 5)
                return "Tuesday";
            else
                return "Wednesday";
        }

        unsigned int get_millisecond()
        {
            return millisecond;
        }

        unsigned int get_second()
        {
            return second;
        }
        unsigned int get_minute()
        {
            return minute;
        }
        unsigned int get_hour()
        {
            return hour;
        }
        unsigned int get_day()
        {
            return day;
        }
        unsigned int get_year()
        {
            return year;
        }
};

#endif // DATETIME_H_INCLUDED