Reputation: 13
Looking for a code sample that returns the difference in seconds between two TTime values with positive and negative values. I tried with SecondsBetween function which is part of the DateUtils unit but it gives only positive value.
Upvotes: 1
Views: 274
Reputation: 18027
The signed difference is as simple as this:
uses DateUtils, Math;
function TimeDiff(t1, t2: TTime): Integer;
begin
Result := Sign(t2 - t1) * SecondsBetween(t1, t2);
end;
Upvotes: 3
Reputation: 8243
The below code will return the (signed) difference between two TTime values, like if you'd done a T1-T2.
USES System.DateUtils;
FUNCTION TimeDifference(T1,T2 : TTime) : INTEGER;
BEGIN
Result:=ROUND((FRAC(T1)-FRAC(T2))*SecsPerDay)
END;
Sample code:
T1:=EncodeTime(12,0,0,0);
T2:=EncodeTime(13,0,0,0);
WRITELN(TimeDifference(T1,T2));
WRITELN(TimeDifference(T2,T1));
Output:
-3600
3600
Please note, that there's no "negative TTime value". A TTime is always a positive value - the time since midnight on any given day.
Upvotes: 0