American or Canadian C# Code? (2/28/2008)

 The worst thing that I can imagine is that the compiled code works properly on your development/staging environment but fails to work on production. The nightmare starts when you just remember that you don't have debugger on production. Here is what happened to me yesterday. I developed a web service for the Time-Off time calculation. The code was excluding Ontario statuary holidays. This service apparently without any external dependency was working beautifully on my development laptop, but failing with some strange error on the server.

Take a look at this code:
DateTime holiday = DateTime.Parse("28/02/2008");

Sounds ok and works well on my Canadian computer (hey, it is made in China but Windows regional options is set to English(Canada)). But the very same line of code fails to run on an American machine. Simply because the American version of this code should be like this:
DateTime holiday = DateTime.Parse("02/28/2008");

That's just the date format but what can you do in a compiled code for this? Aha, .NET Framework is the answer. If you read the time variable from a Calendar or a DateTimePicker then the control is smart enough to do that, but if you are reading a preformatted string or you are hard-coding the date in your application then this issue come up. Here is the right way of solving this problem:

using System;
using System.Globalization;

namespace DotNetKing
{
    class Program
    {
        static void Main(string[] args)
        {
            CultureInfo culture = new CultureInfo("en-CA");
            DateTime holiday = DateTime.Parse("28/02/2008", culture);
            Console.Write(holiday.ToString());
        }
    }
}

This way you not only hardcode the date but also hardcode the date culture, so your code behaves Canadian even on an American server!
Cheers
Alireza


By: Alireza  
I do agree with you Mustafa, but remember it's not an application. What I did was just making an XML web service function to tell another service, if the day is a holiday or not. No user interaction at all.
URL: http://www.dotnetking.com  
By: mostafa arafa  
Hi Ali,

i prefer to avoid this problem is to configure your site to be hosted under a selected culture.

for example,in your case,add this key in the web.config:
{i removed the starting and closing tags}

globalization culture="en-CA" uiCulture="en-US"
the benefits of doing that:
1)then you don't need to write in every single control the culture hard coded in your source files.
2) no need to pass and initiate a culture object in your datetime objects.

Hope this helps.
URL: http://moustafa-arafa.blogspot.com  
By: Alireza  
Jason
You are right, but the clinet wants a simple text file that they can enter the dates for each year, considering that these Stat holidays may change in the future. That's why the format bacame an issue. For example this year we had a new holiday in Canada called Family day and hopefully they add more holidays to Canadian Calender as well (lol).
Thanks for the comment.
Alireza
URL: http://www.dotnetking.com  
By: Jason Real  
Ali,

I would think that an even more appropriate method to correct this issue would be to have your application calculate the date that you want to use from epoch. This would prevent there an issue with current time settings or region selection.

Best,
Jason
URL: www.tinidian.com  
Your name:
Email:
URL:
Comments:
 

 -