-
Notifications
You must be signed in to change notification settings - Fork 0
/
OtherTime.pas
77 lines (63 loc) · 1.62 KB
/
OtherTime.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
unit OtherTime;
interface
type
TCustomTime = class
protected
function GetTime: TDateTime; virtual; abstract;
function GetUnixTime: Int64;
public
property Time: TDateTime read GetTime;
property UnixTime: Int64 read GetUnixTime;
end;
TSystemTime = class(TCustomTime)
protected
function GetTime: TDateTime; override;
public
end;
TDeltaSystemTime = class(TCustomTime)
protected
fTimeDelta: TDateTime;
function GetTime: TDateTime; override;
public
procedure setTime(reference_time: TDateTime);
constructor Create(aTimeDelta: TDateTime = 0);
property TimeDelta: TDateTime read fTimeDelta write fTimeDelta;
function delayToStr: string;
end;
implementation
uses DateUtils, SysUtils;
constructor TDeltaSystemTime.Create(aTimeDelta: TDateTime = 0);
begin
inherited Create();
fTimeDelta := aTimeDelta;
end;
function TDeltaSystemTime.delayToStr: string;
var days: integer;
begin
days := trunc(TimeDelta);
if (TimeDelta > 0) then
Result := '+'
else
Result := '-';
if (days > 1) then
Result := Result + IntToStr(days) + ' days ';
Result := Result + TimeToStr(TimeDelta);
end;
function TDeltaSystemTime.GetTime: TDateTime;
begin
Result := Now + TimeDelta;
end;
procedure TDeltaSystemTime.setTime(reference_time: TDateTime);
begin
TimeDelta := reference_time - Now();
end;
function TSystemTime.GetTime: TDateTime;
begin
Result := Now;
end;
function TCustomTime.GetUnixTime: Int64;
begin
// force to round to the lower number
Result := DateTimeToUnix(GetTime - 0.5/24/60/60);
end;
end.