(CODE) Calendar Date Class in C++

(CODE) Calendar Date Class in C++

As a means of illustrating what an actual large and complete C++ class looks like, we will present a class for managing calendar dates. Commentary on this class is given below the source.

First of all, the header:

        // Date class header file

        #ifndef __DATE_H__
        #define __DATE_H__

        typedef unsigned short Drep;            // internal storage format

        const int MIN_YEAR = 1875;
        const int MAX_YEAR = 2025;
        const Drep MAX_DAY = 55152;
        const int DOW_MIN = 6;

        class Date {
                Drep d;                                 // actual date
                static int init_flag;                   // init flag
                static int isleap(int);                 // leap year?
                static Drep cdays[MAX_YEAR-MIN_YEAR+1]; // cumul days per yr
                static void init_date();                // initialize date
                static Drep mdy_to_d(int, int, int);    // m/d/y --> day
                static void d_to_mdy(Drep,int&,int&,int&);// day --> m/d/y
        public:
                Date(Drep);                     // constructor from internal
                Date(const Date&);              // copy constructor
                Date(int, int, int);            // constructor from m/d/y
                Date(const char*);              // constructor from char*
                operator Drep();                // conversion to Drep
                void print(char* = (char*)0);   // print
                void get_mdy(int&, int&, int&); // get m/d/y
                long operator-(const Date&);    // difference of dates
                int dow();                      // day of week
                long wdays(const Date&);        // work days between dates
        };

        #endif

and then the source itself, along with a driver program:

        // Date class and driver program
        
        #include <string.h>
        #include <stdlib.h>
        #include <stdio.h>
        #include <ctype.h>
        #include <assert.h>
        #include "date.h"
        
        // days in the various months
        const char days_in_month[12] = {31, 28, 31, 30, 31, 30,
                                        31, 31, 30, 31, 30, 31};
        
        Drep Date::cdays[MAX_YEAR - MIN_YEAR + 1];
        int Date::init_flag = 0;
        
        // initialize date structures
        void Date::init_date()
        {
                int i;
                Drep cumul = 0;
        
                init_flag = 1;
                for (i = MIN_YEAR; i <= MAX_YEAR; i++) {
                        cumul += 365 + isleap(i);
                        cdays[i - MIN_YEAR] = cumul;
                }
        }
        
        // a leap year?
        int Date::isleap(int year)
        {
                if (year % 4)
                        return 0;
                if (year % 100)
                        return 1;
                if (year % 400)
                        return 0;
                return 1;
        }

[READ MORE ..]

COURTESY: www.glenmccl.com

Google