-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlgRaceTimeUser.pas
147 lines (129 loc) · 3.65 KB
/
dlgRaceTimeUser.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
unit dlgRaceTimeUser;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.WinXCtrls;
type
TRaceTimeUser = class(TForm)
pnlFooter: TPanel;
btnOk: TButton;
btnCancel: TButton;
edtRaceTimeUser: TEdit;
rpnlBody: TRelativePanel;
lblErrMsg: TLabel;
procedure btnCancelClick(Sender: TObject);
procedure btnOkClick(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
fRaceTime: TTime;
fRaceTimeUser: TTime;
public
{ Public declarations }
property RaceTime: TTime read FRaceTime write FRaceTime;
property RaceTimeUser: TTime read fRaceTimeUser write fRaceTimeUser;
end;
var
RaceTimeUser: TRaceTimeUser;
implementation
{$R *.dfm}
uses System.DateUtils;
function TryStrToWord(const S: string; out Value: Word): Boolean;
var
intValue: Integer;
begin
Result := TryStrToInt(S, intValue) and (intValue >= Low(Word)) and (intValue
<= High(Word));
if Result then
Value := Word(intValue);
end;
function TryStrToTimeWithMilliseconds(const S: string; var Time: TTime): Boolean;
var
Min, Sec, MSec: Word;
TimeParts: TArray<string>;
MSecStr: string;
begin
Result := False;
// Split the string into minutes, seconds, and milliseconds
TimeParts := S.Split([':', '.']);
// Ensure there are exactly 3 parts: minutes, seconds, and milliseconds
if Length(TimeParts) = 3 then
begin
// Try to convert minutes and seconds to a word (unsigned 16-bit integer)
if TryStrToWord(TimeParts[0], Min) and
TryStrToWord(TimeParts[1], Sec) then
begin
// Process milliseconds part
MSecStr := TimeParts[2];
// Pad the milliseconds part with zeros if necessary to ensure it is three digits
while Length(MSecStr) < 3 do
MSecStr := MSecStr + '0';
if TryStrToWord(MSecStr, MSec) then
begin
// Validate the range of each part
if (Min < 60) and (Sec < 60) and (MSec < 1000) then
begin
// Encode the time
Time := EncodeTime(0, Min, Sec, MSec);
Result := True;
end;
end;
end;
end;
end;
procedure TRaceTimeUser.btnCancelClick(Sender: TObject);
begin
ModalResult := mrCancel;
end;
procedure TRaceTimeUser.btnOkClick(Sender: TObject);
begin
ModalResult := mrOk;
end;
procedure TRaceTimeUser.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
t: TTime;
begin
if ModalResult = mrOk then
begin
if TryStrToTimeWithMilliseconds(edtRaceTimeUser.Text,t) then
begin
fRaceTimeUser := t;
CanClose := true;
end
else
CanClose := false;
end;
end;
procedure TRaceTimeUser.FormCreate(Sender: TObject);
begin
fRaceTime := 0;
fRaceTimeUser := 0;
end;
procedure TRaceTimeUser.FormKeyDown(Sender: TObject; var Key: Word; Shift:
TShiftState);
begin
if (Key = VK_ESCAPE) then
begin
ModalResult := mrCancel;
end;
lblErrMsg.caption := '';
end;
procedure TRaceTimeUser.FormShow(Sender: TObject);
var
fs: TFormatSettings;
begin
fs := TFormatSettings.Create;
{ The conversion uses the format specified by the LongTimeFormat global
variable or its TFormatSettings equivalent. }
fs.LongTimeFormat := 'nn:ss.zzz';
fs.TimeSeparator := ':';
if (fRaceTimeUser <> 0) then
edtRaceTimeUser.Text := TimeToStr(TimeOF(fRaceTimeUser), fs)
else
edtRaceTimeUser.Text := TimeToStr(TimeOf(fRaceTime), fs);
end;
end.