1

概述

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 服务器上运行时的行为仍然相同(在这种情况下,以确保日期字符串的格式一致,其中 New Zealand = dd/mm/yyyy和美国 = mm/dd/yyyy ) 或世界其他任何地方。

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

注意-CultureSwitcher.SetTo(CultureInfo culture)CurrentCulture和设置CurrentUICulture为提供的指定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."
        }
    }
}
4

0 回答 0