stopwatch_0.1.0_624a7c87/src/stopwatch.ads

 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
private with Ada.Calendar;

package Stopwatch is

   type Instance is tagged private;

   procedure Reset (This : in out Instance);

   function Elapsed (This : Instance) return Duration;

   procedure Hold (This : in out Instance; Enable : Boolean := True);
   --  Stop counting time, or re-start if not Enable

   procedure Release (This : in out Instance);
   --  Equivalent to Hold (Enable => False)

   function Is_Held (This : Instance) return Boolean;

   function Image (This : Instance; Decimals : Natural := 2) return String;
   --  Elapsed time in seconds, without leading space, without units

   function Image (Elapsed : Duration; Decimals : Natural := 2) return String;
   --  Convenience to format durations even without a stopwatch

private

   use Ada.Calendar;

   type Instance is tagged record
      Start   : Time     := Clock;
      --  Last moment the timer was released/started

      Held    : Boolean  := False;

      Elapsed : Duration := 0.0;
      --  Track elapsed time when held
   end record;

end Stopwatch;