加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何将毫秒转换为TDateTime?

发布时间:2020-12-15 09:49:58 所属栏目:大数据 来源:网络整理
导读:我正在做一个长循环下载数千个文件.我想显示剩余的估计时间,因为它可能需要数小时.但是,根据我的编写,我得到平均毫秒数.如何将此平均下载时间从毫秒转换为TDateTime? 看看我在哪里设置Label1.Caption: procedure DoWork;const AVG_BASE = 20; //recent fil
我正在做一个长循环下载数千个文件.我想显示剩余的估计时间,因为它可能需要数小时.但是,根据我的编写,我得到平均毫秒数.如何将此平均下载时间从毫秒转换为TDateTime?

看看我在哪里设置Label1.Caption:

procedure DoWork;
const
  AVG_BASE = 20; //recent files to record for average,could be tweaked
var
  Avg: TStringList; //for calculating average
  X,Y: Integer; //loop iterators
  TS,TE: DWORD; //tick counts
  A: Integer; //for calculating average
begin
  Avg:= TStringList.Create;
  try
    for X:= 0 to FilesToDownload.Count - 1 do begin //iterate through downloads
      if FStopDownload then Break; //for cancelling
      if Avg.Count >= AVG_BASE then //if list count is 20
        Avg.Delete(0); //remove the oldest average
      TS:= GetTickCount; //get time started
      try
        DownloadTheFile(X); //actual file download process
      finally
        TE:= GetTickCount - TS; //get time elapsed
      end;
      Avg.Add(IntToStr(TE)); //add download time to average list
      A:= 0; //reset average to 0
      for Y:= 0 to Avg.Count - 1 do //iterate through average list
        A:= A + StrToIntDef(Avg[Y],0); //add to total download time
      A:= A div Avg.Count; //divide count to get average download time
      Label1.Caption:= IntToStr(A); //<-- How to convert to TDateTime?
    end;
  finally
    Avg.Free;
  end;
end;

PS – 我可以通过不同方式计算最近20次(或AVG_BASE)下载的平均速度,因为我确信我的字符串列表解决方案并不是最好的.我不想根据所有下载来计算它,因为速度可能会在那段时间内发生变化.因此,我只是检查最后20个.

解决方法

TDateTime值基本上是一个double,其中整数部分是天数,fraction是时间.

在一天中有24 * 60 * 60 = 86400秒(在SysUtils中声明的SecsPerDay常量)所以得到A作为TDateTime do:

dt := A/(SecsPerDay*1000.0); // A is the number of milliseconds

计时时间的更好方法是在诊断单元中使用TStopWatch构造.

例:

sw.Create;
.. 
sw.Start;
// Do something
sw.Stop;
A := sw.ElapsedMilliSeconds;
// or as RRUZ suggested ts := sw.Elapsed; to get the TimeSpan

要获得平均时间,请考虑使用此移动平均记录:

Type
  TMovingAverage = record
  private
    FData: array of integer;
    FSum: integer;
    FCurrentAverage: integer;
    FAddIx: integer;
    FAddedValues: integer;
  public
    constructor Create(length: integer);
    procedure Add( newValue: integer);
    function Average : Integer;
  end;

procedure TMovingAverage.Add(newValue: integer);
var i : integer;
begin
  FSum := FSum + newValue - FData[FAddIx];
  FData[FAddIx] := newValue;
  FAddIx := (FAddIx + 1) mod Length(FData);
  if (FAddedValues < Length(FData)) then
    Inc(FAddedValues);
  FCurrentAverage := FSum div FAddedValues;
end;

function TMovingAverage.Average: Integer;
begin
  Result := FCurrentAverage;
end;

constructor TMovingAverage.Create(length: integer);
var
  i : integer;
begin
  SetLength( FData,length);
  for i := 0 to length - 1 do
    FData[i] := 0;
  FSum := 0;
  FCurrentAverage := 0;
  FAddIx := 0;
  FAddedValues := 0;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读