Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

delphi - ShortDateFormat vs FormatSettings.ShortDateFormat?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...