Thread safe variables for date and time formatting

by Radek Červinka 26. November 2010 22:15

In Delphi XE are changes in RTL about formatting time and date. In old versions of Delphi RTL there are globals variables.

New approach is backward compatible, but can be used as thread safe for new programs.

When converting from old code we get warning about deprecated code, but our code can be compiled and with small change deprecated warning can be removed.

The basic is extended record TFormatSettings, with global variable FormatSettings. Items of this record are exactly in same order as formatting variabled from previous version of Delphi (like DecimalSeparator, ShortDateFormat and so). This record is with absolute directive mapped to old variables (markered as deprecated, when used deprecated warning is showed). This "absolute" directive warrants that modifications of old variables updates new record and vice versa because they shared same memory. Nice trick Embarcadero!

[DCC Warning] Project1.dpr(14): W1000 Symbol 'DecimalSeparator' 
 is deprecated: 'Use FormatSettings.DecimalSeparator'

For remove deprecated warnign (warning - this is not enough for thread safety) simple replace DecimalSeparator with FormatSettings.DecimalSeparator (as hinted warning).

OK, but why?

Every function like FloatToStr, DateTimeToStr get brother, where second parameter is TFormatSettings and in this case passed data are used instead of global variables (global variables can be modified from every place, BTW RTL this sometimes refresh - to disable use Application.UpdateFormatSettings).

But we can use own format settings record, set to own values, save and use later without any risc of modification.

For example classic code for formatting real number always with . (for some people: some localization use , or something else and is good idea to have store data in know format).

    1program Project1;
    2{$APPTYPE CONSOLE}
    3uses
    4  SysUtils;
    5var
    6  f: double;
    7  s: string;
    8  fs: TFormatSettings;
    9  c: char;
   10begin
   11  c := DecimalSeparator; // save old separator
   12  try
   13    DecimalSeparator := '.';
   14    f := StrToFloat('10.1');
   15//    [DCC Warning] Project1.dpr(14): W1000 Symbol 'DecimalSeparator' is 
   16// deprecated: 'Use FormatSettings.DecimalSeparator'
   17
   18    FormatSettings.DecimalSeparator := '.'; //OK without warning, but..
   19    f := StrToFloat('10.1');
   20  finally
   21    DecimalSeparator := c; //restore separator
   22  end;
   23// ***********************************
   24  //Thread safe currency and date/time formatting
   25  // fs is record, can be stored for future use
   26  fs := fs.Create;  
   27  // Create obtain data from system
   28  writeln('DecimalSeparator ', fs.DecimalSeparator);
   29  fs.DecimalSeparator := '.'; // set own separator
   30  f := StrToFloat('10.1', fs); //and use this, and maybe later
   31end.

Tags: , ,

Delphi news

Comments

11/26/2010 11:54:03 PM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Delphi.cz | Thread safe promÄ›nnĂ© pro formátovánĂ­ ÄŤasu a oddÄ›lovaÄŤe
        [delphi.cz]
        on Topsy.com

topsy.com |

11/28/2010 4:15:31 PM #

Radim

Prikazom:

FormatSettings.DecimalSeparator := '.';

prepisujem systemove nastavenie, ktore by sa inak aplikovalo? Nap.r mohla by to byt aj - , /ciarka/?

Radim |

11/28/2010 9:13:15 PM #

radekc

Prepisuji nastavení načtené ze systému při startu programu. Pro naše Windows je to , tj. čárka, ale pokud někde něco takového ukládám tak používám svůj formát tj. s tečkou.

Nejsem si jist zda moc rozumím tvému dotazu.

radekc |

11/30/2010 5:57:47 PM #

Radim

Nie je horsie ney niekto napise program na anglickych Windows s cislami s . a potom kontroluje vstupne udaje regionalne s , . Stretol som sa s par /free/ programkami, kde po starte pred spustenim bolo potrebne rucne prepisat vsetky bodky na ciarky alebo naopak. Uff, uz ziadny taky nastastie nemam.

Radim |

12/1/2010 10:11:38 AM #

JaroB

Chápu to dobře, že nastavení v Sysutils a FormatSettings je synchronizováno?

JaroB |

12/1/2010 10:37:11 AM #

radekc

Oboje je v SysUtils

  FormatSettings: TFormatSettings absolute CurrencyString;

kde CurrencyString je prvni promennou z bloku promennych, takze diky absolute je to proste prepisovane, at menite to a nebo to.


var
  // Important: Do not change the order of these declarations, they must
  // match the declaration order of the fields in TFormatSettings exactly!
  CurrencyString: string deprecated 'Use FormatSettings.CurrencyString';
  CurrencyFormat: Byte deprecated 'Use FormatSettings.CurrencyFormat';
  CurrencyDecimals: Byte deprecated 'Use FormatSettings.CurrencyDecimals';
  DateSeparator: Char deprecated 'Use FormatSettings.DateSeparator';
  TimeSeparator: Char deprecated 'Use FormatSettings.TimeSeparator';
  ListSeparator: Char deprecated 'Use FormatSettings.ListSeparator';

radekc |

Comments are closed