Link to home
Start Free TrialLog in
Avatar of Lee_Nover
Lee_Nover

asked on

StrToDate and ShortDateFormat

running a service on win2k sp4
sometimes ShortDateFormat is OK (in my case it's dd.mm.yyyy)
but very often the format is different .. like 'ddd mm yyyy' or whatever it is
ofcourse in the later case StrToDate raises an exception

where is this format read from ? every user can have different locale settings .. where's the setting for SYSTEM or computer global setting
I really don't want to set ShortDateFormat everytime before I call DateToStr nor use the overloaded version with FormatSettings
actually the only way I could solve this issue was with the overlaoded version with FS (in one project)
but the same problem will arise in all other projects .. which are much larger

sooo .. where can I set these global globale settings ?
and it's really weird .. some threads run in the right LCID and some don't .. I have no idea what affects this
Avatar of Lee_Nover
Lee_Nover

ASKER

also while remote debugging I don't get these weird format settings
looks like I get the current user's formatsettings .. and no, the service isn't interactive :)
I normally keep the setting in a variable, then change it to something I know how to handle, and reset it when I'm done. This way you won't have to retrieve the locale/user date setting.
Hi,

I had a similar problem with TDateTimePicker component on every new form when the process was running under another users account. ShortDateFormat was correct but the date format in that component was different. I solved it another way but you may check HKEY_USERS\.DEFAULT\Control Panel\International\sShortDate, change its value to dd.mm.yyyy and try again. I haven't tried that myself, it's just a guess ;-)

Regards, Geo
pritaeas: yes well .. already did that but I can't control vcl methods .. like TParam.AsString which calls StrToSQLTimeStamp .. don't know when it will be called
I thought of monitoring the change of ShortDateFormat or some hack around it .. no pretty solutions yet :/

geobul: did that and am waiting for results
Project / View Source

add SysUtils to uses clause and after begin try this:

  ShortDateFormat := 'dd.mm.yyyy';
  Application.UpdateFormatSettings := False;
tnx, but there's no UpdateFormatSettings or anything similar in a ServiceApp
use:

//........
initialization
  ShortDateFormat := 'DD.MM.YYYY';

end.
esbg .. just .. forget it .. ok ..
L_N o.k.
Avatar of Russell Libby

Lee,

If you set the ShortDateFormat ONCE then you should be fine in your service application. This value (and a few others) gets loaded via a call to GetFormatSettings, which is made in only 2 places:

1.) in the initialization of the SysUtils unit
2.) can also be made in the WM_WININICHANGE handling of TApplication (which is where UpdateFormatSettings is used, if false then GetFormatSettings is not called). This one does not apply to your situation.

Basically, the only place it is getting set is in the init of SysUtils, so setting it once should be suffecient.

Regarding getting the system date setting, you might try the following calls to see if any return what you are expecting:

  // Get the short date format for Thread, User, and System locale, both with and without the user override
  MessageBox(0, PChar(GetLocaleString(GetThreadLocale, LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);
  MessageBox(0, PChar(GetLocaleString(GetThreadLocale, LOCALE_NOUSEROVERRIDE or LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);
  MessageBox(0, PChar(GetLocaleString(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);
  MessageBox(0, PChar(GetLocaleString(LOCALE_USER_DEFAULT, LOCALE_NOUSEROVERRIDE or LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);
  MessageBox(0, PChar(GetLocaleString(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);
  MessageBox(0, PChar(GetLocaleString(LOCALE_SYSTEM_DEFAULT, LOCALE_NOUSEROVERRIDE or LOCALE_SSHORTDATE)), nil, MB_OK or MB_SERVICE_NOTIFICATION);


function GetLocaleString(Locale: LCID; LCType: DWORD): String;
var  lpBuffer:   Array [0..1023] of Char;
     dwReturn:   Integer;
begin

  // Get the locale info
  dwReturn:=GetLocaleInfo(Locale, LCType, lpBuffer, SizeOf(lpBuffer));

  // Check return value
  if (dwReturn = 0) then
     // Raise exception using last error
     RaiseLastWin32Error
  else
  begin
     // Null terminate
     lpBuffer[dwReturn]:=#0;
     // Return the string
     result:=StrPas(@lpBuffer);
  end;

end;


----------

Hope this helps,
Russell

Aaa, yep. Sorry, I forgot you are writting a service...

There were some API functions ... GetLocaleInfo and SetLocaleInfo I think. There could get/set the date format system-wide. You have to choose between system-wide settings of hard-coded date format. Sorry I can not provide you with example right now... later I hope.
rllibby, good point...
I already tried setting ShortDateFormat:='dd.mm.yyyy'; everytime before StrToDate was called  .. so that should be ok .. guess what :)
I have no clue where or what sets it to a different format
I know how to get those settings .. I was asking from where the service loads the format because it wasn't always the same
I'll dive deep into it later ..
in SysUtils.pas, procedure GetFormatSettings, the line is:
ShortDateFormat := TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SSHORTDATE, 'm/d/yy'));
:)
not where in code .. but from where in the system .. rllibby answered that ..
now I just want to know in what order these go

btw .. the problem might be dbExpress related
currently I'm not in the mood to play with this :)
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tnx ... exactly what I thought is going on (somebody logged on or not)
dbExpress driver might be setting the thread locale to something it prefers .. dunno .. just a speculation .. because:

ShortDateFormat:='dd.MM.yyyy';
ClientDataSet.Filter:=Format('OBDELAJ_DELAVEC = %d AND OBDELAJ_DATUM = %s', [ADelavec, QuotedStr(VarToStr(VarSQLTimeStampCreate(ADatum)))]);
believe it or not this sometimes fails .. and there are no parameters for Filter
(sqltimestamp and all those conversions are for testing .. any method that calls StrToDate failed !)
ShortDateFormat is set just before the filter is set ! I even locked ShortDateFormat with a CS .. so it must be something internal to dbExpress


so there's actually no clean solution to this .. will hack something up :)
wasn't really a solution but a complete explanation .. tnx
btw: haven't delt with this problem for a while now (project on the side)