C++/Using and doing calculations on dates in c++
Expert: Zlatko - 7/16/2010
QuestionQUESTION: Sir,
I am a high school student and I am creating a Library Managment Program.
I need to use and store today's date (as date of issue of book)and add 15 days to the current date(as last date of return)..
Please help me in doing this..
Thank You!
ANSWER: Hello Avijeet
I'll help you with your program. Since you are asking this question in the C++ category, I'll assume that you are learning C++. For your date problem, I recommend that you create a Date class which you can use to store the date. I suppose that a library application would be using dates in terms of days, months, and years, instead of some other format, so it makes sense to store the Date data with day, month and year variables. The Date class below should get you started. You will probably need to add more features to it to suit your program so feel free to experiment. Ask about what you don't understand.
You can separate the class below into header and cpp files if you like, or you can put it into Date.h
class Date
{
public:
Date(int y, int m, int d)
{
mYear = y;
mMonth = m;
mDay = d;
// TODO verify that the values are valid.
// If they are not, perhaps throw an exception
}
// addDays adds a certain number of days to this Date
void addDays(int daysToAdd)
{
do
{
int daysLeft = daysLeftThisMonth();
if (daysLeft >= daysToAdd)
{
mDay += daysToAdd;
daysToAdd = 0;
}
else
{
mDay = 1;
daysToAdd -= daysLeft+1;
incrementMonth();
}
} while(daysToAdd);
}
// Some simple accessor functions
const char* monthName(void) const { return sMonthNames[mMonth]; }
int month(void) const { return mMonth; }
int day(void) const { return mDay; }
int year(void) const { return mYear; }
// daysLeftThisMonth returns the number of days left
// in the month specified by this Date
int daysLeftThisMonth(void) const
{
return daysThisMonth() - mDay;
}
// daysThisMonth returns the number of days in the month
// specified by this Date
int daysThisMonth(void) const
{
if (mMonth == 2 && isThisLeapYear()) return sDaysPerMonth[mMonth] + 1;
return sDaysPerMonth[mMonth];
}
private:
// incrementMonth adds one month to this Date and increments the
// year if needed
void incrementMonth(void)
{
mMonth += 1;
if (mMonth > 12)
{
mMonth = 1;
++mYear;
}
}
// isThisLeapYear returns true if the year of this Date is a leap year
// otherwise it returns false
bool isThisLeapYear(void) const
{
/*
leap years are divisible by 4, but if a year is divisible by 100, and not by 400,
then it is not a leap year.
*/
return mYear % 400 == 0 || mYear % 4 == 0 && mYear % 100 != 0;
}
private:
static int sDaysPerMonth[13];
static const char* sMonthNames[13];
int mYear;
int mMonth;
int mDay;
};
// Ja Fe Mr Ap My Jn Jl Au Se Oc No De
int Date::sDaysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char* Date::sMonthNames[13] = {
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
Below is a sample program that uses the Date
#include <iostream>
#include "Date.h"
std::ostream& operator<<(std::ostream& out, const Date& d)
{
out << d.monthName() << " " << d.day() << ", " << d.year();
return out;
}
using namespace std;
int main(void)
{
Date d(2004, 1, 1);
for(int day = 1; day <= 32; ++day)
{
cout << d << endl;
d.addDays(15);
}
return 0;
}
---------- FOLLOW-UP ----------
QUESTION: Thanks,it was very helpful...
How can a program take the system date automatically???
I also want to check whethar "last date of return has passed todays's date or not...
How dates can be compared??
Thanks in advance...
AnswerHello Avijeet
You can get the local date with another constructor like this:
#include <time.h>
class Date
{
public:
Date()
{
time_t now = time(NULL);
struct tm* lt = localtime(&now);
mYear = lt->tm_year + 1900;
mMonth = lt->tm_mon;
mDay = lt->tm_mday;
}
......
You will need to #include <time.h>
To compare Date objects, you could add comparison operators to your Date class like this:
bool operator==(const Date& rhs) const
{
// TODO put your logic here
}
bool operator >(const Date& rhs) const
{
// rhs stands for "right hand side"
// TODO put your logic here
}
So if you have 2 dates, d1 and d2, you can say
if (d1 > d2) ...
I leave it up to you to do the logic of the comparison. The comparison functions must return true or false. You can also implement other comparison operators like != and < in the same way.
If you as more questions, please let me know which operating system and compiler you are using.
Best regards
Zlatko