Skip to content

DateOnly and TimeOnly: retarget net10.0, add conversion samples and tests - #2152

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63545-dateonly
Open

DateOnly and TimeOnly: retarget net10.0, add conversion samples and tests#2152
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/63545-dateonly

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets dotnet-datetime/DateOnlyAndTimeOnlyInCSharp to .NET 10 and fills the gap the article's biggest query cluster is asking about: converting between DateTime, DateOnly, and TimeOnly.

Retarget and packages

  • Both projects net6.0 -> net10.0.
  • Test packages to the current stable set a net10.0 test project needs: Microsoft.NET.Test.Sdk 18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.

New sample code

  • DemonstrateConversions() on the existing Program: ToDateTime(TimeOnly), ToDateTime(TimeOnly.MinValue), ToDateTime(TimeOnly, DateTimeKind), DateOnly.FromDateTime(DateTime.Now), TimeOnly.FromDateTime(DateTime.Now), TimeOnly.FromTimeSpan, ToTimeSpan(), DayNumber, FromDayNumber, ParseExact("2022-01-01", "yyyy-MM-dd"), and AddHours(double, out int).
  • CurrentCulture is set to en-US in Main so the printed output does not change with the host machine.

Tests

  • 11 new xUnit facts, 25 total, all passing on the .NET 10.0.302 SDK.
  • The DateTime.Now facts assert round-trip consistency rather than a fixed value, since the suite runs at an arbitrary instant.

Build is clean: 0 warnings, 0 errors.

…ests

Retarget both projects from net6.0 to net10.0 and bring the test packages
up to the versions a net10.0 test project needs (Microsoft.NET.Test.Sdk
18.9.0, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector
10.0.1).

Add DemonstrateConversions() covering the DateOnly-to-DateTime direction
the sample did not have: ToDateTime(TimeOnly), ToDateTime(TimeOnly.MinValue),
ToDateTime(TimeOnly, DateTimeKind), FromDateTime(DateTime.Now) on both
types, TimeOnly.FromTimeSpan/ToTimeSpan, DayNumber/FromDayNumber,
ParseExact with a fixed format, and AddHours(double, out int).

Set CurrentCulture to en-US in Main so the printed output is the same on
every machine instead of depending on the host culture.

Eleven new xUnit facts cover the additions. The DateTime.Now facts assert
round-trip consistency rather than an exact value, because the suite runs
at an arbitrary instant.
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