FormatDateTime with option fdoInterval not showing leading zeros
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
The function FormatDateTime can be set up to display hours > 23 (or minutes or seconds > 59) by using the corresponding parameter in square brackets. However, the result string does not display leading zeros even when the format string tells that it should.
Example
FormatDateTime('[hh]:nn:ss.zzz', t, [fdoInterval]), for t = 8:30:15 displays the hour as single-digit number, although the format string tells that it should use a leading zero when the value is < 10.
The attached patch fixes the issue.
Steps to reproduce:
Run this simple test program:
program Project1;
uses
SysUtils;
procedure Test(t: TDateTime; AFormatString: String; Expected: String);
var
s: String;
begin
s := FormatDateTime(AFormatString, t, [fdoInterval]);
Write(TimeToStr(t):15, AFormatString:20, Expected:20, s:20);
if s = Expected then WriteLn('OK':8) else WriteLn('ERROR':8);
end;
var
t: TDateTime;
begin
WriteLn('Time':15, 'Format string':20, 'Expected':20, 'Result':20);
WriteLn;
t := EncodeTime(8, 30, 15, 0);
Test(t, '[h]:nn:ss.zzz', '8:30:15.000');
Test(t, '[hh]:nn:ss.zzz', '08:30:15.000');
t := EncodeTime(0, 8, 15, 0);
Test(t, '[n]:ss', '8:15');
Test(t, '[nn]:ss', '08:15');
t := EncodeTime(0, 0, 8, 216);
Test(t, '[s].zzz', '8.216');
Test(t, '[ss].zzz','08.216');
ReadLn;
end.
The output of the current version of FormatDateTime is
Time Format string Expected Result
08:30:15 [h]:nn:ss.zzz 8:30:15.000 8:30:15.000 OK
08:30:15 [hh]:nn:ss.zzz 08:30:15.000 8:30:15.000 ERROR
00:08:15 [n]:ss 8:15 8:15 OK
00:08:15 [nn]:ss 08:15 8:15 ERROR
00:00:08 [s].zzz 8.216 8.216 OK
00:00:08 [ss].zzz 08.216 8.216 ERROR
After application of the patch the output is correct:
Time Format string Expected Result
08:30:15 [h]:nn:ss.zzz 8:30:15.000 8:30:15.000 OK
08:30:15 [hh]:nn:ss.zzz 08:30:15.000 08:30:15.000 OK
00:08:15 [n]:ss 8:15 8:15 OK
00:08:15 [nn]:ss 08:15 08:15 OK
00:00:08 [s].zzz 8.216 8.216 OK
00:00:08 [ss].zzz 08.216 08.216 OK
Mantis conversion info:
- Mantis ID: 38577
- Fixed in version: 3.3.1
- Fixed in revision: 49299 (#5cde6fac)
- Target version: 3.2.2