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
| with Ada.Integer_Text_Io;
with Ada.Text_Io;
with Ada.Calendar;
use Ada.Calendar;
with Ada.Calendar.Time_Zones;
with Ada.Calendar.Formatting;
with Ada.Calendar.Arithmetic;
use Ada.Calendar.Arithmetic;
use Ada;
procedure Test_Elapsed is
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
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);
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;
if Top_Year < Bot_Year then
Years := (Bot_Year - 1) - (Top_Year);
if Bot_Month - 1 >= Top_Month then
Months := 12 - ((Bot_Month) - (Top_Month));
else
Months := 12 - ((Top_Month) - (Bot_Month - 1));
end if;
else
Months := (Bot_Month - Top_Month);
end if;
if Bot_Day - 1 > Top_Day then
if Top_Month > 1 then
Days := (Days_Months_Count(Top_Month-1) - Top_Day) + Bot_Day;
else
Days := (Days_Months_Count(Days_Months_Count'last) - Top_Day) + Bot_Day;
end if;
elsif Bot_Day - 1 < Top_Day then
if Top_Month < 12 then
Days := (Days_Months_Count(Top_Month) - Top_Day) + (Bot_Day - 1);
if Days = Days_Months_Count(Top_Month + 1) then
Text_Io.Put_Line("3.1");
Days := 0;
end if;
else
Days := (Days_Months_Count(Days_Months_Count'first) - Top_Day) + (Bot_Day - 1);
if Days = Days_Months_Count(1) then
Days := 0;
end if;
end if;
else
if Is_Leap_Year(Bot_Year) then
if Bot_Month = 2 then
if Bot_Day > Days_Months_Count(2) then
Days := 1;
end if;
else
if Top_Month > 1 then
Days := (Days_Months_Count(Top_Month-1) - Top_Day) + Bot_Day - 1;
else
Days := (Days_Months_Count(Days_Months_Count'last) - Top_Day) + Bot_Day - 1;
end if;
end if;
end if;
end if;
end Difference_In_Years;
Top_Date : Time := Time_Of(2010, 01, 27, 18000.0);
Bot_Date : Time := Time_Of(2012, 02, 29, 18000.0);
Years : Natural := 0;
Months : Natural := 0;
Days : Natural := 0;
Houres : Natural := 0;
Minutes : Natural := 0;
Second : Natural := 0;
Rest : Natural := 0;
begin
Difference_In_Years(Top_Date,
Bot_Date,
Years,
Months,
Days,
Houres,
Minutes,
Second,
Rest);
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, ");
Text_Io.Put(Natural'Image(Houres)(2..Natural'Image(Houres)'last));
Text_Io.Put(':');
Text_Io.Put(Natural'Image(minutes)(2..Natural'Image(Minutes)'last));
Text_Io.Put(':');
Text_Io.Put(Natural'Image(Second)(2..Natural'Image(Second)'last));
Text_Io.Put('.');
Text_Io.Put(Natural'Image(Rest)(2..Natural'Image(Rest)'last));
Text_Io.Put('s' & Character'Val(10));
end Test_Elapsed; |
Partager