Standard library: Dates & Times

Holocene calendar

Goal: create a function that returns the year in the Holocene calendar.

Steps:

  1. Implement the To_Holocene_Year function.

Requirements:

  1. The To_Holocene_Year extracts the year from a time object (Time type) and returns the corresponding year for the Holocene calendar.

    1. For positive (AD) years, the Holocene year is calculated by adding 10,000 to the year number.

Remarks:

  1. In this exercise, we don't deal with BC years.

  2. Note that the year component of the Time type from the Ada.Calendar package 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:

  1. Implement the Events package.

    1. Declare the Event_Item type.

    2. Declare the Event_Items type.

  2. Implement the Events.Lists package.

    1. Declare the Event_List type.

    2. Implement the Add procedure.

    3. Implement the Display procedure.

Requirements:

  1. The Event_Item type (from the Events package) contains the description of an event.

    1. This description shall be stored in an access-to-string type.

  2. The Event_Items type stores a list of events.

    1. This will be used later to represent multiple events for a specific date.

    2. You shall use a vector for this type.

  3. The Events.Lists package contains the subprograms that are used in the test application.

  4. The Event_List type (from the Events.Lists package) maps a list of events to a specific date.

    1. You must use the Event_Items type for the list of events.

    2. You shall use the Time type from the Ada.Calendar package for the dates.

    3. Since we expect the events to be ordered by the date, you shall use ordered maps for the Event_List type.

  5. Procedure Add adds an event into the list of events for a specific date.

  6. Procedure Display must 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>
    
    1. You should use the auxiliary Date_Image function — available in the body of the Events.Lists package — to display the date in the YYYY-MM-DD format.

Remarks:

  1. Let's briefly illustrate the expected output of this system.

    1. 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;
      
    2. The expected output of the Test procedure 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;