The topic for this chapter is Date and Time. Deceptively simple, handling dates and times in the context of leap years, across time zones and daylight savings and other such phenomena make this extremely complicated. In a projectlet or 2, we will explore some problems and thus exercise the predefined libraries of the language.
6.1 Date of Birth
6.1.1 Learning Objectives
Convert Date and Time between internal and Printable formats
Arithmetic with Date, Time and Duration
6.1.2 Projectlet Specification
Given a date of birth argument, compute and printout the age, details of the 50th, 60th and other significant birthdays. Leap years should be handled appropriately.
6.1.3 Sample Runs
~/bin/bday
Bday.P1
Date of Birth 1986-04-01 06:00:01
Day of the Week TUESDAY
Age (days) 14191
(years) 38
Birthday at Age 50 2036-04-01 04:00:00 TUESDAY
Birthday at Age 60 2046-04-01 05:00:00 SUNDAY
Birthday at Age 70 2056-04-01 05:00:00 SATURDAY
Birthday at Age 80 2066-04-01 04:00:00 THURSDAY
Birthday at Age 90 2076-04-01 04:00:00 WEDNESDAY
Birthday at Age 100 2086-04-01 04:00:00 MONDAY
0022 | procedure Show_Birthday (dob : Ada.Calendar.Time; age : Positive) is
0023 | reqbday : Ada.Calendar.Time;
0024 | begin
0025 | reqbday := Time_Of (Year (dob) + age, Month (dob), Day (dob));
0026 |
0027 | Put ("Birthday at Age ");
0028 | Put (Positive'Image (age));
0029 | Set_Col (24);
0030 | Put (Formatting.Image (reqbday));
0031 | Set_Col (44);
0032 | Put (Formatting.Day_Of_Week (reqbday)'Image);
0033 | New_Line;
0034 | end Show_Birthday;
6.2 Multiple Dates
6.2.1 Learning Objectives
Date Comparison
Passage of Time
6.2.2 Projectlet Specifications
Given 2 dates, determine which is older. Essentially date comparisons.
Assuming these are dates of birth, determine the year when the older age will be double the younger. Triple the younger.
6.2.3 Sample runs
~/bin/dates 1996-10-07
dates
dates.P1
Person A
Birthday 1986-04-01 06:00:01
Day of the Week TUESDAY
Age today 14191 days
38 years
Person B
Birthday 1996-10-07 06:00:01
Day of the Week MONDAY
Age today 10349 days
28 years
Person A is older
Age difference is 3842 days
or 10 years
Double the Age
On 2006-10-07 04:00:00
Person B
will be 10 years old
Person A
will be 20 years old
6.2.4 Implementation
6.2.4.1 Code fragments
Given 2 dates of birth determine who is older. Then project when the older person will be double the age of the younger person.