ShortDateFormat vs FormatSettings.ShortDateFormat?

旧城冷巷雨未停 提交于 2020-01-13 19:48:06

问题


In trying to get Log4D to compile in XE4, I was seeing

[dcc32 Error] Log4D.pas(2139): E2003 Undeclared identifier: 'ShortDateFormat'

on this line:

SetOption(DateFormatOpt, ShortDateFormat); 

A bit of googling led me to the solution of changing ShortDateFormat to FormatSettings.ShortDateFormat, which led to the following compiling code on XE4:

SetOption(DateFormatOpt, FormatSettings.ShortDateFormat); 

However, I don't really understand why that fixes things why it's needed to specify FormatSettings since I already included SysUtils in my uses statement, and secondly, I'm not sure how to rewrite this line to continue to be backward compatible with the versions of Delphi this open source project already supports.

I suppose I could add an IFDEF around that parameter or line of code for whatever version of Delphi introduced FormatSettings - but I'm not even sure what version of Delphi that was, let alone whether that's a good or bad way to solve this problem.


回答1:


The global SysUtils.ShortDateFormat was finally removed in XE3, see Global Variables.

In modern Delphi versions, the global FormatSettings variable record is also not recommended to use. Main reason is that it is not thread safe (which the old global ShortDateFormat also suffered from). You should define your own TFormatSettings variable that is consistent throughout your scope.

This will also make your code backwards compatible.

However, the way to initialize your FormatSetting record varies between Delphi versions.

On older versions (D7+) use:

GetLocaleFormatSettings(GetThreadLocale, FormatSettings);

And in newer versions (XE+):

FormatSettings := TFormatSettings.Create(GetThreadLocale); // Or one of the overloads



回答2:


Based on my previous internet research (You can believe everything on the internet, can't you?), the old ShortDateFormat was deprecated started with Delphi XE. So, yes, you can IFDEF to solve the problem. That's what I do. However, according to Peter Johnson's DelphiDabbler Blog TFormatSettings was introduced in Delphi 7, but I cannot confirm that.




回答3:


To fix the compiler error:

  • download a newer version of jedi.inc from https://github.com/project-jedi/jedi/blob/master/jedi.inc
  • place the new jedi.inc in the Log4D source folder

and change line 2211 in Log4D.pas to

SetOption(DateFormatOpt, {$IFDEF DELPHIXE_UP}FormatSettings.{$ENDIF}ShortDateFormat);

(I have submitted some patch requests to the Log4D project admin)



来源:https://stackoverflow.com/questions/23707323/shortdateformat-vs-formatsettings-shortdateformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!