Back in the 'old' days when we where using Visual Studio 6 and made our first Visual Basic programs, we had to rely on 'good luck' and 'plumbing' to get some tasks done !
What I mean to say is, that sometimes the software would have a mind of his own ! Some code just didn't perform in the correct expected manner. And this wasn't something you actually could have avoided, no the software had some flaws !
But with the arrival of Visual Studio .Net and the new programming languages like C#, this seemed a thing of the past ! Until now that is ! Because I have a small code example I would like to show you, that would seems very straightforward, but has me 'bugged' whole week.
The thing I'm trying to do, is cast a normal DateTime.Now to a Time format, but depending on the current user settings ( in the regional settings ). What would seem like an obvious thing to do... So here we go with our little experiment !
- First set your regional settings correct : English ( US )
- Then go to Customize and in the Time pane change the time to display using the 24 hours notation ( and loose the AM & PM )
- Create a small console application and paste following code ( we are talking Visual Studio 2005 here )
// Expected : True, Actual : True
// Expected : H:mm:ss, Actual : h:mm:ss
// Expected : H:mm:ss, Actual : h:mm:ss
// Expected : 10/13/2006 13:31, Actual : 10/13/2006 01:31
// Expected : 10/13/2006 13:31:28, Actual : 10/13/2006 13:31:28 !!!!!!
// Expected : 13:31, Actual : 01:31
So as you can see, the DateTime.Now.ToString("t") option will NOT take into account the current user settings, found on the regional settings pane ! This is not something I would expect... I would just like my code to be as little dependent as possible. So I WANT to use the default DateTime formatting options and not having to wander about what the user chooses. It must be up to the user to get the right format he wants, I just want to be able to tell the program to give me the Time and not the complete DateTime.
After this little experiment, I searched further for any information, but got back empty handed.
The only document on MSDN that says something significant about DateTime and the regional settings can be found here. But it just states the things I would expect in my test and they just aren't true.
After this set back I contacted Chris Sells Program Manager in the Distributed Systems Group at Microsoft, hoping maybe he could shed a little light on my issue ! To be honest this guy is amazing , knowing he's probably swamped with work, he still managed to reply me in a very fast time frame ( thanks Chris ) !
The first thing Chris noticed was that I could get the good user settings information through the following code :
But this wasn't helping me much... the real problem is not getting the info about the DateTime format, but displaying it correctly using the default formatters in .Net ! So this info is a bonus, yes, but still I'm stuck !!
So for now I use a small work around, that I would love to delete as soon as possible, by reading out the users settings of the regional settings through the HKEY_CURRENT_USER in the registry ( \Control Panel\International ).
Chris will still check his huge background information repository and hopefully come back with some good news. But if ANYBODY knows how I can solve this 'little' problem, please help me out !
UPDATE : well Chris got back to me on this problem... he submitted my question on some community and he got an answer from Michael Kaplan !
It seems he has answered this question before !! Take a look here for more info on the solution.
In short : "A lot of the confusion is due to the fact that the .NET Framework has two time formats (long and short) while Windows only has one time format (long) which can optionally be truncated in GetTimeFormat." And : "So the answer to the original customer inquiry, to wit: "The main issue is that I don't understand why/when .Net uses the user overrides of the Regional Settings" is that only one of the settings in .NET directly relates to the one in Windows, even though they both look like they ought to...."