DateTime formatting the .Net way

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
Console.WriteLine(Thread.CurrentThread.CurrentCulture.UseUserOverride.ToString());
// Expected : H:mm:ss, Actual : h:mm:ss
Console.WriteLine(Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern);
// Expected : H:mm:ss, Actual : h:mm:ss
Console.WriteLine(System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortTimePattern);
// Expected : 10/13/2006 13:31, Actual : 10/13/2006 01:31
Console.WriteLine(DateTime.Now.ToString("g"));
// Expected : 10/13/2006 13:31:28, Actual : 10/13/2006 13:31:28 !!!!!!
Console.WriteLine(DateTime.Now.ToString());
// Expected : 13:31, Actual : 01:31
Console.WriteLine(DateTime.Now.ToString("t"));
Console.ReadLine();

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 :

System.Globalization.DateTimeFormatInfo.CurrentInfo.FullDateTimePattern

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….”

Currently listening to : Autoharp – Hooverphonic ( The Magnificent Tree )
Del.icio.us tags: , , ,

Football – stop saying soccer

Ok this post is going to be a bit odd… it involves soccer !!

Not that I’m that big of a fan of the game, although I played it myself for most of my current life, but this little thing just has me hooked.

There is a social network game online, called Hattrick and it is created around the most popular sport in the world soccer. The objective is quit straightforward : manage your own team !

The setup is simple, first you create a free account on Hattrick and after a day you get a team assigned ! You’ll be placed in a small competition and will be able to play against the other teams inside your competition.

Sounds easy and it is, but what strikes my is the level of detail you have at your disposal to manage all kinds of settings ! A small list of things to do :

  • Assign a coach
  • Choose training schemas
  • Hire other staff ( like a doctor )
  • Buy and sell players at a special eBay like trading place
  • Manage the players on the field, choose the right positions and follow their progress
  • Choose different setup formats for each league game
  • Expand your home stadium

And the list just goes on and on ! After reading this you’ll think it to be overwhelming and impossible to manage, but it really is just easy…

What I find even more extraordinary is the fact that everything just runs very smooth and seems to be programmed with the old ASP technology ! You can even follow the games in real time, through text messaging !

Damn I wish I could create something like that !!

Currently listening to: Stereo MC’sWe Belong in This World Together
Del.icio.us tags: , , ,

Attempting recognition

Why do firms, companies or other organisations create a website ? Most of them do not provided webbased services, so a direct ‘client – company’ connection can’t be made ! But still each and everyone of them would love to be ‘online’…

So why are they so eager to commit a part of there communication budget to the internet ? Well mainly to get a face, to get some recognition for the small part they ( could ) play in our day to day life !

Does it work ? Most of the time, I would think that it does… a good visual campaign helps people decide if they should apply their money to the companies service or trade ! I do most of my ‘window shopping’ online, so it makes a difference !

But it isn’t always easy to set up the correct architecture to have the rigth impact ! There is that ‘budget’ factor that has to be incorporated, it draws an invisible wall and sets the maximum reach.

Let me illustrate with a real life example, that involves my own little me 😉 !

I’m a proud member of the WingTsjun organistation, headquarters based in Germany, but with several schools around the world !
So assume you would be looking to have a go at this type of martial art, what would you do ? Well, a good guess would be to search the internet to find a school near to your own current location !

So you would most likely be expecting following thing :

  • A site for your own country ( if possible translated )
  • An overview of all schools ( sorted per region ) for the given country
  • A good information page of the current training hours and location
  • Overall information regarding the style, the teachers, the school infrastructure, etc…

But creating these items, involves manual labour, money and time ! Our current solution offers most of the expected items, but there is still room for improvement ( remember that invisible wall 😉 )…

We currently provide a specific translated site for each country, but they are not home based ! Meaning, not a domain URL for each country… let me be frank here, when I really want to find a website about some firm I need to locate, I use my country specific Google search engine !! So a country specific domain name would make sense !

But does this mean we don’t target the right people now ? No, that isn’t the problem… but the audiance could get bigger by changing some of the current architecture !
Does it mean we have a wrong architecture now ? No, we just have a different approach and use a central starting point, hereby creating a feeling of ‘one’ ! Coming out and saying, ‘we are a team’, not seperate groups…

So sometimes the corporate slogan could have an impact on the outline of the architectural design.

Currently it just fascinates me how I can look at the current end result and wonder why some choices were made and what could change… it just helps me decide for future projects !

Disable Theming and StyleSheet theme for one specific page in Asp.Net 2.0

I recently started using the new Theming features of asp.net 2.0 and I have to say it works like a charm !
Just putting one line inside the Web.Config file to enable a theme and change the look of the complete website is just fabulous…

But what to do when you want to disable everything for one page ? I searched the internet for a solution but found none ( maybe I didn’t look very good ? 😉 ) !

I know there are 2 page directives that help setting the themes, but to disable them I only found one !

<%@ Page Language="C#" Theme="OrangeTheme" StyleSheetTheme="GreenTheme" %>
<%@ Page Language="C#" EnableTheming="false" %>

But the EnableTheming will only effect the choosen theme and will not effect the StyleSheet theme !!

Even our friend the intellisence didn’t offer me any clues… When you type Theme=”, it will show you a list of available themes, so you can choose one from it.

So I was stuck and needed this feature for the default.aspx page of a small website I was building. But like many other solutions, this one also came by accident !

When I was trying every combination of theme and stylesheet, I just ( for the fun of it ) entered 2 empty string sequences, to see what the result would be… And tadaaaaa, what did I get, a default.aspx page ‘without a theme and stylesheet theme’ !!!

So people when you want NO theme and NO stylesheet theme just use following code :

<%@ Page Language="C#" Theme="" StyleSheetTheme="" %>