分时间片计算包间费演示程序

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, DateUtils;

type
  TForm1 = class(TForm)
    DTPStartDate: TDateTimePicker;
    DTPStartTime: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    Button1: TButton;
    Memo1: TMemo;
    DTPEndTime: TDateTimePicker;
    DTPEndDate: TDateTimePicker;
    DTP01A: TDateTimePicker;
    DTP01B: TDateTimePicker;
    Label3: TLabel;
    DTP02A: TDateTimePicker;
    Timer1: TTimer;
    DTP02B: TDateTimePicker;
    DTP03A: TDateTimePicker;
    DTP03B: TDateTimePicker;
    DTP04A: TDateTimePicker;
    DTP04B: TDateTimePicker;
    DTP05A: TDateTimePicker;
    DTP05B: TDateTimePicker;
    DTP06A: TDateTimePicker;
    DTP06B: TDateTimePicker;
    Label4: TLabel;
    procedure DTP01BChange(Sender: TObject);
    procedure DTP02BChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DTP03BChange(Sender: TObject);
    procedure DTP04BChange(Sender: TObject);
    procedure DTP05BChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DTP01BChange(Sender: TObject);
begin
  if DTP01B.Time < DTP01A.Time then begin DTP01B.Time := DTP01A.Time; exit; end;
  if SecondsBetween(DTP01B.Time, strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99')) = 0 then
  begin
    DTP02A.Visible := false; DTP02B.Visible := false; DTP03A.Visible := false; DTP03B.Visible := false; DTP04A.Visible := false; DTP04B.Visible := false; DTP05A.Visible := false; DTP05B.Visible := false; DTP06A.Visible := false; DTP06B.Visible := false;
  end else
  begin
    DTP02A.Visible := true; DTP02B.Visible := true; DTP02B.Time := strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99');
    DTP02A.Time := IncSecond(DTP01B.Time, 1);
  end;
end;

procedure TForm1.DTP02BChange(Sender: TObject);
begin
  if DTP02B.Time < DTP02A.Time then begin DTP02B.Time := DTP02A.Time; exit; end;
  if SecondsBetween(DTP02B.Time, strtodatetime(formatDatetime('yyyy-MM-dd', DTP02B.date) + ' 23:59:59.99')) = 0 then
  begin
    DTP03A.Visible := false; DTP03B.Visible := false; DTP04A.Visible := false; DTP04B.Visible := false; DTP05A.Visible := false; DTP05B.Visible := false; DTP06A.Visible := false; DTP06B.Visible := false;
  end else
  begin
    DTP03A.Visible := true; DTP03B.Visible := true; DTP03B.Time := strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99');
    DTP03A.Time := IncSecond(DTP02B.Time, 1);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  DTP01B.Time := strtodatetime(formatDatetime(' hh:mm:ss', strtodatetime(' 23:59:59.99')));
end;

procedure TForm1.DTP03BChange(Sender: TObject);
begin
  if DTP03B.Time < DTP03A.Time then begin DTP03B.Time := DTP03A.Time; exit; end;
  if SecondsBetween(DTP03B.Time, strtodatetime(formatDatetime('yyyy-MM-dd', DTP03B.date) + ' 23:59:59.99')) = 0 then
  begin
    DTP04A.Visible := false; DTP04B.Visible := false; DTP05A.Visible := false; DTP05B.Visible := false; DTP06A.Visible := false; DTP06B.Visible := false;
  end else
  begin
    DTP04A.Visible := true; DTP04B.Visible := true; DTP04B.Time := strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99');
    DTP04A.Time := IncSecond(DTP03B.Time, 1);
  end;
end;

procedure TForm1.DTP04BChange(Sender: TObject);
begin
  if DTP04B.Time < DTP04A.Time then begin DTP04B.Time := DTP04A.Time; exit; end;
  if SecondsBetween(DTP04B.Time, strtodatetime(formatDatetime('yyyy-MM-dd', DTP03B.date) + ' 23:59:59.99')) = 0 then
  begin
    DTP05A.Visible := false; DTP05B.Visible := false; DTP06A.Visible := false; DTP06B.Visible := false;
  end else
  begin
    DTP05A.Visible := true; DTP05B.Visible := true; DTP05B.Time := strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99');
    DTP05A.Time := IncSecond(DTP04B.Time, 1);
  end;
end;

procedure TForm1.DTP05BChange(Sender: TObject);
begin
  if DTP05B.Time < DTP05A.Time then begin DTP05B.Time := DTP05A.Time; exit; end;
  if SecondsBetween(DTP05B.Time, strtodatetime(formatDatetime('yyyy-MM-dd', DTP05B.date) + ' 23:59:59.99')) = 0 then
  begin
    DTP06A.Visible := false; DTP06B.Visible := false;
  end else
  begin
    DTP06A.Visible := true; DTP06B.Visible := true; DTP06B.Time := strtodatetime(formatDatetime('yyyy-MM-dd', DTP01B.date) + ' 23:59:59.99');
    DTP06A.Time := IncSecond(DTP05B.Time, 1); DTP06B.Enabled := false;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  tempCurrDate, tempbegin, tempend: Tdatetime;
  tempTotNum, i: integer;
  YorN: boolean; //判断是不是计算完成的标志
begin
  YorN := true; i := 1; 
  if DTP06A.Visible then tempTotNum := 6 else if DTP05A.Visible then tempTotNum := 5 else if DTP04A.Visible then tempTotNum := 4 else if DTP03A.Visible then tempTotNum := 3 else if DTP02A.Visible then tempTotNum := 2 else if DTP01A.Visible then tempTotNum := 1;
  tempCurrDate := strtodatetime(formatDatetime('yyyy-MM-dd', DTPStartDate.date) + formatDatetime(' hh:mm:ss', DTPStartTime.Time));
  while YorN do
  begin
    for i := 1 to tempTotNum do
    begin
      // TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'A')).Time;
      tempbegin := strtodatetime(formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', (TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'A')).Time)));
      tempend := strtodatetime(formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', (TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'B')).Time)));
      if (tempCurrDate >= tempbegin) and (tempCurrDate <= tempend) then
      begin
        if strtodatetime(formatDatetime('yyyy-MM-dd', DTPEndDate.date) + formatDatetime(' hh:mm:ss', DTPEndTime.Time)) <= (strtodatetime(formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', (TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'B')).Time)))) then
        begin
          Memo1.Lines.Add(formatDatetime('yyyy-MM-dd hh:mm:ss', tempCurrDate) + '--->' + formatDatetime('yyyy-MM-dd', DTPEndDate.date) + formatDatetime(' hh:mm:ss', DTPEndTime.Time));
          exit;
        end
        else
        begin
          Memo1.Lines.Add(formatDatetime('yyyy-MM-dd hh:mm:ss', tempCurrDate) + '--->' + formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', (TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'B')).Time)));
          tempCurrDate := IncSecond(strtodatetime(formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', (TDateTimePicker(FindComponent('DTP0' + trim(inttostr(i)) + 'B')).Time))));
        end;
      end;
    end;
    //如果超过一天了话再循环计算:tempCurrDate是已经加了一秒后的时间。
    //tempCurrDate := strtodatetime(formatDatetime('yyyy-MM-dd', tempCurrDate) + formatDatetime(' hh:mm:ss', tempCurrDate));
    if tempCurrDate >= strtodatetime(formatDatetime('yyyy-MM-dd', DTPEndDate.date) + formatDatetime(' hh:mm:ss', DTPEndTime.Time)) then YorN := false else YorN := true;
  end;
end;


end.

posted @ 2012-06-02 07:10  bingege  阅读(315)  评论(0编辑  收藏  举报