How to remove the seconds from TTime without resorting to TimeToStr(const datetime:TDateTime; const formatsettings:TFormatSettings)
Asked Answered
B

5

11

How can the seconds be removed from a TTime variable without resorting to the extra overhead of using TimeToStr(const datetime:TDateTime; const formatsettings:TFormatSettings) to get the TTime value without the seconds?

ie this is what I need -> HH:MM:00.

Is there some kind of math operation (like ANDing or ORing the value with something) that can be performed?

Bick answered 25/6, 2011 at 4:52 Comment(2)
Actually, you can't! A TTime variable is stored internally as a float. Float values can have rounding errors. When rounding a time to minutes, a rounding error might occur, transforming to e.g. 13:17:59 or 13:18:01 instead of 13:18:00... Very small chance, but still...Lavonnelaw
Yes, this is correct and can occasionally be a cause of hard to find errors. If your application is really depending on this to be exact, do not use TTime or TDateTime for time calculations. Keep also in mind that future versions of Delphi may change the precision of floating point calculations. Another thing about using DateUtils, be sure to use the latest version or at least the one Nick Hodges released [cc.embarcadero.com/item/27675]. John Herbster has written much about a better format for TDateTime [forums.embarcadero.com/thread.jspa?threadID=3867].Albany
A
18
var
  t: TTime;
  iHour, iMin, iSec, iMSec: Word;

DecodeTime(t, iHour, iMin, iSec, iMSec);
t := EncodeTime(iHour, iMin, 0, 0);
Abadan answered 25/6, 2011 at 5:3 Comment(0)
P
22
uses
  ..., DateUtils;

var
  t: TTime;
begin
  t := ...;
  t := RecodeSecond(t, 0);
end;
Platoon answered 25/6, 2011 at 7:22 Comment(0)
A
18
var
  t: TTime;
  iHour, iMin, iSec, iMSec: Word;

DecodeTime(t, iHour, iMin, iSec, iMSec);
t := EncodeTime(iHour, iMin, 0, 0);
Abadan answered 25/6, 2011 at 5:3 Comment(0)
A
3
var
t : TTime;

t := Trunc(t * MinsPerDay) / MinsPerDay;

EDIT :

This is a more accurate function to truncate the seconds. It rounds the time to nearest milliseconds before truncating.

uses
  SysUtils;

const
  FMSecsPerDay: Double = MSecsPerDay;
  FMSecsPerMinute: Double = SecsPerMin * MSecsPerSec;  
  FMinsPerDay: Double = HoursPerDay * MinsPerHour;


function TruncateSeconds(aTime: TDateTime): TDateTime;
begin
  { - Round to closest millisecond before truncating the seconds }
  Result := Trunc(Round(aTime * FMSecsPerDay) / FMSecsPerMinute) / FMinsPerDay;
  //              -- Time in Milliseconds --
  //        ------------- Time in minutes -----------------------
end;
Albany answered 25/6, 2011 at 6:52 Comment(7)
It would be interesting to see which of @Abadan and your methods is most efficient - but probably of only very occasional value in the real world.Mahmud
@Gerry, this one is hard to beat in CPU time, but the main reason I use it is that no extra helper declarations is needed.Albany
This is bad practice. The helpers are your friends and not your enemies you have suffered implementation leak.Core
@David, well the op did ask for a math operation. Normally this is in my MyDateUtils unit as a function.Albany
David: What is "implementation leak"?Meredi
+1 this is the fastest and the cleanest method (literally: round down to the minutes)Stink
David, this is a well established technique for rounding up values, but I realize not all programmers are educated in math. Having to resort to calling two library functions for pulling this out, in my opinion is close to code bloat. Thanks NGLN.Albany
M
0
LongTimeFormat="hh:mm";
Label1->Caption=TimeToStr(Time());
Molality answered 23/1, 2014 at 9:16 Comment(0)
U
-1

Piece of cake, dudes !

s:=copy(TimeToStr(TIME),1,length(TimeToStr(TIME))-3);

Unmoral answered 4/7, 2024 at 14:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.