Skip to content

Make DateTime.Parse translation tests culture-invariant - #38384

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ajcvickers:fix/datetime-parse-culture-invariant
Jun 8, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ajcvickers:fix/datetime-parse-culture-invariant

Conversation

@ajcvickers

Copy link
Copy Markdown
Contributor

They fail locally for me against the Cosmos emulator on Mac, which has GB-en settings. (So sane date ordering. :-) )

Parse_with_constant and Parse_with_parameter parsed "5/4/1998 15:30:10 PM",
which DateTime.Parse interprets using the ambient culture: en-US reads it as
4 May 1998 (matching the seed data), while en-GB (and other d/M/yyyy cultures)
reads it as 5 April 1998, so the query matched no rows and both tests failed
locally on non-US machines (they passed on en-US CI).

Use an unambiguous ISO-8601 string ("1998-05-04T15:30:10") parsed identically
across cultures. The resulting DateTime value, query translation shape, and
provider SQL baselines are unchanged, so no provider overrides need updating.
@ajcvickers
ajcvickers requested a review from a team as a code owner June 8, 2026 16:05
Copilot AI review requested due to automatic review settings June 8, 2026 16:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates DateTime parsing strings in temporal translation tests to use an ISO-8601-like format for more deterministic parsing across environments.

Changes:

  • Replaced culture-dependent date/time string literals with "1998-05-04T15:30:10" in Parse_with_constant.
  • Replaced the parameterized date string with the same ISO-8601-like format in Parse_with_parameter.

[Fact]
public virtual Task Parse_with_constant()
=> AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(o => o.DateTime == DateTime.Parse("5/4/1998 15:30:10 PM")));
=> AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(o => o.DateTime == DateTime.Parse("1998-05-04T15:30:10")));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colin Copilot is wrong. We need expressions that EF can translate. Full write up from Claude to Colin below:

I tried this, and it doesn't work for these tests. Passing CultureInfo.InvariantCulture (via ParseExact or the Parse overload) introduces a member-access argument that causes EF's funcletizer to parameterize the expression instead of inlining it as a constant. The Parse_with_constant translation then becomes:

@ParseExact='1998-05-04T15:30:10'
… WHERE (c["DateTime"] = @ParseExact)

That breaks the asserted SQL baseline and, more importantly, collapses Parse_with_constant and Parse_with_parameter into identical parameterized SQL — eliminating the constant-vs-parameter distinction these two tests exist to verify. There's no Parse/ParseExact overload that is both explicitly invariant and inlinable as a constant, so this trade-off is unavoidable. (Switching to a DateTime constructor would sidestep Parse, but the inlined-constant DateTime path is already covered by New_with_constant, and the point of these tests is to exercise Parse.)

On the broader culture point: you're right that an ISO‑8601 string isn't invariant under non-Gregorian process calendars (e.g. th-TH parses the year as 2541). But the failure this PR fixes is the d/M/yyyy vs M/d/yyyy ambiguity of "5/4/1998", which the ISO format resolves for all Gregorian-calendar cultures — and non-Gregorian process cultures aren't part of EF's test matrix. So I'm keeping the single-arg DateTime.Parse with the ISO literal.

Comment on lines +96 to 98
var date = "1998-05-04T15:30:10";

return AssertQuery(ss => ss.Set<BasicTypesEntity>().Where(o => o.DateTime == DateTime.Parse(date)));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

@AndriySvyryd
AndriySvyryd merged commit e4afabf into dotnet:main Jun 8, 2026
14 checks passed
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants