home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 24 - Programming exercise 1
- with Text_IO; use Text_IO;
- with Calendar; use Calendar;
-
- procedure CH24_1 is
-
- package Int_IO is new Text_IO.Integer_IO(INTEGER);
- use Int_IO;
- package Fix_IO is new Text_IO.Fixed_IO(DAY_DURATION);
- use Fix_IO;
-
- Year,Month,Day : INTEGER;
- Start,Seconds : DAY_DURATION;
- Time_And_Date : TIME;
-
- -- This procedure outputs the time in an Hour:Minute:Second format
- -- using a FLOAT type for splitting the time into the various
- -- fields. It works fine for simple time logging but lacks the
- -- accuracy required for finer time. If finer time is required,
- -- it will be necessary to keep the time in the fixed point format,
- -- and use lots of type conversions to get the three fields, and
- -- the fractional second field if needed. An alternative method
- -- would be to use a floating point type with more significant
- -- digits to maintain the accuracy, but it would require more time
- -- to execute. This is definitely an application for the fixed
- -- point data type.
- procedure Output_Time(Time_In_Seconds : DAY_DURATION) is
- Time : FLOAT;
- Hours, Minutes, Seconds : INTEGER;
- begin
- Time := FLOAT(Time_In_Seconds);
- Hours := INTEGER((Time - 30.0 * 60.0) / (60.0 * 60.0));
- Time := Time - FLOAT(Hours) * 60.0 * 60.0;
- Minutes := INTEGER((Time - 30.0) / 60.0);
- Seconds := INTEGER(Time - 0.5) mod 60;
- Put(Hours,3);
- Put(":");
- Put(Minutes,2);
- Put(":");
- Put(Seconds,2);
- end Output_Time;
-
- begin
-
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Output_Time(Seconds);
- Put_Line(" Begin 3.14 second delay");
-
- delay 3.14;
-
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Output_Time(Seconds);
- Put_Line(" End of 3.14 second delay");
-
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Start); -- get starting time
-
- for Index in 1..9 loop
- Put("The date and time are now");
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Put(Month,3);
- delay 0.2;
- Put(Day,3);
- delay 0.1;
- Put(Year,5);
- delay 0.1;
- Put(Seconds - Start,8,3,0);
- New_Line;
- delay 0.6;
- end loop;
-
- Put_Line("Begin non-accumulative timing loop here.");
-
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Start); -- get starting time
- for Index in 1..9 loop
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Put("The elapsed time is");
- Put(Seconds - Start,8,3,0);
- New_Line;
- delay Day_Duration(Index) - (Seconds - Start);
- end loop;
-
- Put(" The current time is ");
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Output_Time(Seconds);
- end CH24_1;
-
- -- Result of Execution
-
- -- 9:54:13 Begin 3.14 second delay
- -- 9:54:16 End of 3.14 second delay
- -- The date and time are now 7 22 1988 0.000
- -- The date and time are now 7 22 1988 1.090
- -- The date and time are now 7 22 1988 2.140
- -- The date and time are now 7 22 1988 3.180
- -- The date and time are now 7 22 1988 4.230
- -- The date and time are now 7 22 1988 5.270
- -- The date and time are now 7 22 1988 6.320
- -- The date and time are now 7 22 1988 7.360
- -- The date and time are now 7 22 1988 8.400
- -- Begin non-accumulative timing loop here
- -- The elapsed time is 0.000
- -- The elapsed time is 1.100
- -- The elapsed time is 2.030
- -- The elapsed time is 3.020
- -- The elapsed time is 4.040
- -- The elapsed time is 5.030
- -- The elapsed time is 6.020
- -- The elapsed time is 7.010
- -- The elapsed time is 8.000
- -- The current time is 9:54:35
-