本地计算机和服务器上的相同文化设置会生成不同的AMD签名者

时间:2016-11-28 02:18:07

标签: c# datetime cultureinfo culture uiculture

概述

在本地计算机和服务器上都进行了一致Culture设置,但它们生成了格式不同的DateTime字符串:

local:  "22/11/2016 12:00:00 AM"
server: "22/11/2016 12:00:00 a.m."

上下文

  • 语言:C#6
  • 平台:.NET 4.6.1
  • 本地操作系统:Windows 10 Pro
  • 服务器操作系统:Windows Server 2012 R2,版本6.3

背景

我正在编写一个单元测试,无论测试运行在哪里,我都希望表现完全相同。为此,我创建了一个自定义属性,以便在测试期间手动设置机器的当前CultureInfo。这样,如果我在新西兰本地运行测试,它在美国的AWS服务器上运行时仍然会表现相同(在这种情况下,为了确保日期字符串的一致格式,新西兰= dd / mm / yyyy 和美国= mm / dd / yyyy )或世界上任何其他地方。

到目前为止,自定义属性正确设置了机器的文化设置,并正确格式化了新西兰( dd / mm / yyyy )和美国之间的日期和时间( mm / dd / yyyy ),除了我的本地机器输出“AM”后,服务器输出“am” (参见下面的代码,这是AMDesignator类的DateTimeFormat属性)。似乎服务器正在从指定的CultureInfo之外获取格式设置,为什么会发生这种情况?我错过了什么吗?

注意 - CultureSwitcher.SetTo(CultureInfo culture)CurrentCultureCurrentUICulture设置为所提供的指定CultureInfo

结果(本地服务器):

using (var poly = PAssert.Poly())
{
    var date = new DateTime(2016, 11, 22);

    // New Zealand culture
    using (CultureSwitcher.SetTo(new CultureInfo("en-NZ")))
    {
        var currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
        var amDesignator = currentCulture.DateTimeFormat.AMDesignator;
        var currencySymbol = currentCulture.NumberFormat.CurrencySymbol;

        poly.IsTrue(() => System.Threading.Thread.CurrentThread.CurrentCulture.ToString() == "en-NZ");      // True | True
        poly.IsTrue(() => System.Threading.Thread.CurrentThread.CurrentUICulture.ToString() == "en-NZ");    // True | True
        poly.IsTrue(() => CultureInfo.CurrentCulture.ToString() == "en-NZ");                                // True | True
        poly.IsTrue(() => CultureInfo.CurrentUICulture.ToString() == "en-NZ");                              // True | True
        poly.IsTrue(() => amDesignator == "AM");                                                            // True | FALSE - expected "a.m."
        poly.IsTrue(() => date.ToString() == "22/11/2016 12:00:00 AM");                                     // True | FALSE - expected "22/11/2016 12:00:00 a.m."
    }
    // United States culture
    using (CultureSwitcher.SetTo(new CultureInfo("en-US")))
    {
        var culture = CultureInfo.CreateSpecificCulture("en-US");
        var amDesignator = culture.DateTimeFormat.AMDesignator;
        var currencySymbol = culture.NumberFormat.CurrencySymbol;

        poly.IsTrue(() => System.Threading.Thread.CurrentThread.CurrentCulture.ToString() == "en-US");      // True | True
        poly.IsTrue(() => System.Threading.Thread.CurrentThread.CurrentUICulture.ToString() == "en-US");    // True | True
        poly.IsTrue(() => CultureInfo.CurrentCulture.ToString() == "en-US");                                // True | True
        poly.IsTrue(() => CultureInfo.CurrentUICulture.ToString() == "en-US");                              // True | True
        poly.IsTrue(() => amDesignator == "AM");                                                            // True | True
        poly.IsTrue(() => date.ToString() == "11/22/2016 12:00:00 AM");                                     // True | True
    }
}

自己尝试(结果=本地|服务器):

using System;
using System.Globalization;

namespace ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            // New Zealand culture
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-NZ");
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-NZ");

            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator);                  // "AM" | "a.m."
            Console.WriteLine(CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat.AMDesignator);  // "AM" | "AM"
            Console.WriteLine(CultureInfo.CreateSpecificCulture("en-NZ").DateTimeFormat.AMDesignator);  // "AM" | "a.m."

            // United States culture
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");

            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.AMDesignator);                  // "AM" | "AM"
            Console.WriteLine(CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat.AMDesignator);  // "AM" | "AM"
            Console.WriteLine(CultureInfo.CreateSpecificCulture("en-NZ").DateTimeFormat.AMDesignator);  // "AM" | "a.m."
        }
    }
}

0 个答案:

没有答案