Advertisement
vonfalk

date_handling.adb

Dec 29th, 2017
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.55 KB | None | 0 0
  1. with Ada.Text_IO; use Ada.Text_IO;
  2. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  3.  
  4. package body Date_Handling is
  5.    function Previous_Date (D : in Date_Type) return Date_Type is
  6.       Prev : Date_Type := D;
  7.    begin
  8.       if D.Month = 1 and D.Day = 1 then
  9.      Prev.Year := D.Year - 1;
  10.      Prev.Month := 12;
  11.      Prev.Day := 31;
  12.       elsif D.Month = 3 and D.Day = 1 then
  13.      Prev.Month := 2;
  14.      Prev.Day := 28;
  15.       elsif (D.Month = 5 or D.Month = 7 or D.Month = 10 or D.Month = 12) and D.Day = 1 then
  16.      Prev.Month := D.Month - 1;
  17.      Prev.Day := 30;
  18.       elsif D.Day = 1 then
  19.      Prev.Month := D.Month - 1;
  20.      Prev.Day := 31;
  21.       else
  22.      Prev.Day := D.Day - 1;
  23.       end if;
  24.       return Prev;
  25.    end Previous_Date;
  26.    
  27.    function Next_Date (D : in Date_Type) return Date_Type is
  28.       Next : Date_Type := D;
  29.    begin
  30.       if D.Month = 12 and D.Day = 31 then
  31.      Next.Year := D.Year + 1;
  32.      Next.Month := 1;
  33.      Next.Day := 1;
  34.       elsif D.Month = 2 and D.Day = 28 then
  35.      Next.Month := 3;
  36.      Next.Day := 1;
  37.       elsif (D.Month = 4 or D.Month = 6 or D.Month = 9 or D.Month = 11) and D.Day = 30 then
  38.      Next.Month := D.Month + 1;
  39.      Next.Day := 1;
  40.       elsif D.Day = 31 then
  41.      Next.Month := D.Month + 1;
  42.      Next.Day := 1;
  43.       else
  44.      Next.Day := D.Day + 1;
  45.       end if;
  46.       return Next;
  47.    end Next_Date;
  48.    
  49.    function "<" (A, B : in Date_Type) return Boolean is
  50.    begin
  51.       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
  52.      return True;
  53.       else
  54.      return False;
  55.       end if;
  56.    end "<";
  57.    
  58.    function ">" (A, B : in Date_Type) return Boolean is
  59.    begin
  60.       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
  61.      return True;
  62.       else
  63.      return False;
  64.       end if;
  65.    end ">";
  66.    
  67.    function "=" (A, B : in Date_Type) return Boolean is
  68.    begin
  69.       if A.Year = B.Year and A.Month = B.Month and A.Day = B.Day then
  70.      return True;
  71.       else
  72.      return False;
  73.       end if;
  74.    end "=";
  75.    
  76.    procedure Get(D : in out Date_Type) is
  77.       C : Character;
  78.       D_28 : Integer range 1..28;
  79.       D_30 : Integer range 1..30;
  80.    begin
  81.       loop
  82.      begin
  83.         begin
  84.            Get(D.Year);
  85.         exception
  86.            when others =>
  87.           raise Year_Error;
  88.         end;
  89.        
  90.         Get(C);
  91.        
  92.         begin
  93.            Get(D.Month);
  94.         exception
  95.            when others =>
  96.           raise Month_Error;
  97.         end;
  98.        
  99.         Get(C);
  100.        
  101.         begin
  102.            if D.Month = 2 then
  103.           Get(D_28);
  104.           D.Day := D_28;
  105.            elsif D.Month = 4 or D.Month = 6 or D.Month = 9 then
  106.           Get(D_30);
  107.           D.Day := D_30;
  108.            else
  109.           Get(D.Day);
  110.            end if;
  111.         exception
  112.            when others =>
  113.           raise Day_Error;
  114.         end;
  115.        
  116.         exit;      
  117.        
  118.      exception
  119.         when Year_Error =>
  120.            Put_Line("Ogiltigt år.");
  121.            Skip_Line;
  122.         when Month_Error =>
  123.            Put_Line("Ogiltig månad.");
  124.            Skip_Line;
  125.         when Day_Error =>
  126.            Put_Line("Ogiltig dag.");
  127.            Skip_Line;
  128.      end;
  129.       end loop;
  130.    end Get;
  131.    
  132.    procedure Put(D : in Date_Type) is
  133.    begin
  134.       if D.Year < 10 then
  135.      Put("000");
  136.       elsif D.Year < 100 then
  137.      Put("00");
  138.       elsif D.Year < 1000 then
  139.      Put("0");
  140.       end if;
  141.       Put(D.Year, 0);
  142.       Put('-');
  143.       if D.Month < 10 then
  144.      Put(0, 0);
  145.       end if;
  146.       Put(D.Month, 0);
  147.       Put('-');
  148.       if D.Day < 10 then
  149.      Put(0, 0);
  150.       end if;
  151.       Put(D.Day, 0);
  152.    end Put;
  153.    
  154. end Date_Handling;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement