Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateFormatter prints extra dash "-" for negative date times #715

Closed
thejoecode opened this issue Nov 12, 2022 · 0 comments
Closed

DateFormatter prints extra dash "-" for negative date times #715

thejoecode opened this issue Nov 12, 2022 · 0 comments
Labels
Milestone

Comments

@thejoecode
Copy link

thejoecode commented Nov 12, 2022

Describe the bug

Version 19, .net Core 7
Date times such as: DateTimeOffset.Parse("2022-09-25T17:59:59-07:00")
Get saved to diff file as: 2022-09-25 18:00 --7

A clear and concise description of what you expected to happen:
Ideally the result would be: 2022-09-25 18:00 -7

Add any other context about the problem here.
Issue is in: src/Verify/Serialization/DateFormatter.cs

GetDateOffset adds a dash "-" to the front of the negative number 7.
For some reason offsetMinutes will also be negative for the offset minute case:
DateTimeOffset.Parse("2022-09-25T17:59:59-07:30") => --7--30

static string GetDateOffset(DateTimeOffset value)
{
    var offset = value.Offset;

    if (offset > TimeSpan.Zero)
    {
        if (offset.Minutes == 0)
        {
            return $"+{offset.TotalHours:0}";
        }

        return $"+{offset.Hours:0}-{offset.Minutes:00}";
    }

    if (offset < TimeSpan.Zero)
    {
        if (offset.Minutes == 0)
        {
            return $"-{offset.Hours:0}";
        }

        return $"-{offset.Hours:0}-{offset.Minutes):00}";
    }

    return "+0";
}

Minimal Repro

  [Fact]
  public async Task DateTest() {
    var dto = DateTimeOffset.Parse("2022-09-25T17:59:59-07:30");

    await Verify(dto)
      .DontScrubDateTimes();
  }

Submit a PR that fixes the bug

static string GetDateOffset(DateTimeOffset value)
{
    var offset = value.Offset;

    if (offset > TimeSpan.Zero)
    {
        if (offset.Minutes == 0)
        {
            return $"+{offset.TotalHours:0}";
        }

        return $"+{offset.Hours:0}-{offset.Minutes:00}";
    }

    if (offset < TimeSpan.Zero)
    {
        if (offset.Minutes == 0)
        {
            // remove dash
            return $"{offset.Hours:0}";
        }
        var minutes = Math.Abs(offset.Minutes);
        // remove dash, make sure positive
        return $"{offset.Hours:0}-{minutes:00}";
    }

    return "+0";
}
@SimonCropp SimonCropp added this to the 19.0.1 milestone Nov 12, 2022
@SimonCropp SimonCropp added the Bug label Nov 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants