Skip to content

DateTime.ParseExact - message text in FormatException does not include string argument #94837

Closed

Description

@apunin

Description

FormatException.Message contains empty string instead of actual string argument from DateTime.ParseExact

Reproduction Steps

try
{
    var dt = DateTime.ParseExact("11.2023", "dd.mm.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
catch(FormatException ex)
{
    Console.WriteLine(ex.Message);
}

Expected behavior

String '11.2023' was not recognized as a valid DateTime.

Actual behavior

String '' was not recognized as a valid DateTime.

Regression?

Works in net7-

Known Workarounds

No response

Configuration

net8/x64/Windows 11

Other information

No response

Activity

  1. ghost added
    untriaged
    on Nov 16, 2023
  2. karakasa commented on Nov 16, 2023

    @karakasa
    Contributor

    The exact bug occurs here:

    https://github.com/dotnet/runtime/blob/fe3abf5a6018226901054fb4bc2bc2aaf0fda462/src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeParse.cs#L4450C36-L4450C36

    All SetBadDateTimeFailure() from DateTimeParse.ParseByFormat() and .DoStrictParse() seemingly don't set the message argument. I will propose to put extra failure handling code in DateTimeParse.TryParseExact(), such as:

    internal static bool TryParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, DateTimeFormatInfo dtfi, DateTimeStyles style, scoped ref DateTimeResult result)
            {
                if (s.Length == 0)
                {
                    result.SetBadDateTimeFailure();
                    return false;
                }
    
                if (format.Length == 0)
                {
                    result.SetBadFormatSpecifierFailure();
                    return false;
                }
    
                Debug.Assert(dtfi != null, "dtfi == null");
    
                bool result = DoStrictParse(s, format, style, dtfi, ref result);
                if (!result && result.failure == ParseFailureKind.Format_BadDateTime && result.failureMessageFormatArgument == null)
                {
                    result.failureMessageFormatArgument = s.ToString();
                }
                return result;
            }
    

    Similar issues occur in DateTimeParse.TryParse as well.

  3. tarekgh commented on Nov 16, 2023

    @tarekgh
    Member

    This is fixed in the .NET 9.0 through #91303.

    CC @stephentoub

  4. ghost removed
    untriaged
    on Nov 16, 2023
  5. locked and limited conversation to collaborators on Dec 17, 2023
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