Recent

Author Topic: FormatDateTime with [fdoInterval] does not display an initial 0  (Read 1393 times)

Joek

  • New Member
  • *
  • Posts: 27
When using [fdoInterval] in FormatDateTime, the initial 0 is not displayed. This behaves [hh] and [nn] and [ss].
For example:
writeln (FormatDateTime ('hh: nn: ss: zzz', t));
... returns correctly 08: 58: 32: 255

writeln (FormatDateTime ('[hh]: nn: ss: zzz', t, [fdoInterval]));
... returns 8: 58: 32: 255

If the number of hours exceeds 23, it behaves OK (displays days as hours)

Does anyone know what I'm doing wrong?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: FormatDateTime with [fdoInterval] does not display an initial 0
« Reply #1 on: March 03, 2021, 01:24:08 pm »
Probably nothing, it's probably "Delphi compatible".

Sorry, I lost patience with that mantra when I discovered that milliseconds don't have leading zeroes either.

Feel free to pick over https://github.com/MarkMLl/Contec_cms50dplus/blob/main/common/inifilesabout.pas

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: FormatDateTime with [fdoInterval] does not display an initial 0
« Reply #2 on: March 03, 2021, 02:03:00 pm »
Definitely nothing related to "Delphi compatible" because Delphi does not have this option.

It is easy to fix (however, requires recompilation of the FCL): Open the file dati.inc in folder (fpc)/rtl/objpas/sysutls, find procedure DateTimeToString and the nested procedure StoreFormat. In the large "case Token of" find the cases 'H', 'N', and 'S'. They always begin with
Code: Text  [Select][+][-]
  1.               if isInterval then
  2.                 StoreInt(Hour + trunc(abs(DateTime))*24, 0)    // <--- Replace the "0" by "Count"
(this is for 'H', but 'N' and 'S' are similar. The last parameter, 0, of the StoreInt call should be replaced by 'Count' which is the number of H, N or S in the format string.

Is this a bug? Looks like...

Or is it intentional? An argument could be that your format string can contain as many 'H' as you want, and the output string will have as many digits for the hours as you like. But maybe at some time in the future, the function may be extended to 'D', days, -- in this case the function FormatDateTime will allow two meanings for 'DDDD': in the normal format strings it will be the long weekday name, but in the interval case it will mean "four-digit days". This could lead to some confusion, and maybe this was the reason why no leading zeros were allowed here. I don't know...

Joek

  • New Member
  • *
  • Posts: 27
Re: FormatDateTime with [fdoInterval] does not display an initial 0
« Reply #3 on: March 03, 2021, 04:02:30 pm »
Thank you for the explanation. I do not consider it appropriate to interfere in the FCL in this way.
Inifilesabout.pas is an inspiration!
For my purposes, a short procedure will suffice, it was just not clear to me whether I am not making a mistake in calling FormatDateTime.

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: FormatDateTime with [fdoInterval] does not display an initial 0
« Reply #4 on: March 03, 2021, 04:53:22 pm »
Nevertheless, I posted a bug report (https://bugs.freepascal.org/view.php?id=38577). Let's the FPC devs decide whether this is a bug or a feature.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: FormatDateTime with [fdoInterval] does not display an initial 0
« Reply #5 on: March 04, 2021, 09:11:17 pm »
Inifilesabout.pas is an inspiration!

You can tell how long I've been tinkering with that by the fact that I started off using Modula-2 style uppercase keywords :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018