Standard library: Dates & Times
Holocene calendar
Goal: create a function that returns the year in the Holocene calendar.
Steps:
Implement the
To_Holocene_Yearfunction.
Requirements:
The
To_Holocene_Yearextracts the year from a time object (Timetype) and returns the corresponding year for the Holocene calendar.
For positive (AD) years, the Holocene year is calculated by adding 10,000 to the year number.
Remarks:
In this exercise, we don't deal with BC years.
Note that the year component of the
Timetype from theAda.Calendarpackage is limited to years starting with 1901.
with Ada.Calendar; use Ada.Calendar;
function To_Holocene_Year (T : Time) return Integer is
begin
return 0;
end To_Holocene_Year;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with To_Holocene_Year;
procedure Main is
type Test_Case_Index is
(Holocene_Chk);
procedure Display_Holocene_Year (Y : Year_Number) is
HY : Integer;
begin
HY := To_Holocene_Year (Time_Of (Y, 1, 1));
Put_Line ("Year (Gregorian): " & Year_Number'Image (Y));
Put_Line ("Year (Holocene): " & Integer'Image (HY));
end Display_Holocene_Year;
procedure Check (TC : Test_Case_Index) is
begin
case TC is
when Holocene_Chk =>
Display_Holocene_Year (2012);
Display_Holocene_Year (2020);
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;
List of events
Goal: create a system to manage a list of events.
Steps:
Implement the
Eventspackage.
Declare the
Event_Itemtype.Declare the
Event_Itemstype.Implement the
Events.Listspackage.
Declare the
Event_Listtype.Implement the
Addprocedure.Implement the
Displayprocedure.
Requirements:
The
Event_Itemtype (from theEventspackage) contains the description of an event.
This description shall be stored in an access-to-string type.
The
Event_Itemstype stores a list of events.
This will be used later to represent multiple events for a specific date.
You shall use a vector for this type.
The
Events.Listspackage contains the subprograms that are used in the test application.The
Event_Listtype (from theEvents.Listspackage) maps a list of events to a specific date.
You must use the
Event_Itemstype for the list of events.You shall use the
Timetype from theAda.Calendarpackage for the dates.Since we expect the events to be ordered by the date, you shall use ordered maps for the
Event_Listtype.Procedure
Addadds an event into the list of events for a specific date.Procedure
Displaymust display all events for each date (ordered by date) using the following format:<event_date #1> <description of item #1a> <description of item #1b> <event_date #2> <description of item #2a> <description of item #2b>
You should use the auxiliary
Date_Imagefunction — available in the body of theEvents.Listspackage — to display the date in theYYYY-MM-DDformat.
Remarks:
Let's briefly illustrate the expected output of this system.
Consider the following example:
with Ada.Calendar; with Ada.Calendar.Formatting; use Ada.Calendar.Formatting; with Events.Lists; use Events.Lists; procedure Test is EL : Event_List; begin EL.Add (Time_Of (2019, 4, 16), "Item #2"); EL.Add (Time_Of (2019, 4, 15), "Item #1"); EL.Add (Time_Of (2019, 4, 16), "Item #3"); EL.Display; end Test;The expected output of the
Testprocedure must be:EVENTS LIST - 2019-04-15 - Item #1 - 2019-04-16 - Item #2 - Item #3
package Events is
type Event_Item is null record;
type Event_Items is null record;
end Events;
with Ada.Calendar; use Ada.Calendar;
package Events.Lists is
type Event_List is tagged private;
procedure Add (Events : in out Event_List;
Event_Time : Time;
Event : String);
procedure Display (Events : Event_List);
private
type Event_List is tagged null record;
end Events.Lists;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
package body Events.Lists is
procedure Add (Events : in out Event_List;
Event_Time : Time;
Event : String) is
begin
null;
end Add;
function Date_Image (T : Time) return String is
Date_Img : constant String := Image (T);
begin
return Date_Img (1 .. 10);
end;
procedure Display (Events : Event_List) is
T : Time;
begin
Put_Line ("EVENTS LIST");
-- You should use Date_Image (T) here!
end Display;
end Events.Lists;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Events.Lists; use Events.Lists;
procedure Main is
type Test_Case_Index is
(Event_List_Chk);
procedure Check (TC : Test_Case_Index) is
EL : Event_List;
begin
case TC is
when Event_List_Chk =>
EL.Add (Time_Of (2018, 2, 16),
"Final check");
EL.Add (Time_Of (2018, 2, 16),
"Release");
EL.Add (Time_Of (2018, 12, 3),
"Brother's birthday");
EL.Add (Time_Of (2018, 1, 1),
"New Year's Day");
EL.Display;
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;