Skip to content

DateTime.AddSeconds(double) precision error #80549

Open
@StefanBertels

Description

@StefanBertels

Description

Upgrading to .NET 7.0 results in (small) DateTime errors when adding seconds (double), probably introduced by #73198.

While it's clear that this cannot be precise for every value it should be precise for values within some range (including the attached example which works flawless in .NET 6.0).

Reproduction Steps

[Fact]
void DateTimeAddSecondsRegression()
{
    var dt = DateTime.Parse("2012-09-08T01:22:27.249");
    
    Assert.Equal(dt, dt.Date.AddTicks(Convert.ToInt64(dt.TimeOfDay.TotalSeconds * TimeSpan.TicksPerSecond)));
    Assert.Equal(dt, dt.Date.AddSeconds(dt.TimeOfDay.TotalSeconds)); // fails in .NET7
}

Expected behavior

Test should be successful every time.

Actual behavior

Test fails for .NET 7.0 (while successful for .NET 6.0).

Regression?

Yes

Known Workarounds

Workaround: Avoid AddSeconds() method and use your own calculation using ticks.

Configuration

  • dotnet 7.0.101
  • Windows 10 x64
  • Bug is in DateTime.cs (seems to be independent of OS)

Other information

https://github.com/dotnet/runtime/blob/2335269/src/libraries/System.Private.CoreLib/src/System/DateTime.cs#L862

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions