Mega Code Archive

 
Categories / Delphi / Forms
 

Convert an Amount of Microseconds into Delphis TDateTime and Displayed Formatted using FormatDateTime

Title: Convert an Amount of Microseconds into Delphi's TDateTime and Displayed Formatted using FormatDateTime The FormatDateTime Delphi RTL function converts a TDateTime value given by DateTime using the format given, into a string value. Using the format parameter you can specify how to display the time part up to the millisecond "precision". When testing your code for speed, or when you have some lengthy function, and you need to display how much time some process took to execute, you would maybe need to take microseconds into account also. The TStopWatch (custom) class can be used to measure the speed of execution where the precision is needed to be in microseconds or even nanoseconds. Note: there are 1000 microseconds in one millisecond and 1000 nanoseconds in one microsecond. Microseconds in FormatDateTime Here's a custom MicrosecondsToTime function you can use to convert an amount of microseconds into a TDateTime value and then convert to a more user friendly representation using the FormatDateTime function: uses DateUtils; function MicrosecondsToTime(const microSeconds : TLargeInteger) : string; CONST MicroPerMilli = 1000; var dt : TDateTime; microSec : TLargeInteger; begin dt := microSeconds / MicroPerMilli / MSecsPerSec / SecsPerDay; microSec := microseconds - MicroPerMilli * MilliSecondOf(dt) ; if microsec then begin dt := IncMilliSecond(dt,-1) ; microsec := MicroPerMilli + microsec; end; result := Format('%d days, %s %d', "FormatDateTime[/link"('hh:nn:ss.z', Frac(dt)), microSec]) ; end; Note: If you have milliseconds and you need a TDateTime value: Convert Milliseconds to Days and Time