Previous Table of Contents Next


10.4.1.6. Displaying the Current Date

The next example, Show_Date, displays the current date in the form 05 AUG 1997.

    1 with Ada.Text_IO;
    2 with Ada.Calendar;
    3 procedure Show_Date is
    4
    5   Now  : Ada.Calendar.Time;
    6
    7   type Months is
    8     (Jan, Feb, Mar, Apr, May, Jun,
    9      Jul, Aug, Sep, Oct, Nov, Dec);
   10
   11 begin
   12
   13   Now := Ada.Calendar.Clock;
   14
   15   Ada.Text_IO.Put_Line(“Today is”
   16     & Integer’Image(Ada.Calendar.Day(Now)) & ‘ ‘
   17     & Months’Image
   18         (Months’Val( Ada.Calendar.Month(Now) - 1))
   19     & Integer’Image(Ada.Calendar.Year (Now)));
   20
   21 end Show_Date;

The context clause in line 2 mentions Ada.Calendar; line 7 declares a time variable Now, which, in line 13, is set to the current time by a call to Ada.Calendar.Clock. Line 13 is an assignment statement; the symbol := means assignment; the statement

   Now := Ada.Calendar.Clock;

assigns to the variable Now the value returned by the Clock call.

To display the date, I must extract the month, day, and year from the time value. I extract the day in line 16 with the function call Ada.Calendar.Day(Now) and extract the month and year in lines 18 and 19 with similar calls.

I choose to display the date (lines 15-19) by constructing a string from substrings concatenated together using the & operator. The expression (line 16)

   Integer’Image(Ada.Calendar.Day(Now))

extracts the day as an integer value and then produces its image (Integer’Image) as a numeric string. The form Integer’Image is known as an attribute; each type in Ada has a set of retrievable attributes.

To display the month as a 3-letter abbreviation instead of a number 1 .. 12, I introduce an enumeration type in lines 5-7. An enumeration type is declared by listing—enumerating—its values. This creates an ordered set of values, with no value appearing more than once. The values are, syntactically, either characters or (as in this case) identifiers.

Two especially useful attributes of enumeration types are ‘Pos and ‘Val. Months’Pos(Jan) returns a nonnegative integer representing the position of Jan in the type, or 0. Months’Val(0) goes the other way, returning Jan; Months’Pos(May) returns 4; Months’Val(8) returns Sep. Because Ada.Calendar.Month returns an integer in the range 1 .. 12, the expression

   Months’Val(Ada.Calendar.Month(Now) - 1)

returns just the right enumeration value, and so

   Months’Image(Months’Val(Ada.Calendar.Month(Now) - 1))

produces the desired output substring.

10.4.1.7. Displaying the Date and Time

The program Show_Date_and_Time extends the previous example to display both the time and the date.

    1 with Ada.Text_IO, Ada.Integer_Text_IO;
    2 with Ada.Calendar;
    3 procedure Show_Date_and_Time is
    4
    5   type Months is
    6     (Jan, Feb, Mar, Apr, May, Jun,
    7      Jul, Aug, Sep, Oct, Nov, Dec);
    8
    9   Now          : Ada.Calendar.Time := Ada.Calendar.Clock;
   10   SecsPast_0h00: Natural
   11                    := Natural(Ada.Calendar.Seconds(Now));
   12   MinsPast_0h00: Natural := SecsPast_0h00 / 60;
   13   Secs         : Natural := SecsPast_0h00 rem 60;
   14   Mins         : Natural := MinsPast_0h00 rem 60;
   15   Hrs          : Natural := MinsPast_0h00 / 60;
   16
   17 begin
   18
   19   Ada.Text_IO.Put(“The date and time is”
   20     & Integer’Image(Ada.Calendar.Day(Now)) & ‘ ‘
   21     & Months’Image
   22         (Months’Val(Ada.Calendar.Month(Now) - 1))
   23     & Integer’Image(Ada.Calendar.Year (Now)) & ‘ ‘);
   24
   25   if Hrs  < 10 then
   26     Ada.Text_IO.Put (Item => ‘0’);
   27   end if;
   28   Ada.Integer_Text_IO.Put (Item => Hrs, Width => 1);
   29   Ada.Text_IO.Put (Item => ‘:’);
   30
   31   if Mins < 10 then
   32     Ada.Text_IO.Put (Item => ‘0’);
   33   end if;
   34   Ada.Integer_Text_IO.Put (Item => Mins, Width => 1);
   35   Ada.Text_IO.Put (Item => ‘:’);
   36
   37   if Secs < 10 then
   38     Ada.Text_IO.Put (Item => ‘0’);
   39   end if;
   40   Ada.Integer_Text_IO.Put (Item => Secs, Width => 1);
   41
   42   Ada.Text_IO.New_Line;
   43
   44 end Show_Date_and_Time;

Using the ‘Image attribute to display a number is convenient, but it gives us little control over the format of the output value. Therefore, the context clause in line 1 mentions another standard package, Ada.Integer_Text_IO, which provides extended facilities for reading and writing integer values.

In line 9 I declare Now as before and then add five more variables. These are all given as type Natural, a subtype of Integer, declared in the standard as

   subtype Natural is Integer range 0..Integer’Last;

where Integer’Last returns the largest positive integer supported by the type.

This is a good time to mention that the Ada standard does not specify the range of Integer but requires that the range -2**15+1 .. +2**15-1 (16 bits) be included. In current practice on popular computers, it is usually safe to assume that Integer is represented using 32 bits. For absolute certainty, Ada allows you to declare your own numeric types and specify the required width in bits; this is an example of the way Ada provides simple default structures but allows the programmer much flexibility in overriding the defaults where necessary.


Previous Table of Contents Next