Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- package body Date_Handling is
- function Previous_Date (D : in Date_Type) return Date_Type is
- Prev : Date_Type := D;
- begin
- if D.Month = 1 and D.Day = 1 then
- Prev.Year := D.Year - 1;
- Prev.Month := 12;
- Prev.Day := 31;
- elsif D.Month = 3 and D.Day = 1 then
- Prev.Month := 2;
- Prev.Day := 28;
- elsif (D.Month = 5 or D.Month = 7 or D.Month = 10 or D.Month = 12) and D.Day = 1 then
- Prev.Month := D.Month - 1;
- Prev.Day := 30;
- elsif D.Day = 1 then
- Prev.Month := D.Month - 1;
- Prev.Day := 31;
- else
- Prev.Day := D.Day - 1;
- end if;
- return Prev;
- end Previous_Date;
- function Next_Date (D : in Date_Type) return Date_Type is
- Next : Date_Type := D;
- begin
- if D.Month = 12 and D.Day = 31 then
- Next.Year := D.Year + 1;
- Next.Month := 1;
- Next.Day := 1;
- elsif D.Month = 2 and D.Day = 28 then
- Next.Month := 3;
- Next.Day := 1;
- elsif (D.Month = 4 or D.Month = 6 or D.Month = 9 or D.Month = 11) and D.Day = 30 then
- Next.Month := D.Month + 1;
- Next.Day := 1;
- elsif D.Day = 31 then
- Next.Month := D.Month + 1;
- Next.Day := 1;
- else
- Next.Day := D.Day + 1;
- end if;
- return Next;
- end Next_Date;
- function "<" (A, B : in Date_Type) return Boolean is
- begin
- if A.Year < B.Year or (A.Year = B.Year and A.Month < B.Month) or (A.Year = B.Year and A.Month = B.Month and A.Day < B.Day) then
- return True;
- else
- return False;
- end if;
- end "<";
- function ">" (A, B : in Date_Type) return Boolean is
- begin
- if A.Year > B.Year or (A.Year = B.Year and A.Month > B.Month) or (A.Year = B.Year and A.Month = B.Month and A.Day > B.Day) then
- return True;
- else
- return False;
- end if;
- end ">";
- function "=" (A, B : in Date_Type) return Boolean is
- begin
- if A.Year = B.Year and A.Month = B.Month and A.Day = B.Day then
- return True;
- else
- return False;
- end if;
- end "=";
- procedure Get(D : in out Date_Type) is
- C : Character;
- D_28 : Integer range 1..28;
- D_30 : Integer range 1..30;
- begin
- loop
- begin
- begin
- Get(D.Year);
- exception
- when others =>
- raise Year_Error;
- end;
- Get(C);
- begin
- Get(D.Month);
- exception
- when others =>
- raise Month_Error;
- end;
- Get(C);
- begin
- if D.Month = 2 then
- Get(D_28);
- D.Day := D_28;
- elsif D.Month = 4 or D.Month = 6 or D.Month = 9 then
- Get(D_30);
- D.Day := D_30;
- else
- Get(D.Day);
- end if;
- exception
- when others =>
- raise Day_Error;
- end;
- exit;
- exception
- when Year_Error =>
- Put_Line("Ogiltigt år.");
- Skip_Line;
- when Month_Error =>
- Put_Line("Ogiltig månad.");
- Skip_Line;
- when Day_Error =>
- Put_Line("Ogiltig dag.");
- Skip_Line;
- end;
- end loop;
- end Get;
- procedure Put(D : in Date_Type) is
- begin
- if D.Year < 10 then
- Put("000");
- elsif D.Year < 100 then
- Put("00");
- elsif D.Year < 1000 then
- Put("0");
- end if;
- Put(D.Year, 0);
- Put('-');
- if D.Month < 10 then
- Put(0, 0);
- end if;
- Put(D.Month, 0);
- Put('-');
- if D.Day < 10 then
- Put(0, 0);
- end if;
- Put(D.Day, 0);
- end Put;
- end Date_Handling;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement