1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
with Ada.Integer_Text_Io;
with Ada.Text_Io;
with Ada.Calendar;
use Ada.Calendar;
with Ada.Calendar.Time_Zones;
use Ada.Calendar.Time_Zones;
with Ada.Calendar.Formatting;
use Ada.Calendar;
with Ada.Calendar.Arithmetic;
use Ada.Calendar.Arithmetic;
use Ada;
procedure Elapsed_Years is
procedure Print_Elapsed(Top_Date : in Time;
Bot_Date : in Time;
Years : in Natural;
Months : in Natural;
Days : in Natural;
Houres : in Natural;
Minutes : in Natural;
Second : in Natural;
Rest : in Natural) is
begin
Text_Io.Put("Pour l'interval entre le " &
Formatting.Image(Top_Date, False, UTC_Time_Offset(Top_Date)) & " et le " &
Formatting.Image(Bot_Date, False, UTC_Time_Offset(Bot_Date)) & " le temps à écoulé ");
Text_Io.Put(Natural'Image(Years)(2..Natural'Image(Years)'Last));
Text_Io.Put("y, ");
Text_Io.Put(Natural'Image(Months)(2..Natural'Image(Months)'last));
Text_Io.Put("m, ");
Text_Io.Put(Natural'Image(Days)(2..Natural'Image(Days)'last));
Text_Io.Put("d");
end Print_Elapsed;
function Is_Leap_Year (Year : Integer) return Boolean is
begin
return (Year rem 4 = 0) and ((Year rem 100 /= 0) or (Year rem 16 = 0));
end Is_Leap_Year;
pragma Inline (Is_Leap_Year);
Days_Months_Count : constant array (Month_Number) of Day_Number := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
procedure Difference_In_Years(Top_Date : in Time;
Bot_Date : in Time;
Years : out Natural;
Months : out Natural;
Days : out Natural;
Houres : out Natural;
Minutes : out Natural;
Second : out Natural;
Rest : out Natural) is
function Last_Days(Years , Months, Days : in Natural) return Natural is
begin
if Months = 1 then
return Days_Months_Count(12);
end if;
if Months /= 3 then
return Days_Months_Count(Months-1);
end if;
if Is_Leap_Year(Years) then
return Days_Months_Count(2) + 1;
else
return Days_Months_Count(2);
end if;
end Last_Days;
Top_Days_Total : Day_Count := 0;
Top_Seconds_Total : Duration := 0.0;
Top_Leap_Seconds : Leap_Seconds_Count := 0;
Bot_Days_Total : Day_Count := 0;
Bot_Seconds_Total : Duration := 0.0;
Bot_Leap_Seconds : Leap_Seconds_Count := 0;
Top_Seconds : Day_Duration := Seconds(Top_Date);
Bot_Seconds : Day_Duration := Seconds(Bot_Date);
Top_Day : Day_Number := Day(Top_Date);
Bot_Day : Day_Number := Day(Bot_Date);
Top_Year : Year_Number := Year(Top_Date);
Bot_Year : Year_Number := Year(Bot_Date);
Top_Month : Month_Number := Month(Top_Date);
Bot_Month : Month_Number := Month(Bot_Date);
Years_Number : Integer := 0;
Months_Number : Integer := 0;
Days_Number : Integer := 0;
begin
if Top_Date > Bot_Date then
raise Constraint_Error;
end if;
Years := 0;
Months := 0;
Days := 0;
Houres := 0;
Minutes := 0;
Second := 0;
Rest := 0;
Years_Number := (Bot_Year - Top_Year - 1);
if (Bot_Month > Top_Month) or ((Bot_Month = Top_Month) and (Bot_Day >= Top_Day)) then
Years := Years_Number + 1;
else
Months_Number := Bot_Month + 12;
Years := Years_Number;
end if;
Months_Number := Months_Number - Top_Month - 1;
if Bot_Day >= Top_Day then
Months := Months_Number + 1;
Days := Bot_Day - Top_Day;
else
Days_Number := Last_Days (Bot_Year, Months_Number, Bot_Day) - Top_Day;
if Days_Number < 0 then
Days := Days_Number + Bot_Day;
end if;
Months := Months_Number;
end if;
end Difference_In_Years;
Top_Date : Time := Time_Of(2009, 03, 28, 18000.0);
Bot_Date : Time := Time_Of(2011, 05, 30, 18000.0);
Years : Natural := 0;
Months : Natural := 0;
Days : Natural := 0;
Houres : Natural := 0;
Minutes : Natural := 0;
Second : Natural := 0;
Rest : Natural := 0;
type Elapsed_Type is
record
Top_Date : Time;
Bot_Date : Time;
end record;
Test_Set : array (1..14) of Elapsed_Type:=
((Time_Of(2009, 05, 15, 0.0), Time_Of(2009, 05, 15, 0.0)),
(Time_Of(2009, 04, 15, 0.0), Time_Of(2011, 05, 1, 0.0)),
(Time_Of(2009, 05, 15, 0.0), Time_Of(2011, 04, 1, 0.0)),
(Time_Of(2009, 04, 1, 0.0), Time_Of(2011, 05, 15, 0.0)),
(Time_Of(2012, 02, 1, 0.0), Time_Of(2015, 03, 1, 0.0)),
(Time_Of(2012, 02, 1, 0.0), Time_Of(2015, 01, 31, 0.0)),
(Time_Of(2012, 02, 29, 0.0), Time_Of(2015, 01, 31, 0.0)),
(Time_Of(2012, 03, 1, 0.0), Time_Of(2015, 02, 28, 0.0)),
(Time_Of(2012, 03, 1, 0.0), Time_Of(2016, 02, 29, 0.0)),
(Time_Of(2012, 02, 28, 0.0), Time_Of(2014, 12, 31, 0.0)),
(Time_Of(2012, 02, 29, 0.0), Time_Of(2015, 1, 1, 0.0)),
(Time_Of(2012, 02, 28, 0.0), Time_Of(2012, 02, 29, 0.0)),
(Time_Of(2013, 02, 28, 0.0), Time_Of(2015, 3, 1, 0.0)),
(Time_Of(2012, 02, 2, 0.0), Time_Of(2015, 3, 1, 0.0)));
begin
for Elapsed in Test_Set'Range loop
Difference_In_Years(Test_Set(Elapsed).Top_Date,
Test_Set(Elapsed).Bot_Date,
Years,
Months,
Days,
Houres,
Minutes,
Second,
Rest);
Print_Elapsed(Test_Set(Elapsed).Top_Date,
Test_Set(Elapsed).Bot_Date,
Years,
Months,
Days,
Houres,
Minutes,
Second,
Rest);
Text_Io.New_Line;
end loop;
end Elapsed_Years; |
Partager