All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.delphi.rules.community-delphi.DateFormatSettings.html Maven / Gradle / Ivy

There is a newer version: 1.12.1
Show newest version

Why is this an issue?

A TFormatSettings argument can be provided to the TDate and TDateTime string formatting functions in System.SysUtils. If a TFormatSettings argument is not provided, then the global TFormatSettings will be used by default.

Relying on the global TFormatSettings is problematic because it is populated with system default values from the OS, leading to different behavior on different machines.

Functions that accept a TFormatSettings argument include:

  • System.SysUtils.DateToStr
  • System.SysUtils.DateTimeToStr
  • System.SysUtils.StrToDate
  • System.SysUtils.StrToDateDef
  • System.SysUtils.TryStrToDate
  • System.SysUtils.StrToDateTime
  • System.SysUtils.StrToDateTimeDef
  • System.SysUtils.TryStrToDateTime

How to fix it

Create a local TFormatSettings instance, configure it with the desired date formatting properties, and pass it in as the last parameter of the formatting routine.

procedure PrintCurrentTime;
begin
  Writeln(DateToStr(Now));
end;
procedure PrintCurrentTime;
var
  LocalFormatSettings: TFormatSettings;
begin
  LocalFormatSettings := TFormatSettings.Create;
  LocalFormatSettings.ShortDateFormat := 'dd/mm/yyyy';
  Writeln(DateToStr(Now), LocalFormatSettings);
end;

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy