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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
unit ChessClockUnit;
interface
type
TChessClock = class
public
class function IsZeitnot(const time: TDateTime): boolean;
class function ConvertToStr(const time: TDateTime): string;
class function ConvertToFullStr(const time: TDateTime;
bIncludeMSec: boolean = TRUE): string;
class function ConvertFromFullStr(const strTime: string): TDateTime;
end;
implementation
uses
SysUtils;
const
FULL_TIME_FORMAT = 'h":"n":"s"."z';
HOUR_TIME_FORMAT = 'h":"nn":"ss';
MIN_TIME_FORMAT = 'n":"ss';
ZEITNOT_FORMAT = 's"."zzz';
ZEITNOT_BOARDER = 10; // sec. - zeitnot border
////////////////////////////////////////////////////////////////////////////////
// TChessClock
class function TChessClock.IsZeitnot(const time: TDateTime): boolean;
begin
Result := ((time > 0) and (time < EncodeTime(0, 0, ZEITNOT_BOARDER, 0)));
end;
class function TChessClock.ConvertToStr(const time: TDateTime): string;
begin
LongTimeFormat := MIN_TIME_FORMAT;
if (time >= EncodeTime(1, 0, 0, 0)) then
LongTimeFormat := HOUR_TIME_FORMAT
else if (IsZeitnot(time)) then
LongTimeFormat := ZEITNOT_FORMAT;
Result := TimeToStr(time);
end;
class function TChessClock.ConvertToFullStr(const time: TDateTime;
bIncludeMSec: boolean = TRUE): string;
begin
if (bIncludeMSec) then
LongTimeFormat := FULL_TIME_FORMAT
else
LongTimeFormat := HOUR_TIME_FORMAT;
Result := TimeToStr(time);
end;
class function TChessClock.ConvertFromFullStr(const strTime: string): TDateTime;
procedure NParse(strTime: string; out Hour, Min, Sec, MSec: Word);
const
TIME_DELIM = ':';
MSEC_DELIM = '.';
var
iPos: integer;
str: string;
begin
Hour := 0;
Min := 0;
Sec := 0;
MSec := 0;
iPos := LastDelimiter(MSEC_DELIM, strTime);
if (iPos > 0) then
begin
str := Copy(strTime, iPos + 1, MaxInt);
strTime := Copy(strTime, 1, iPos - 1);
MSec := StrToInt(str);
end;
strTime := TIME_DELIM + strTime;
iPos := LastDelimiter(TIME_DELIM, strTime);
if (iPos = 0) then
exit;
str := Copy(strTime, iPos + 1, MaxInt);
strTime := Copy(strTime, 1, iPos - 1);
Sec := StrToInt(str);
iPos := LastDelimiter(TIME_DELIM, strTime);
if (iPos = 0) then
exit;
str := Copy(strTime, iPos + 1, MaxInt);
strTime := Copy(strTime, 1, iPos - 1);
Min := StrToInt(str);
iPos := LastDelimiter(TIME_DELIM, strTime);
if (iPos = 0) then
exit;
str := Copy(strTime, iPos + 1, MaxInt);
Hour := StrToInt(str);
end;
var
Hour, Min, Sec, MSec: Word;
begin // .ConvertFromFullStr
NParse(strTime, Hour, Min, Sec, MSec);
Result := EncodeTime(Hour, Min, Sec, MSec);
end;
end.
|