I have run into an issue that is probably due to my mis-understanding of how the DateTime.ToShortTimeString() method works. When formatting time strings with this function,
Answer to your second question is
DateTimeFormat.Format(DateTime.Now, "t", CultureInfo.CurrentUICulture);
or
DateTime.Now.ToString("t", CultureInfo.CurrentUICulture);
It’s actually always better to use explicit methods that accept CultureInfo. There is no consistency how .Net chooses what to use by default either CurrentCulture or CurrentUICulture or InvarinatCulture.
To make answer full. Also I will outline differences between cultures.
So CurrentCulture is "Control Panel -> Clock, Language and Region -> Region and Language -> Formats Tab." This is culture that you expect your calculations to be. For example you can do your accounting in US, so you have this to be configured in US.
CurrentUICulture is "Region and Language -> Display language", means that when you are emigrant from Ukraine, you want your application to be localized in UA (but all calculations is still in US).
And InvariantCulture is so called culture agnostic locale. You should use this for storing information and so on. Effectively it En-US.
Note: I could be wrong where each setting is located in windows. But you probably got an idea.
I'm quite sure the fact that the short time format string isn't used in DateTime.ToShortTimeString() or DateTime.ToString("t") is a bug, because it was fixed in .NET framework 4.0.
In answer to each of my questions:
1) What is my misunderstanding of .NET and Windows Formats?
The short answer is, there is no link between the "Short Time" setting in "Regional and Language" settings and .NET's ShortTimePattern property. However the LongTimePattern property is dictated by the "Long Time" setting.
I adapted the above method replacing the two formatting lines to:
string sixAmString = sixAm.ToString("T", culture.DateTimeFormat);
string sixPmString = sixPm.ToString("T", culture.DateTimeFormat);
Here is the output:
Culture: en-GB, 6AM: 06:00:00, 6PM: 18:00:00 // HH:mm:ss Culture: en-GB, 6AM: 06:00:00 AM, 6PM: 06:00:00 PM //hh:mm:ss tt
The bottom of this article explained the problem to me.
2) What is the best solution to create a short format time string (HH:mm or hh:mm tt) based upon the operating system setting?
I don't know about the best solution, but I created the following function that converts the LongTimeFormat to a ShortTimeFormat thus allowing an application to follow the users option if they change the "Long Time" (albeit it won't track the "Short Time" setting).
static string GetShortTimeString(DateTime ShortTimeString)
{
DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
string ShortTimePattern = dateTimeFormat.LongTimePattern.Replace(":ss", String.Empty);
ShortTimePattern = ShortTimePattern.Replace(":s", String.Empty);
return ShortTimeString.ToString(ShortTimePattern);
}
The output after making the above changes:
Culture: en-GB, 6AM: 06:00, 6PM: 18:00 Culture: en-GB, 6AM: 06:00 AM, 6PM: 06:00 PM
The P/Invoke option is to use GetTimeFormat passing the TIME_NOSECONDS using DateTime.ToString(Format) as above. I have not tested this as I would prefer to avoid using P/Invoke.