.NET (3.5) formats times using dots instead of colons as TimeSeparator for it-IT culture?
Asked Answered
S

4

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.?

Shiner answered 21/9, 2011 at 10:9 Comment(0)
R
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.

Rawalpindi answered 22/9, 2011 at 9:48 Comment(0)
M
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");
Marek answered 22/9, 2011 at 7:54 Comment(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! :/Shiner
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.Marek
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)Shiner
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 :(Shiner
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?Shiner
B
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.

Badly answered 21/9, 2011 at 10:11 Comment(4)
@DannyTuppeny - Not unheard of.Badly
@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.Gain
@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.Gain
See answer from @Dario Solera - apparently this is not the case, and in Italy they do use colons! :(Shiner
R
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.

Rawalpindi answered 22/9, 2011 at 9:48 Comment(0)
G
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);
Gain answered 22/9, 2011 at 8:31 Comment(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 :(Shiner
@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.Gain
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 :)Shiner
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!Shiner
@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.Gain
Our native culture (en-GB) also uses colons, but we see dots! Do you see this .NET 3.5 as well as 4?Shiner
Looks like this is a 3.5 issue - just confirmed that 4 uses colons, so I'm going to try and accelerate our upgrade to .NET 4!Shiner

© 2022 - 2024 — McMap. All rights reserved.