-
Notifications
You must be signed in to change notification settings - Fork 1
/
uselecttime.pas
153 lines (128 loc) · 3.57 KB
/
uselecttime.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
148
149
150
151
152
unit uSelectTime;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Math,
DateUtils, uAeView;
type
EDateException = Class(Exception);
{ TfrmSelectTime }
TfrmSelectTime = class(TForm)
btOK: TButton;
btCancel: TButton;
cbTimeFrame: TComboBox;
edEndTime: TEdit;
edStartTime: TEdit;
lbTimeFrame: TLabel;
lbEndTime: TLabel;
lbStartTime: TLabel;
procedure btOKClick(Sender: TObject);
procedure btCancelClick(Sender: TObject);
procedure cbTimeFrameChange(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
public
StartTime: TDateTime;
EndTime: TDateTime;
procedure UpdateTimeFrameStatus;
end;
var
frmSelectTime: TfrmSelectTime;
AbortClose: boolean;
DateException: EDateException;
implementation
{$R *.lfm}
{ TfrmSelectTime }
procedure TfrmSelectTime.UpdateTimeFrameStatus;
// adjust interface to user selection
var
custom: boolean;
begin
custom := false;
edStartTime.Caption := FormatDateTime('YYYY-MM-DD', StartTime);
if EndTime <> 0 then
edEndTime.Caption := FormatDateTime('YYYY-MM-DD', EndTime)
else
edEndTime.Caption := '';
if cbTimeFrame.ItemIndex = 7 then
custom := true;
lbStartTime.Enabled := custom;
lbEndTime.Enabled := custom;
edStartTime.Enabled := custom;
edEndTime.Enabled := custom;
end;
procedure TfrmSelectTime.FormCreate(Sender: TObject);
begin
DateException := EDateException.Create('Invalid date.');
cbTimeFrame.Items.Add('Last 24 hours');
cbTimeFrame.Items.Add('Last 7 days');
cbTimeFrame.Items.Add('Last 15 days');
cbTimeFrame.Items.Add('Last 30 days');
cbTimeFrame.Items.Add('Last 3 months');
cbTimeFrame.Items.Add('Last 6 months');
cbTimeFrame.Items.Add('Last 12 months');
cbTimeFrame.Items.Add('Custom...');
cbTimeFrame.ItemIndex := 0;
end;
procedure TfrmSelectTime.FormShow(Sender: TObject);
begin
UpdateTimeFrameStatus;
end;
procedure TfrmSelectTime.cbTimeFrameChange(Sender: TObject);
begin
if cbTimeFrame.ItemIndex < 7 then
GetPredefinedTime(cbTimeFrame.ItemIndex, StartTime, EndTime);
UpdateTimeFrameStatus;
end;
procedure TfrmSelectTime.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
CanClose := not AbortClose;
AbortClose := false;
end;
procedure TfrmSelectTime.btCancelClick(Sender: TObject);
begin
Close;
end;
procedure TfrmSelectTime.btOKClick(Sender: TObject);
var
vr: TValueRelationship;
begin
if cbTimeFrame.ItemIndex = 7 then
// check start / end date of custom time frame
begin
// check start date
try
StartTime := ScanDateTime('yyyy-mm-dd', edStartTime.Text);
vr := CompareDate(StartTime, Epoch);
if vr < 0 then
Raise DateException;
vr := CompareDate(StartTime, LiveTime(1));
if vr > 0 then
Raise DateException;
except
ShowMessage('Invalid start date.');
AbortClose := true;
exit;
end;
// check end date
try
EndTime := ScanDateTime('yyyy-mm-dd', edEndTime.Text);
EndTime := IncHour(EndTime, 23);
EndTime := IncMinute(EndTime, 59);
EndTime := IncSecond(EndTime, 59);
vr := CompareDate(EndTime, StartTime);
if vr < 0 then
Raise DateException;
vr := CompareDate(EndTime, LiveTime);
if vr > 0 then
Raise DateException;
except
ShowMessage('Invalid end date.');
AbortClose := true;
exit;
end;
end;
end;
end.