home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / a-reatim.adb < prev    next >
Text File  |  2000-07-19  |  7KB  |  209 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
  4. --                                                                          --
  5. --                         A D A . R E A L _ T I M E                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.32 $                             --
  10. --                                                                          --
  11. --            Copyright (C) 1991-1999, Florida State University             --
  12. --                                                                          --
  13. -- GNARL is free software; you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNARL; see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNARL was developed by the GNARL team at Florida State University. It is --
  32. -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
  33. -- State University (http://www.gnat.com).                                  --
  34. --                                                                          --
  35. ------------------------------------------------------------------------------
  36.  
  37. with System.Task_Primitives.Operations;
  38. --  used for Clock
  39.  
  40. package body Ada.Real_Time is
  41.  
  42.    -----------
  43.    -- Clock --
  44.    -----------
  45.  
  46.    function Clock return Time is
  47.    begin
  48.       return Time (System.Task_Primitives.Operations.Clock);
  49.    end Clock;
  50.  
  51.    ---------
  52.    -- "+" --
  53.    ---------
  54.  
  55.    --  Note that Constraint_Error may be propagated
  56.  
  57.    function "+" (Left : Time; Right : Time_Span) return Time is
  58.    begin
  59.       return Time (Duration (Left) + Duration (Right));
  60.    end "+";
  61.  
  62.    function "+" (Left : Time_Span; Right : Time) return Time is
  63.    begin
  64.       return Time (Duration (Left) + Duration (Right));
  65.    end "+";
  66.  
  67.    function "+" (Left, Right : Time_Span) return Time_Span is
  68.    begin
  69.       return Time_Span (Duration (Left) + Duration (Right));
  70.    end "+";
  71.  
  72.    ---------
  73.    -- "-" --
  74.    ---------
  75.  
  76.    --  Note that Constraint_Error may be propagated
  77.  
  78.    function "-" (Left : Time; Right : Time_Span) return Time is
  79.    begin
  80.       return Time (Duration (Left) - Duration (Right));
  81.    end "-";
  82.  
  83.    function "-" (Left, Right : Time) return Time_Span is
  84.    begin
  85.       return Time_Span (Duration (Left) - Duration (Right));
  86.    end "-";
  87.  
  88.    function "-" (Left, Right : Time_Span) return Time_Span is
  89.    begin
  90.       return Time_Span (Duration (Left) - Duration (Right));
  91.    end "-";
  92.  
  93.    function "-" (Right : Time_Span) return Time_Span is
  94.    begin
  95.       return Time_Span_Zero - Right;
  96.    end "-";
  97.  
  98.    ---------
  99.    -- "/" --
  100.    ---------
  101.  
  102.    --  Note that Constraint_Error may be propagated
  103.  
  104.    function "/" (Left, Right : Time_Span) return Integer is
  105.    begin
  106.       return Integer (Duration (Left) / Duration (Right));
  107.    end "/";
  108.  
  109.    function "/" (Left : Time_Span; Right : Integer) return Time_Span is
  110.    begin
  111.       return Time_Span (Duration (Left) / Right);
  112.    end "/";
  113.  
  114.    ---------
  115.    -- "*" --
  116.    ---------
  117.  
  118.    --  Note that Constraint_Error may be propagated
  119.  
  120.    function "*" (Left : Time_Span; Right : Integer) return Time_Span is
  121.    begin
  122.       return Time_Span (Duration (Left) * Right);
  123.    end "*";
  124.  
  125.    function "*" (Left : Integer; Right : Time_Span) return Time_Span is
  126.    begin
  127.       return Time_Span (Left * Duration (Right));
  128.    end "*";
  129.  
  130.    -----------------
  131.    -- To_Duration --
  132.    -----------------
  133.  
  134.    function To_Duration (TS : Time_Span) return Duration is
  135.    begin
  136.       return Duration (TS);
  137.    end To_Duration;
  138.  
  139.    ------------------
  140.    -- To_Time_Span --
  141.    ------------------
  142.  
  143.    function To_Time_Span (D : Duration) return Time_Span is
  144.    begin
  145.       return Time_Span (D);
  146.    end To_Time_Span;
  147.  
  148.    -----------------
  149.    -- Nanoseconds --
  150.    -----------------
  151.  
  152.    function Nanoseconds (NS : Integer) return Time_Span is
  153.    begin
  154.       return Time_Span_Unit * NS;
  155.    end Nanoseconds;
  156.  
  157.    ------------------
  158.    -- Microseconds --
  159.    ------------------
  160.  
  161.    function Microseconds (US : Integer) return Time_Span is
  162.    begin
  163.       return Time_Span_Unit * US * 1_000;
  164.    end Microseconds;
  165.  
  166.    -------------------
  167.    --  Milliseconds --
  168.    -------------------
  169.  
  170.    function Milliseconds (MS : Integer) return Time_Span is
  171.    begin
  172.       return Time_Span_Unit * MS * 1_000_000;
  173.    end Milliseconds;
  174.  
  175.    -----------
  176.    -- Split --
  177.    -----------
  178.  
  179.    procedure Split (T : Time; SC : out Seconds_Count; TS : out Time_Span) is
  180.    begin
  181.       --  Extract the integer part of T
  182.  
  183.       if T = 0.0 then
  184.          SC := 0;
  185.       else
  186.          SC := Seconds_Count (Time_Span'(T - 0.5));
  187.       end if;
  188.  
  189.       --  Since we loose precision when converting a time value to float,
  190.       --  we need to add this check
  191.  
  192.       if Time (SC) > T then
  193.          SC := SC - 1;
  194.       end if;
  195.  
  196.       TS := T - Time (SC);
  197.    end Split;
  198.  
  199.    -------------
  200.    -- Time_Of --
  201.    -------------
  202.  
  203.    function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time is
  204.    begin
  205.       return Time (SC) + TS;
  206.    end Time_Of;
  207.  
  208. end Ada.Real_Time;
  209.