A Date object

Started by
1 comment, last by pizza box 19 years, 10 months ago
Hello, I''ve been working on a problem that requires me to utilize dates and compare them. I found a date type from MFC, but it wasn''t what I was looking for. I created my own object with a few functions to set the date, print it, and compare it with another object. Here is the code:

// DateTest.cpp

// Created June 5, 2004 - Adam Sheehan


// Holds an object that can hold dates as well as print and compare them


#include <iostream>
using namespace std;

const int monthArray[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};

struct Date	{

	int day;		// Ranges from 1-365

	int printDay;	// Ranges from 1-30(1)

	int month;
	int year;

	// Recieves the date as m/d/yyyy and enters the data in the struct

	int SetDate( int mnth, int dy, int yr );	
	// Prints the date as m/d/yyyy

	void PrintDate();
	// Returns the difference in days from the date given

	int Difference( Date* date1 );
};

// Recieves the date as m/d/yyyy and enters the data in the struct

int Date::SetDate( int mnth, int dy, int yr )	{
	
	// Check for appropriate range of month

	if ( ( mnth < 1 ) || ( mnth > 12 ) )	{
		cout << "Invalid entry for month (1-12)." << endl;
		return 0;	}		// Return error


	month = mnth;

	// Day is stored independantly of month

	day = monthArray[mnth-1] + dy;

	// Check for appropriate range of day( day either extends into next month or negative value )

	if ( day > monthArray[mnth] || dy < 1 )	{
		cout << "Invalid entry for day." << endl;
		return 0;		}	// Return error


	printDay = dy;

	// Check to make sure year is non-negative

	if ( yr < 0 )	{
		cout << "Invalid entry for year (0+)." << endl;
		return 0;		}	// Return error


	year = yr;

	return 1;	// Return success

}

// Prints the date as m/d/yyyy

void Date::PrintDate()	{

	// Print the date

	cout << month << "/" << printDay << "/" << year;

}

// Returns the difference in days from the date given

int Date::Difference( Date* date1 )	{

	int diffYears, diffDays;

	// Check for difference in years

	if ( date1->year != year )	{
	
		// Check if date1 is later

		if ( date1->year > year )	{
			// Get difference in years and convert to days

			diffYears = date1->year - year;
			diffDays  = diffYears * 365;

			// Check which day comes first (if equal doesn''t really matter)

			if ( date1->day >= day )	{
				diffDays += (date1->day - day);				}
			else	{
				// The later date comes in an earlier month in a later year

				diffDays -= date1->day;
				diffDays += day;
			}
		}
		else	{
			// date1 comes before

			diffYears = year - date1->year;
			diffDays = diffYears *365;

			// Check which day comes first 

			if ( day >= date1->day )	{
				diffDays += (day - date1->day);
			}
			else	{
				// The later date comes in an earlier month in a later year

				diffDays -= day;
				diffDays += date1->day;
			}
		}
	}
	else	{
	// The years are equal, subtract days and take absolute value

	diffDays = date1->day - day;
	if ( diffDays < 0 )	{
		diffDays = -diffDays;	}
	}


	// Return the difference in days

	return (diffDays);
}
My main concern is with the Difference() function. It feels like I am making too many tests for something that seems very basic. Am I missing a shortcut to this problem or is this a decent method? There wasn''t much info on dates from the compiler''s help so I''m not sure if it has been done before. Any input would be appreciated! Thanks. PS Another problem is that the Difference function doesn''t handle leap years.
Advertisement
I suppose you could get rid of most of that if you convert both dates to compare into tm structures and pass them to mktime. Then you can just subtract and take the absolute value. That gives you the number of seconds of difference. Then just divide that by the number of seconds in a day. That should also handle leap years.

Loop up the CRT time functions, they can help you alot.
There is so much you could do to improve that but there isn''t much point, your better off using boost Date-Time libaray.

have look here: http://www.boost.org/libs/date_time/doc/index.html

This topic is closed to new replies.

Advertisement