3

According to Wikipedia (and confirmed in an answer by Dario Solera), in Italy they format times using colons:

The 24-hour notation is used in writing with a colon as a separator. Example: 14:05. The minutes are written with two digits; the hour numbers can be written with or without leading zero.

However, running the following code seems to output dots:

using System.Globalization;

Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("it-IT");
// Outputs "11.08"
Console.WriteLine(DateTime.Now.ToShortTimeString());

// Outputs "."
Console.WriteLine(new CultureInfo("it-IT").DateTimeFormat.TimeSeparator);

Is this a framework bug? What's the best way to "fix" it? TimeSeparator is settable - should we just overwrite it before assigning to Thread.CurrentThread.CurrentCulture etc.?

4 Answers 4

6

I can guarantee in Italy we use colons to separate hour and minute digits, and we use the 24-hour format. Wikipedia is correct (at least this time).

Your problem is likely that you're not setting the Thread's UI culture. Something like this should work:

Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("it-IT");
5
  • Thanks for the info! I tried setting both the UICulture and the Culture, but the UICulture had no effect. This confuses things somewhat, as it seems the .NET Framework is wrong! :/ Sep 22, 2011 at 8:20
  • Dates and times are output correctly on my machine - perhaps it's fighting with the locale set for the OS in your case. Also, keep in mind that if you are in a ASP.NET application, you must override the culture at a very specific moment (InitializeCulture IIRC) during page's lifetime, otherwise the runtime will use the culture value provided by the browser to serve the entire request. Sep 22, 2011 at 8:31
  • If you run the code I posted (I updated it to include UICulture), do you get the same output, or colons? I'm seeing dots on both my local dev machine (Windows 7) and our servers (Win 2003 Server) Sep 22, 2011 at 8:32
  • Our default culture is UK, and also uses colons. We're definitely setting cultures in the right place, as our whole app works as intended (and if we didn't set it, it would presumably fall back to the UK default). I'm baffled why you see different behaviour to us :( Sep 22, 2011 at 8:46
  • Turns out this is a .NET 3.5 issue - in .NET 4 it returns colon! Not sure if this is a bug, or maybe Italy used dots sometime in the past? Sep 22, 2011 at 9:48
4

The hours/minutes separator (TimeSeparator) in Italy seems to be a ., not a :.

You are specifically formatting for the Italian culture, so it follows that this is what will be used.

In a DateTime format string, the : is a place holder for this separator - if the culture defines . or , or anything else as the separator, that's what will be substituted when formatting the DateTime with that culture.

4
  • @DannyTuppeny - Not unheard of.
    – Oded
    Sep 21, 2011 at 10:14
  • @Danny - It may also depend on the language packs installed and system configuration. E.g. if you have your Windows installation set to 'it-IT', go into regional settings and override the format there; .Net will reflect that change. Also CultureInfo implemented IFormatProvider, try the ToString overload that uses it. Sep 22, 2011 at 8:04
  • @Danny I'll throw together an answer quickly that will help you with this, but I won't answer this question exactly - so keep this answer accepted. Sep 22, 2011 at 8:27
  • See answer from @Dario Solera - apparently this is not the case, and in Italy they do use colons! :( Sep 22, 2011 at 8:32
3

This seems to be a .NET 3.5 issue. In .NET 4.0 the code you posted uses a colon as expected. Seems like a strange breaking change between the framework versions, but seems like upgrading to .NET 4 will solve the problem.

1

Following from the conversation under Oded's answer, this is probably what you should be using:

var culture = CultureInfo.GetCultureInfo("it-IT");
var stringValue = new TimeSpan(100, 100, 100, 100, 100).ToString(null, culture);
var timespan = TimeSpan.Parse(stringValue, culture);
// Another example
var culture = CultureInfo.GetCultureInfo("it-IT");
var stringValue = DateTime.Now.ToString(null, culture);
var dateTime = DateTime.Parse(stringValue, culture);
7
  • This is pretty much what we're doing, but we're using TimeSpans, and TimeSpan.Parse in 3.5 doesn't support different cultures :( The problem, however, is that I can't make my machine correctly output colons when our culture is set to it-IT :( Sep 22, 2011 at 8:38
  • @DannyTuppeny sounds like a .Net Framework bug. I had a quick look in ILSpy and in .Net 4.0 the Timespan parsing is 100% managed - you should be able to copy it out of ILSpy (System.Globalization.TimeSpanParse); or grab it from the Mono codebase. Sep 22, 2011 at 8:50
  • The documentation seems to suggest in 3.5 it's just not culture-aware (and doesn't support cultures), I don't think it's a bug, more an oversight :( Will have a look at how the 4.0 one works, thanks :) Sep 22, 2011 at 8:54
  • Oh, but it doesn't solve the issue of it-IT using dots :/ Can you try the code I posted in the question and see what results you see? We're fully patched here, not sure why we're seeing dots! Sep 22, 2011 at 8:54
  • @DannyTuppeny Nope, I am seeing colons. 104.05:41:40, so that's HH:MM:SS. But my native culture 'en-ZA' uses colons too. Sep 22, 2011 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.