Rule "Use a testable date/time provider" does not flag DateTimeOffset

  • ALM used: Azure DevOps
  • CI system used: Azure DevOps
  • Languages of the repository: C#
  • Description:
    The rule to to enforce use of a testable date/time provider (csharpsquid:S6354) only flags the code when DateTime.Now is used, not when using DateTimeOffset.Now. Is this intentional?
    public interface ITimeProvider
    {
        DateTime GetDateTime();

        DateTimeOffset GetDateTimeOffset();
    }

    public class TimeProvider : ITimeProvider
    {
        public DateTime GetDateTime()
        {
            return DateTime.Now; // Flagged.
        }

        public DateTimeOffset GetDateTimeOffset()
        {
            return DateTimeOffset.Now; // Not flagged.
        }
    }

Additional question:
In the code above, how am I supposed to write a time provider, which actually returns DateTime.Now without it being flagged?

Hello @emilatenerginet and welcome to our community!

First of all thank you for reporting this false negative. I can confirm it as a valid case and I have opened this issue, where you can track the progress.

All the best,
Čaba

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.