Skip to content

DateTime.ToString with different behavior from local machine and Azure DevOps #96335

Closed

Description

@MatheusXavier

Description

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
image

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.
image

Activity

  1. ghost added
    untriaged
    on Dec 27, 2023
  2. danmoseley commented on Dec 27, 2023

    @danmoseley
    Contributor

    See #96022

  3. Clockwork-Muse commented on Dec 27, 2023

    @Clockwork-Muse
    Contributor

    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

  4. MichalPetryka commented on Dec 27, 2023

    @MichalPetryka
    Contributor

    using the ISO format

    Using RFC3339 should be okay too but dotnet doesn't have that built in IIRC.

    ignore the actual contents

    Or try to roundtrip it back with parse.

  5. tarekgh commented on Dec 27, 2023

    @tarekgh
    Member

    @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.

  6. ghost removed
    untriaged
    on Dec 27, 2023
  7. locked and limited conversation to collaborators on Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions