-
Notifications
You must be signed in to change notification settings - Fork 1
/
clock.pas
60 lines (48 loc) · 1.2 KB
/
clock.pas
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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit clock;
interface
uses
sysutils;
type
TClock = class abstract
function Now(): TDateTime; virtual; abstract;
end;
TSystemClock = class(TClock)
function Now(): TDateTime; override;
end;
// A clock that returns the same value every time Now() is called.
// The value is forgotten when Unlatch() is called. The value is taken from the given parent TClock when
// the time is first read after the object is created or after Unlatch() is called.
TStableClock = class(TClock)
private
FParentClock: TClock;
FNow: TDateTime;
public
constructor Create(AParentClock: TClock);
procedure Unlatch();
function Now(): TDateTime; override;
end;
implementation
uses math;
function TSystemClock.Now(): TDateTime;
begin
Result := sysutils.Now();
end;
constructor TStableClock.Create(AParentClock: TClock);
begin
inherited Create();
FParentClock := AParentClock;
FNow := NaN;
end;
procedure TStableClock.Unlatch();
begin
FNow := NaN;
end;
function TStableClock.Now(): TDateTime;
begin
if (IsNaN(FNow)) then
FNow := FParentClock.Now();
Result := FNow;
end;
end.