Delphi 转换为延长时间

Delphi 转换为延长时间,delphi,Delphi,我需要将扩展值转换为时间格式。例如: 3.50 represents 00:03:50 62.02 represents 01:02:02 73.70 represents 01:14:10 我尝试了以下函数来转换小时和分钟部分,但我不知道如何转换秒部分 function ConvertToTime(AValue: Extended): TDateTime; begin Result:= EncodeTime(trunc(ArticleRec.Quantity) div 60,tr

我需要将扩展值转换为时间格式。例如:

3.50 represents  00:03:50
62.02 represents 01:02:02
73.70 represents 01:14:10
我尝试了以下函数来转换小时和分钟部分,但我不知道如何转换秒部分

function ConvertToTime(AValue: Extended): TDateTime;
begin
    Result:=  EncodeTime(trunc(ArticleRec.Quantity) div 60,trunc(ArticleRec.Quantity) mod 60,0,0);
end;

感谢您的帮助。

分数部分如下:

var
  SecondsFrac: Double;
....
SecondsFrac := Frac(Value);
var
  Seconds: Integer;
....
Seconds := Round(SecondsFrac*100);
procedure DecodeSeconds(Value: Integer; out Hours, Minutes, Seconds: Integer);
begin
  Seconds := Value mod 60;
  Value := Value div 60;
  Minutes := Value mod 60;
  Value := Value div 60;
  Hours := Value;
end;
然后可以将0到1范围内的浮点分数值转换为0到100范围内的整数,如下所示:

var
  SecondsFrac: Double;
....
SecondsFrac := Frac(Value);
var
  Seconds: Integer;
....
Seconds := Round(SecondsFrac*100);
procedure DecodeSeconds(Value: Integer; out Hours, Minutes, Seconds: Integer);
begin
  Seconds := Value mod 60;
  Value := Value div 60;
  Minutes := Value mod 60;
  Value := Value div 60;
  Hours := Value;
end;
不过,这是一种非常奇怪的存储时间的方式。您必须处理这样一个事实,即当
Seconds>=60
时,您需要增加分钟,并通过
60
减少
Seconds

我想我应该把时间转换成秒,然后从那里开始:

function ConvertWeirdTimeFormatToSeconds(const Value: Double): Integer;
var
  SecondsFrac: Double;
begin
  SecondsFrac := Frac(Value);
  Result := Round(SecondsFrac*100) + Trunc(Value)*60;
end;
然后,您可以将秒数解码为不同的部分,如下所示:

var
  SecondsFrac: Double;
....
SecondsFrac := Frac(Value);
var
  Seconds: Integer;
....
Seconds := Round(SecondsFrac*100);
procedure DecodeSeconds(Value: Integer; out Hours, Minutes, Seconds: Integer);
begin
  Seconds := Value mod 60;
  Value := Value div 60;
  Minutes := Value mod 60;
  Value := Value div 60;
  Hours := Value;
end;
这让我觉得从午夜开始,把时间存储成整数秒可能更好。在我看来,使用标准格式更有意义


我认为没有理由在这里或任何地方使用
扩展
。这是一种非标准类型,由于其奇怪的大小和随之而来的对齐问题,其性能往往很差。而且它仅在x86上受支持。

感谢您的帮助。你建议我使用哪种标准格式?这取决于你的目标是什么。我不知道你的动机是什么。根据说明书,时间是延长的。我以前只为数小时和数分钟的转换提供服务,现在我还需要为第二部分提供服务。是否有其他更简单的方法以数字表示秒的部分。将其存储为
Seconds/60
是一种选择。然而,使用
Extended
无疑是一个糟糕的设计选择。许多人并不真正理解扩展的
的缺点,也没有意识到实际上不存在的好处。谁做出了这个选择?时间跨度和时间是不同的。无论如何,我想我已经回答了您提出的问题。如果您要使用duration(而不是一天的时间),那么您可以使用record(如果您仍然使用Delphi XE2),而不是
TDateTime
数据类型(因为您不容易表示超过24小时的值)。例如
TTimeSpan.FromMinutes
函数可以根据您的格式提供时间跨度记录。好的,是的,我使用的是XE2。谢谢你,我将尝试TTimeSpan。再次感谢David Heffernan和TLama的解决方案。我使用David Heffernan的函数将双精度值转换为秒,然后使用TTimeSpan.FromSeconds并将时间跨度格式化为HH:MM:SS持续时间:1。总计:=TTimeSpan.FromSeconds(ConvertWeirdTimeFormatSeconds(62.02))2。格式('%2d:%.2d:%.2d',[Total.days*24+Total.Hours,Total.Minutes,Total.Seconds])