spat_1.3.0_4ad4ab14/src/util/spat-stop_watch.adb

 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
------------------------------------------------------------------------------
--  Copyright (C) 2020 by Heisenbug Ltd. (gh+spat@heisenbug.eu)
--
--  This work is free. You can redistribute it and/or modify it under the
--  terms of the Do What The Fuck You Want To Public License, Version 2,
--  as published by Sam Hocevar. See the LICENSE file for more details.
------------------------------------------------------------------------------
pragma License (Unrestricted);

package body SPAT.Stop_Watch is

   use type Ada.Real_Time.Time;

   ---------------------------------------------------------------------------
   --  Image
   ---------------------------------------------------------------------------
   function Image (TS : in Ada.Real_Time.Time_Span) return String is
     (Image (Value => Ada.Real_Time.To_Duration (TS => TS)));

   ---------------------------------------------------------------------------
   --  Create
   ---------------------------------------------------------------------------
   function Create return T is
   begin
      return Result : T do
         Result.Reset;
      end return;
   end Create;

   ---------------------------------------------------------------------------
   --  Elapsed
   ---------------------------------------------------------------------------
   function Elapsed (This : in T) return String is
     (Image (TS => Ada.Real_Time.Clock - This.Lap_Time));

   ---------------------------------------------------------------------------
   --  Elapsed_Total
   ---------------------------------------------------------------------------
   function Elapsed_Total (This : in T) return String is
     (Image (TS => Ada.Real_Time.Clock - This.Start_Time));

   ---------------------------------------------------------------------------
   --  Reset
   ---------------------------------------------------------------------------
   procedure Reset (This : in out T) is
      Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;
   begin
      This.Start_Time := Now;
      This.Lap_Time   := Now;
   end Reset;

   ---------------------------------------------------------------------------
   --  Start
   --
   --  Starts a new lap measurement.
   ---------------------------------------------------------------------------
   procedure Start (This : in out T) is
   begin
      This.Lap_Time := Ada.Real_Time.Clock;
   end Start;

end SPAT.Stop_Watch;