You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DateTime.ToString with different behavior from local machine and Azure DevOps #96335
Hi, I noticed that after upgrading from dotnet 7 to dotnet 8.0.0 the DateTime.ToString() behavior has changed between my machine and the Azure DevOps pipeline build.
Given the following simple code:
[Fact]
public void DateTimeTest()
{
var dateTime = new DateTime(2023, 5, 2, 13, 30, 0, DateTimeKind.Utc);
var culture = new CultureInfo("en-US");
string result = dateTime.ToString("t", culture);
result.Should().Be("1:30 PM");
}
When running on my local machine the test runs successfully, but when running inside Azure DevOps I have the following behavior:
Error Message:
Expected actualOutput to be "1:30 PM", but "1:30 PM" differs near " PM" (index 4).
The two strings are identical but after copying and pasting the space disappeared, here is an image to prove it
Looking at the two strings they seem identical but the test still breaks, looking a little closer it seems that there is a differentiation of space.
This is the difference between culture.DateTimeFormat.ShortTimePattern in my machine and Azure DevOps.
Activity
ghost added
untriagedNew issue has not been triaged by the area owner
I don't go into it in the linked issue, but I will always be automatically suspicious of any server-deployed code that does culture-specific formatting.
There are a few cases that require it (eg, plain-text email to user), but the 99% case should be culture agnostic, and using the ISO format (eg, the "O" format specifier).
In addition, running localization tests and checking for exact results will always be brittle - since the localization data comes from the OS and can be updated at any time even tests run on the same machine can fail. Normally you should just check you get some sort of output (non-blank string), and ignore the actual contents. If you must check for exact results, create your own pseudo culture (which can be useful for other testing reasons too) and provide unique format strings
Reacted by Michał Petryka, Huo Yaoyuan and Joe4evr
@MatheusXavier The distinction arises from the ICU version underlying the operating system. In earlier ICU versions, standard spaces were employed in date patterns. However, in recent ICU versions, a non-breaking space is utilized to ensure the proper display of date strings without causing line breaks. As suggested by @Clockwork-Muse and @MichalPetryka you can consider using fixed formats (or you can use Invariant culture when formatting the date which guarantee date patterns never change there). I am closing the issue but feel free to send any questions if you have any.
Activity
danmoseley commentedon Dec 27, 2023
See #96022
Clockwork-Muse commentedon Dec 27, 2023
I don't go into it in the linked issue, but I will always be automatically suspicious of any server-deployed code that does culture-specific formatting.
There are a few cases that require it (eg, plain-text email to user), but the 99% case should be culture agnostic, and using the ISO format (eg, the
"O"format specifier).In addition, running localization tests and checking for exact results will always be brittle - since the localization data comes from the OS and can be updated at any time even tests run on the same machine can fail. Normally you should just check you get some sort of output (non-blank string), and ignore the actual contents. If you must check for exact results, create your own pseudo culture (which can be useful for other testing reasons too) and provide unique format strings
MichalPetryka commentedon Dec 27, 2023
Using RFC3339 should be okay too but dotnet doesn't have that built in IIRC.
Or try to roundtrip it back with parse.
tarekgh commentedon Dec 27, 2023
@MatheusXavier The distinction arises from the ICU version underlying the operating system. In earlier ICU versions, standard spaces were employed in date patterns. However, in recent ICU versions, a non-breaking space is utilized to ensure the proper display of date strings without causing line breaks. As suggested by @Clockwork-Muse and @MichalPetryka you can consider using fixed formats (or you can use Invariant culture when formatting the date which guarantee date patterns never change there). I am closing the issue but feel free to send any questions if you have any.