Recent

Author Topic: Strange behavior with date format on Linux Mint  (Read 641 times)

JanRoza

  • Hero Member
  • *****
  • Posts: 742
    • http://www.silentwings.nl
Strange behavior with date format on Linux Mint
« on: April 10, 2026, 05:22:22 pm »
I have a program that's working for years without errors on Windows but trying to run it in Linux Mint 22.3 it keeps complaining about invalid date.
Even when after forcing the date separator to '-' just before, it still translates Now to a date with slashes instead of the default date-separator. (see attached screenshot of watch window)
In Linux Mint the date format is set to dd-mm-yyyy. (see attached screenshot of the systembar)
For clarity I include the code part where this happens.
Code: Pascal  [Select][+][-]
  1. procedure VerwijderOudeBackups;
  2. var
  3.   sl: TStringList;
  4.   fd: String;
  5.   fn: String;
  6.   i: Integer;
  7.   x: Integer;
  8.   fDate: TDateTime;
  9.  
  10. begin
  11.   sl := TStringList.Create;
  12.   sl := FindAllFiles(ExtractFilePath(Application.ExeName), '*.db', False);
  13.  
  14.   try
  15.     for i := 0 to sl.Count-1 do
  16.     begin
  17.       fn := ExtractFileNameEX(sl[i]);
  18.  
  19.       // Alleen echte backups behandelen
  20.       if Copy(fn, Length(fn), 1) = ')'
  21.       then begin
  22.            if Copy(fn, Length(fn)-10, 1) = ' '          // Datum d-m-jjjj
  23.            then x := 8
  24.            else if Copy(fn, Length(fn)-10, 1) = '('     // Datum d-mm-jjjj
  25.                 then x := 9
  26.                 else x := 10;                           // Datum dd-mm-jjjj
  27.  
  28.            // File datum samenstellen
  29.            DefaultFormatSettings.DateSeparator := '-';
  30.            DefaultFormatSettings.ShortDateFormat := 'dd-mm-yyyy';
  31.            fDate := Now;
  32.  
  33.            fd := Copy(fn, Length(fn)-x, x);
  34.            fDate := StrToDateTime(fd);
  35.  
  36.            // Backups ouder dan x dagen mogen weg
  37.            if DaysBetween(Now, fDate) > Ouderdom
  38.            then DeleteFile(PChar(sl[i]));
  39.       end;
  40.     end;
  41.   finally
  42.     sl.Free;
  43.   end;
  44.  

I always end with an errormessage that fd contains an invalid date (which contains '07-04-2026' as you can see in the watch window).
Anyone any idea where I go wrong?
OS: Windows 11 / Linux Mint 22.3
       Lazarus 4.6 RC FPC 3.2.2
       CodeTyphon 8.90 FPC 3.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 18944
  • Glad to be alive.
Re: Strange behavior with date format on Linux Mint
« Reply #1 on: April 10, 2026, 05:35:52 pm »
First check what the defaultformatsettings actually contains.....
That may be very different from your expectations.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

JanRoza

  • Hero Member
  • *****
  • Posts: 742
    • http://www.silentwings.nl
Re: Strange behavior with date format on Linux Mint
« Reply #2 on: April 10, 2026, 05:58:26 pm »
Thanks Thaddy, but after some more experimenting I got it working by moving the statements for default settings to the formCreate of my main form.
Since then it works as expected.
OS: Windows 11 / Linux Mint 22.3
       Lazarus 4.6 RC FPC 3.2.2
       CodeTyphon 8.90 FPC 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 13484
Re: Strange behavior with date format on Linux Mint
« Reply #3 on: April 10, 2026, 06:13:58 pm »
You probably did not add the linux-only unit "clocale" to the uses clauses when going from Windows to Linux. This applies the format settings of your system to the DefaultFormatSettings record of your appliation.

Just a remark: After changing the DefaultformatSettings for this short conversion routine you should restore the old DefaultFormatsettings to avoid that the application behaves differently from now on. Or better: Do not touch DefaultFormatSettings at all, but use a local copy which you provide as additional parameter to StrToDate:
Code: Pascal  [Select][+][-]
  1. procedure VerwijderOudeBackups;
  2. var
  3.   fs: TFormatSettings;
  4. begin
  5.   fs := DefaultFormatSettings;
  6.   fs.DateSeparator := '-';
  7.   fs.ShortDateFormat := 'dd-mm-yyyy';
  8.   ...
  9.   fdate := StrToDateTime(fd, fs);
  10.   ...
  11. end;
I also would not use the ShortDateFormat 'dd-mm-yyyy', but 'dd/mm/yyyy' since the slash '/' is automatically replaced by the value of the DateSeparator which gives you greater flexibility.

Another idea, maybe, is to use the TryStrToDateTime variant which does not raise an exception in case of a format error. In this case you can try other date formats if you find that the structure of your filenames is not consistent:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFormatSettings;
  3. begin
  4.   fs := DefaultFormatSettings;
  5.   fs.DateSeparator := '-';
  6.   fs.ShortDateFormat := 'dd/mm/yyyy';
  7.   if not TryStrToDateTime(fd, fs, fdate) then
  8.   begin
  9.     fs.DateSeparator := '.';
  10.     if not TryStrToDatetime(fd, fs, fdate) then
  11.       raise Exception.CreateFmt('The filename "%s" contains an invalid date which must be in the format "dd-mm-yyyy" or "dd.mm.yyyy"', [fn]);
  12.  

Final remark:
The StrToDateTime conversion is often very strict. In many cases, it is easier to use the ScanDateTime function from unit DateUtil.
« Last Edit: April 10, 2026, 06:16:33 pm by wp »

JanRoza

  • Hero Member
  • *****
  • Posts: 742
    • http://www.silentwings.nl
Re: Strange behavior with date format on Linux Mint
« Reply #4 on: April 10, 2026, 06:20:38 pm »
Quote
You probably did not add the linux-only unit "clocale" to the uses clauses when going from Windows to Linux. This applies the format settings of your system to the DefaultFormatSettings record of your appliation.

Thanks wp, I indeed never thought about clocale.
I will add that to my project and see how it behaves after removing the other formatting statements.
OS: Windows 11 / Linux Mint 22.3
       Lazarus 4.6 RC FPC 3.2.2
       CodeTyphon 8.90 FPC 3.3.1

 

TinyPortal © 2005-2018