View Issue Details

IDProjectCategoryView StatusLast Update
0038577FPCRTLpublic2021-03-03 16:52
Reporterwp Assigned To 
PrioritynormalSeverityminorReproducibilityhave not tried
Status newResolutionopen 
Summary0038577: FormatDateTime with option fdoInterval not showing leading zeros
DescriptionThe 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 ReproduceRun 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

TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

wp

2021-03-03 16:50

reporter  

dati.inc.patch (1,286 bytes)   
Index: rtl/objpas/sysutils/dati.inc
===================================================================
--- rtl/objpas/sysutils/dati.inc	(revision 48682)
+++ rtl/objpas/sysutils/dati.inc	(working copy)
@@ -1139,7 +1139,7 @@
             end ;
             'H':
               if isInterval then
-                StoreInt(Hour + trunc(abs(DateTime))*24, 0)
+                StoreInt(Hour + trunc(abs(DateTime))*24, Count)
               else
               if Clock12 then
               begin
@@ -1157,7 +1157,7 @@
                   StoreInt(Hour, 2);
               end;
             'N': if isInterval then
-                   StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, 0)
+                   StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, Count)
                  else
                  if Count = 1 then
                    StoreInt(Minute, 0)
@@ -1164,7 +1164,7 @@
                  else
                    StoreInt(Minute, 2);
             'S': if isInterval then
-                   StoreInt(Second + (Minute + (Hour + trunc(abs(DateTime))*24)*60)*60, 0)
+                   StoreInt(Second + (Minute + (Hour + trunc(abs(DateTime))*24)*60)*60, Count)
                  else
                  if Count = 1 then
                    StoreInt(Second, 0)
dati.inc.patch (1,286 bytes)   

wp

2021-03-03 16:52

reporter   ~0129357

(sorry - pasting in the test results was not a good idea...)

Issue History

Date Modified Username Field Change
2021-03-03 16:50 wp New Issue
2021-03-03 16:50 wp File Added: dati.inc.patch
2021-03-03 16:52 wp Note Added: 0129357