Solved

StrToDate and ShortDateFormat

Posted on 2004-10-19
18
4,129 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
  • 8
  • 4
  • 2
  • +3
18 Comments
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
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
Comment Utility
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
Comment Utility
tnx, but there's no UpdateFormatSettings or anything similar in a ServiceApp
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
use:

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

end.
0
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
esbg .. just .. forget it .. ok ..
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
L_N o.k.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

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
Comment Utility
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
Comment Utility
rllibby, good point...
0
 
LVL 12

Author Comment

by:Lee_Nover
Comment Utility
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
Comment Utility
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
Comment Utility
:)
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Save pdf file to other location 3 72
Convert Jpg /PNG To GIF 5 102
Delphi : could not find program, '...exe' 2 115
Working with hours 3 31
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now