Output of times (AM/PM) changed in Windows 10 when

2020-07-02 11:15发布

问题:

I recently upgraded to windows 10 - and I'm now seeing some rather unexpected changes in the output of a date when using the "tt" format specifier.

Here's some code that demonstrates the issue:

using System.IO;
using System;
using System.Globalization;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        var cultures = new string[] {null, "en-NZ", "en-US", "en-AU", "en-GB"};

            foreach (var culture in cultures) {
                if (culture != null) {
                    var c = CultureInfo.GetCultureInfo(culture);
                    System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = c;
                }

                DateTime dt = new DateTime(2015, 1, 2, 3, 4, 5, DateTimeKind.Utc);

                Console.WriteLine("selection: {0} CurrentThread.CurrentCulture.Name: {1} CurrentThread.CurrentUICulture.Name: {2}  Value: {3}",
                    culture ?? "ambient",
                    System.Threading.Thread.CurrentThread.CurrentCulture.Name,
                    System.Threading.Thread.CurrentThread.CurrentUICulture.Name,
                    dt.ToString("hhh:mm tt"));
            }
    }
}

The output in previous versions of windows was:

selection: ambient CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ Value: 03:04 a.m.
selection: en-NZ CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ Value: 03:04 a.m.
selection: en-US CurrentThread.CurrentCulture.Name: en-US CurrentThread.CurrentUICulture.Name: en-US Value: 03:04 AM
selection: en-AU CurrentThread.CurrentCulture.Name: en-AU CurrentThread.CurrentUICulture.Name: en-AU Value: 03:04 AM
selection: en-GB CurrentThread.CurrentCulture.Name: en-GB CurrentThread.CurrentUICulture.Name: en-GB Value: 03:04 am

And in windows 10:

selection: ambient (windows 10) CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-US  Value: 03:04 a.m.
selection: en-NZ CurrentThread.CurrentCulture.Name: en-NZ CurrentThread.CurrentUICulture.Name: en-NZ  Value: 03:04 AM
selection: en-US CurrentThread.CurrentCulture.Name: en-US CurrentThread.CurrentUICulture.Name: en-US  Value: 03:04 AM
selection: en-AU CurrentThread.CurrentCulture.Name: en-AU CurrentThread.CurrentUICulture.Name: en-AU  Value: 03:04 AM
selection: en-GB CurrentThread.CurrentCulture.Name: en-GB CurrentThread.CurrentUICulture.Name: en-GB  Value: 03:04 AM

In both cases this code was compile win Visual Studio 2013 targeting .Net Framework 4.5

Does anyone know why the behavior has changed - and why in windows 10 it appears setting any culture on a thread specifically changes the output of AM/PM to be formatted as "AM" / "PM" not what is normally outputted for that culture?

回答1:

You may want to look at "Culture Data Shouldn't Be Considered Stable" http://blogs.msdn.com/b/shawnste/archive/2005/04/05/405694.aspx

One bit is that we started leveraging CLDR (Common Locale Data Repository, http://cldr.unicode.org) for many locales to better align with the industry standards. You may have noticed that there are quite a few additional locales we now have data for.



回答2:

I always force to use ToString("G") when changing DateTime to string with ToString() function . Then I have no problems to parse the string to DateTime again. I tested it with my client application (gets date & time in XML in "G" format and parses it to DateTime) on Windows 7/8/10.



回答3:

I also found that the combination of 'en-nz' and Windows 10 upgrade suddenly change a.m./p.m.to AM/PM.

The difference is the code I maintained relaying on ToShortTimeString() output with a.m./p.m. but in Windows 10 it changes to AM/PM instead I spend ages trying to find the correct culture info/format string, and I am also trying to find the missing local setting on my new Windows 10 machine compare to my old Windows 7 machine. But it turns out that the same local setting on different version of Windows machine does indeed display a.m./p.m. AM/PM differently.

Also confirm that DateTimeFormatInfo.AMDesignator for 'en-nz' now output 'AM' instead of 'a.m.'