Jump to content
Ian Branch

Setting & Usage of TFormatSettings..

Recommended Posts

Hi Guys,

D11.3.1.

This is what I understand att.  Feel free to correct me if my understanding is incorrect.
1.  If I set FormatSettings with a Country code, i.e. "var FormatSettings := TFormatSettings.Create('en-AU');", then that action will configure the various TFormatSettings properties in SysUtils to the Australian formats.
2.  I can then forget about the variable FormatSettings.
3.  Having set these properties in SysUtils, whatever Unit has SysUtils will have/use the set properties.
4.  Any function/procedure that uses a FormatSettings property will now use the relevant set TFormatSettings property according to the Country/Region code employed in the '.Create()'.

Are my understandings correct?

Regards & TIA,
Ian

Share this post


Link to post

FormatSettings is a "record" with global definition, then if any unit change it, you'll have its effect.

but it's always prudent use a local definition to avoid any problem! mainly on thread, because it's not thread-safe!

 

all internal definitions came from MSWindows/OS, then yes, Create() just use it. you can have how many you needs!

 

Call GetLocaleFormatSettings to populate the TFormatSettings variable with locale information. 

 

TApplication.UpdateFormatSettings:

Specifies whether format settings are updated automatically when the user alters the system configuration. Use UpdateFormatSettings to control automatic updating of format settings. The default of true is set in the constructor. Using the default format settings is recommended. These settings are initialized to the Windows local.

Edited by programmerdelphi2k

Share this post


Link to post
6 hours ago, Ian Branch said:

1.  If I set FormatSettings with a Country code, i.e. "var FormatSettings := TFormatSettings.Create('en-AU');", then that action will configure the various TFormatSettings properties in SysUtils to the Australian formats.

 

 

No. If you say var FormatSettings ... you will declare new FormatSettingsVariable in the scope where you have written that code, that will have nothing to do with global FormatSettings variable. If you want to change the value of global FormatSettings variable you need to write FormatSettings := TFormatSettings.Create('en-AU')

 

However, changing global FormatSettings variable is not advisable for several reasons:

 

1. FormatSettinsg is populated with user's locale. You should honor the user settings and present data to the user in format user has specified, not the one you think it should be.

2. If user changes locale settings while your application is running, those new settings will be applied to the global FormatSettings variable if the Application.UpdateFormatSettings is True (which it is by default)

3. FormatSettings beging a global variable are not thread-safe and if any other code changes those settings (and there has been such code out in the wild) and code running in threads that uses the global will be affected. Using formatting and parsing routines that use FormatSettings passed as additional parameter is the best choice for avoiding thread-safety and other issues as you will have the control over which settings are being used.

4. And the most important aspect, if you have code that relies on very specific values in FormatSettings, like serialization, should not depend on global FormatSettings variable but should have its own more scoped (and therefore more protected) TFormatSettings variable that will be set once and never change during the application lifetime. Using global FormatSettings for working with data that needs to be in some specific format is the fastest way to shoot yourself in the foot.

 

6 hours ago, Ian Branch said:

2.  I can then forget about the variable FormatSettings.

No, you should not forget about it for reasons I specified above.

6 hours ago, Ian Branch said:

3.  Having set these properties in SysUtils, whatever Unit has SysUtils will have/use the set properties.

Any formatting and parsing function that uses global FormatSettings variable will be affected by the change in global FormatSettings whether you change it or some other code. 

6 hours ago, Ian Branch said:

4.  Any function/procedure that uses a FormatSettings property will now use the relevant set TFormatSettings property according to the Country/Region code employed in the '.Create()'.

Yes. If you use functions that rely on global variable, they will use whatever value at the time is in that global variable.

Share this post


Link to post

Hi Dalija,

Thank you for your input.  Appreciated.

Have you ever worked with Pacific Islanders?

I have been dealing with them for the past 7 years and they can be extremely frustrating.

They will do things with their PC just because.  Including changing their date & time formats. and in a couple of cases their Region.

My Customer wants all the Apps to operate to a standard format, interestingly, to en-AU, despite what the User may have done with his/her PC.

I thought this would be a good way to enforce it.

 

Regards,

Ian

Share this post


Link to post
52 minutes ago, Ian Branch said:

My Customer wants all the Apps to operate to a standard format, interestingly, to en-AU, despite what the User may have done with his/her PC.

If that is the requirement, then, yes, setting global FormatSettings variable will have that effect. You also need to change Application.UpdateFormatSettings to false, before you change global FormatSettings to avoid user changes to be applied if the application is already running.

 

You also need to keep in mind that any code that does not use global FormatSettings will not be affected by changes in global variable. For instance Delphi JSON serialization uses fixed settings for serialization that are based on JSON format requirements. Any code that uses own settings will behave in similar way.

 

I would still suggest that for serialization purpose and data persistence, you use separate FormatSettings variable as if (and when) specification changes and users are allowed to have their preferer format used for viewing purposes, it will be very hard to make separation afterwards and find all the places that need to use specific fixed format.

  • Like 1

Share this post


Link to post
10 hours ago, Dalija Prasnikar said:

before you change global FormatSettings to avoid user changes to be applied if the application is already running.

Ahhh.  Tks for the tip.

10 hours ago, Dalija Prasnikar said:

Any code that uses own settings will behave in similar way.

That was always a/the risk.

10 hours ago, Dalija Prasnikar said:

users are allowed to have their preferer format used for viewing purposes,

Won't happen.

 

Tks to all for your inputs and guidance.

 

Regards,

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×