Skip to content

Parsing DateTime in C#: retarget net10.0 and fix the misused format constant - #2157

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/91779-parse-string
Open

Parsing DateTime in C#: retarget net10.0 and fix the misused format constant#2157
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/91779-parse-string

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample for https://code-maze.com/csharp-convert-string-to-datetime/ (dotnet-datetime/ParsingDateTimeInCSharp).

Retarget

Both projects go net7.0 -> net10.0. Test packages lifted to current: Microsoft.NET.Test.Sdk 17.3.2 -> 18.9.0, xunit 2.4.2 -> 2.9.3, xunit.runner.visualstudio 2.4.5 -> 4.0.0, coverlet.collector 3.1.2 -> 10.0.1. Versions queried from NuGet at the time of the change.

A real defect in the sample

Program.cs held DATEFORMAT_DDMYYYY_HHMMSS = "15/1/2023 10:12:12" (a date string in a constant named FORMAT) and passed it as ParseExact's format argument.

It did not throw. Every digit in that "format" is a literal character, and / and : happen to match fr-FR's date and time separators, so the pattern matched the input, extracted no date or time component at all, and ParseExact filled the empty result from the current date. The program printed today at midnight and its output changed every day, while the source comment claimed 15/01/2023 10:12:12 AM. The tests stayed green because ParseExactTest.cs uses the correct "dd/M/yyyy hh:mm:ss". The constant now holds that format string, and the run prints 1/15/2023 10:12:12 AM.

A second drift against the article

Parse(String) is the only demonstration that parses with the current culture, and the article passes it a US-shaped string, "1/15/2023 02:21:37". The sample passed it the day-first string that the fr-FR overloads use, so on a US machine the first demonstration threw a FormatException and the catch swallowed the whole Parse region: four further overloads never ran. The two strings are now two constants, matching the article snippet by snippet.

Output comments are reset from an actual run and now state the culture they assume, because Console.WriteLine formats a DateTime with CurrentCulture regardless of which culture parsed it. Console.ReadKey() is dropped so the sample runs non-interactively.

Idiom

File-scoped namespaces in all five files; Program.cs becomes top-level statements. The very long method names are deliberately left alone: they are the article's H3 titles made executable.

Verification

SDK 10.0.302. dotnet build -c Release: 0 warnings, 0 errors. dotnet test -c Release: Passed 20, Failed 0. dotnet run completes end to end and every output comment matches what it printed.

…file-scoped namespaces

Retargets both projects net7.0 -> net10.0 and lifts the test packages:
Microsoft.NET.Test.Sdk 17.3.2 -> 18.9.0, xunit 2.4.2 -> 2.9.3,
xunit.runner.visualstudio 2.4.5 -> 4.0.0, coverlet.collector 3.1.2 -> 10.0.1.

Fixes a real defect in the sample. DATEFORMAT_DDMYYYY_HHMMSS held a date
string, "15/1/2023 10:12:12", and was passed as ParseExact's format argument.
It did not throw: every digit in it is a literal character and "/" and ":"
happen to match fr-FR's separators, so the pattern matched the input, extracted
no date or time component, and ParseExact filled the empty result from the
current date. The program printed today at midnight and changed every day,
while the source comment claimed 15/01/2023 10:12:12 AM. The tests never saw it
because ParseExactTest.cs uses the correct "dd/M/yyyy hh:mm:ss". The constant
now holds that format string.

Fixes a second drift against the article. The Parse(String) demonstration is the
only one that parses with the current culture, and the article passes it a
US-shaped string, "1/15/2023 02:21:37". The sample passed it the day-first
string used by the fr-FR overloads, so the first demonstration threw a
FormatException on a US machine and the whole Parse region was skipped. The two
strings are now two constants, matching the article snippet by snippet.

Output comments are reset from an actual run and now say which culture they
assume, since Console.WriteLine formats a DateTime with CurrentCulture whatever
culture parsed it. Console.ReadKey() is dropped so the sample can run
non-interactively.

Idiom lift: file-scoped namespaces in all five files, and Program.cs becomes
top-level statements. The long method names are deliberately unchanged; they
are the article's H3 titles made executable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant