?
Solved

StrToDate and ShortDateFormat

Posted on 2004-10-19
18
Medium Priority
?
4,168 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Lee_Nover
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
  • +3
18 Comments
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12345518
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 :)
0
 
LVL 6

Expert Comment

by:pritaeas
ID: 12345593
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12345624
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:Lee_Nover
ID: 12345753
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
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12345856
Project / View Source

add SysUtils to uses clause and after begin try this:

  ShortDateFormat := 'dd.mm.yyyy';
  Application.UpdateFormatSettings := False;
0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12345941
tnx, but there's no UpdateFormatSettings or anything similar in a ServiceApp
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12346828
use:

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

end.
0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12346907
esbg .. just .. forget it .. ok ..
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12347286
L_N o.k.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 12347341

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

0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12347371
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.
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12347382
rllibby, good point...
0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12347600
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 ..
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 12347802
in SysUtils.pas, procedure GetFormatSettings, the line is:
ShortDateFormat := TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SSHORTDATE, 'm/d/yy'));
0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12348039
:)
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 :)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 12348285
This may help explain things a little further....

quoted from:
http://www.aimtec.com.au/articles/ItsAboutTime/Default.htm#Special%20problems%20for%20server%20applications


Special problems for server applications

Delphi applications run with the locale that is the default for the currently logged in user. On NT Server this means that if somebody is logged on you will get their Regional Settings instead of the system defaults, but if nobody is logged on then the system default will be used.

For interactive applications this isn’t a problem since the same person will be logged on for the duration of the application. The problems arise with service applications like CGI scripts, where the behaviour can differ depending on whether somebody is logged in or not. Australian servers are usually set up with a system default locale of "English (Australian)" but it’s not uncommon for users to have the default locale of "English (United States)".

The problem is further compounded by the fact that if there is no logged in user the thread locale will default to the US settings, even if the system default locale is something else.

The following code at the start of your server application will help:

SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
SYSUTILS.GetFormatSettings;
Application.UpdateFormatSettings := False;  // ** would not apply to TServiceApplication

This will set ShortDateFormat and the other locale dependent variables in SysUtils to reflect the system default settings, regardless of who is currently logged in. Note: it’s important to set Application.UpdateFormatSettings to False or the thread locale will be reset to LOCALE_USER_DEFAULT if the user updates the Regional Settings in the Control Panel.

Unfortunately this has no effect on VarToDateTime and VarFromDateTime which like all OLE variant conversions in the VCL are hard coded to use LOCALE_USER_DEFAULT. To do a variant conversion using the system defaults you will need to directly call VariantChangeTypeEx Windows API passing it LOCALE_SYSTEM_DEFAULT instead. Note: The word from Borland is that this is a bug and that the intention is that the variant conversions should follow the thread locale. This will probably be fixed in the next version of Delphi.

-----

Regarding dbExpress, I can't say what affect it may (may not) have

Russell

0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 12348430
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 :)
0
 
LVL 12

Author Comment

by:Lee_Nover
ID: 13293253
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)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question