Learning objectives
Date calculations with the class date |
In many programs date calculations must be performed. Typical problem definitions are the following:
Which day of the week falls on a particular date?
How many days (months, years) are between two particular dates?
A date is given. Which date is exactly n days (months, years) before or after this date?
Which day in the year is a particular date?
Is a particular year a leap year?
To be able to handle dates comfortably and to solve such
problems, LEDA offers the class date.
An object of the type date
represents a date, consisting of a day d, a
month m and a year y. All
dates adhering to the Gregorian calendar between January 1st, 1
A.D., and December 31st, 9999 A.D., are permitted.
As an example of the class date we calculate how many days
old we are. For this we read day, month and year of our
birthday, construct a date from these
data, get ourselves a second date that
represents the date “today” and calculate the
difference (measured in days) of the two dates.
#include <LEDA/date.h>
#include <iostream>
using leda::date;
using std::cin;
using std::cout;
int main()
{
cout << "Please enter you birth date:\n";
int b_day, b_month,b_year;
cout << "Day: "; cin >> b_day;
cout << "Month: "; cin >> b_month;
cout << "Year: "; cin >> b_year;
date birthdate(b_day, date::month(b_month), b_year);
date today;
today.set_to_current_date();
int age_in_days = today - birthdate;
cout << "You are " << age_in_days << " days old.\n";
}
For the top secret data of the author the program spits out the following:
Please enter your birth date: Day:11Month:1Year:1967You are 13252 days old.
Hmmm, some girls call me old...
A date can be constructed from a day, a month and a
year. Day and year are of type int. The month is
of the enumeration type date::month, which
comprises the integer numbers from 1 to 12; these can be
specified in the form date::Jan,
date::Feb etc.
The method
D.set_to_current_date();
sets a date D to
the present day (present according to the system time). The
default constructor also sets a date to the date of the
present day.
If two dates are subtracted by the minus operator, the difference in days is returned.
Conversely, starting out from a given date, one can
“jump” to a future or former date by addition or
subtraction of an integer number, respectively. We use this
possibility in the following problem definition: Statistically
speaking, the 13th day of every month falls on a Friday more
frequently than on any other day of the week. We want to check
this statement with a program that iterates over all permitted
dates of the class date, counting for
every 13th day of a month the day of the week:
#include <LEDA/date.h>
#include <LEDA/array.h>
#include <iostream>
using leda::date;
using leda::array;
int main()
{
array<int> DOW_counter(1,7); // count Day Of Week
DOW_counter.init(0);
for (date D(1, date::Jan,1); ; D++)
{
if(D.get_day() == 13)
DOW_counter[D.get_day_of_week()]++;
// don't apply ++ to the last valid date:
if(D == date(31, date::Dec, 9999)) break;
}
for(int i = 1; i <= 7; i++)
std::cout << i << ": " << DOW_counter[i] << "\n";
}
The output of the program is
1: 17123 2: 17124 3: 17173 4: 17097 5: 17199 6: 17099 7: 17173
This shows that the 13th day of every month falls on the 5th day of the week, that is on Friday, more frequently than on any other day of the week. However, we should not infer from that that there are more unhappy days in life as happy ones, at least as long as we are young!
The operator ++ makes a date
jump to the next day. The operator
-- works correspondingly. The
operators + and
- allow to jump an arbitrary number
of days from a given date into the future and back to the
past, respectively.
The method
D.get_day();
returns the
day in the month belonging to a date D, that is an integer
number between 1 and 31.
The method
D.get_day_of_week();
returns the day in the week belonging to a date D, that is an integer
number between 1 and 7.
The class date offers some
further useful possibilities. For example, dates can be output
or read with the help of a format string in almost every
conceivable format. More information about this is found on
the corresponding manual
page.