简体   繁体   中英

ShortDayPattern varies by build agent when specifying CultureInfo

I have the following function:


        public static string ShortMonthDayPattern(this DateTimeFormatInfo dateTimeFormatInfo)
        {
            // Basically takes the ShortDatePattern (dd-mm-yyyy, mm/dd/yyyy, etc) and strips everything except the dd-mm, mm/dd, etc.
            string shortPattern = dateTimeFormatInfo.ShortDatePattern;
            while (shortPattern[0] != 'd' && shortPattern[0] != 'M')
            {
                shortPattern = shortPattern.Substring(1);
                if (shortPattern.Length == 0)
                    return dateTimeFormatInfo.ShortDatePattern;
            }
            while (shortPattern[shortPattern.Length - 1] != 'd' && shortPattern[shortPattern.Length - 1] != 'M')
            {
                shortPattern = shortPattern.Substring(0, shortPattern.Length - 1);
                if (shortPattern.Length == 0)
                    return dateTimeFormatInfo.ShortDatePattern;
            }
            return shortPattern;
        }

I test this with the following unittest:

        [TestMethod]
        public void ShortMonthDayPattern()
        {
            CultureInfo cultureNl = new CultureInfo("nl-NL");
            CultureInfo cultureUs = new CultureInfo("en-US");

            Assert.AreEqual("1-7", testDate1.ToString(cultureNl.DateTimeFormat.ShortMonthDayPattern(), cultureNl), "Dutch culture");
            Assert.AreEqual("7/1", testDate1.ToString(cultureUs.DateTimeFormat.ShortMonthDayPattern(), cultureUs), "United States culture");

        }

This runs fine on my local development machine, but when I push changes to my repo the Build Pipeline breaks with the following message:

  Failed ShortMonthDayPattern [120 ms]
  Error Message:
   Assert.AreEqual failed. Expected:<1-7>. Actual:<01-07>. Dutch culture
  Stack Trace:
     at Helper.Test.Extensions.DateTimeFormatInfoExtensionsTest.ShortMonthDayPattern() in D:\a\1\s\Helper.Test\Extensions\DateTimeFormatInfoExtensionsTest.cs:line 22

Since I specify the culture, how is it possible that the test fails on the build agent and succeeds on my local machine?

Since I specify the culture, how is it possible that the test fails on the build agent and succeeds on my local machine?

Because the rules for different cultures can vary by operating system, version of operating system, version of .NET, and version of data files that could in some cases be updated independently.

Fundamentally, there are data files somewhere on the machine, roughly representing the data in the Unicode CLDR project . That data is exposed via APIs such as ShortMonthDayPattern . If two machines have different data, for whatever reason, that difference will be exposed via the APIs.

Not sure that the following is the reason in your case, but note that creating culture via constructor will have all culture overrides made by user on Windows:

The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the specified culture identifier matches the culture identifier of the current Windows culture, this constructor creates a CultureInfo that uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property.

You can try using ctor accepting boolean parameter useUserOverride or CultureInfo.GetCultureInfo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM