Question

StrToDate and ShortDateFormat

Asked by: Lee_Nover

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

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-10-19 at 02:04:17ID21173514
Tags

strtodate

Topic

Delphi Programming

Participating Experts
5
Points
500
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. TDateTime and StrToDate problem(Convertionerror)
    I got a problem with the TDateTime type !! It works fine with most Dateformats but When windows date is set to dd-mmm-yy (09-nov-98) the function StrToDate or DateToStr doesn't work !! I get an EConvertionError exception and a '09-nov-98' is not a valid date !! Is there anoth...
  2. k-ary tree
    A k-ary tree is a tree in which every internal node has exactly k children. How can we show that a k-ary tree with node n nodes has (n(k-1)+1)/k leaves and yes this is an assignment so please help me out with it. thanks
  3. multiway/n-ary tree
    I've been looking for any kind of API/class for a multiway/n-ary tree. So far in the books and websites, I've seen, the representation of a node consists of a pointer to the first child and a pointer to the next sibling, i.e.: struct TreeNode { TreeNode* firstChild; ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Lee_NoverPosted on 2004-10-19 at 02:06:33ID: 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 :)

 

by: pritaeasPosted on 2004-10-19 at 02:29:25ID: 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.

 

by: geobulPosted on 2004-10-19 at 02:36:31ID: 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

 

by: Lee_NoverPosted on 2004-10-19 at 03:16:20ID: 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

 

by: Ivanov_GPosted on 2004-10-19 at 03:50:46ID: 12345856

Project / View Source

add SysUtils to uses clause and after begin try this:

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

 

by: Lee_NoverPosted on 2004-10-19 at 04:14:35ID: 12345941

tnx, but there's no UpdateFormatSettings or anything similar in a ServiceApp

 

by: esoftbgPosted on 2004-10-19 at 06:20:21ID: 12346828

use:

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

end.

 

by: Lee_NoverPosted on 2004-10-19 at 06:28:22ID: 12346907

esbg .. just .. forget it .. ok ..

 

by: esoftbgPosted on 2004-10-19 at 07:00:41ID: 12347286

L_N o.k.

 

by: rllibbyPosted on 2004-10-19 at 07:05:27ID: 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

 

by: Ivanov_GPosted on 2004-10-19 at 07:08:51ID: 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.

 

by: Ivanov_GPosted on 2004-10-19 at 07:10:00ID: 12347382

rllibby, good point...

 

by: Lee_NoverPosted on 2004-10-19 at 07:28:26ID: 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 ..

 

by: Ivanov_GPosted on 2004-10-19 at 07:47:16ID: 12347802

in SysUtils.pas, procedure GetFormatSettings, the line is:
ShortDateFormat := TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SSHORTDATE, 'm/d/yy'));

 

by: Lee_NoverPosted on 2004-10-19 at 08:08:08ID: 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 :)

 

by: rllibbyPosted on 2004-10-19 at 08:28:32ID: 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

 

by: Lee_NoverPosted on 2004-10-19 at 08:40:51ID: 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 :)

 

by: Lee_NoverPosted on 2005-02-12 at 03:32:42ID: 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)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...