Hi .NET Folks
Our C# and VB.NET
analysis now provides guidelines and best practices for handling date and time operations. These rules cover a wide range of topics, including time zone conventions, common pitfalls, date parsing and formatting, time arithmetic, and more.
- Use DateTime(Offset).UnixEpoch instead of pointing to that date using the constructor
- Always use DateTimeOffset instead of DateTime
- Do not use DateTime.Now for benchmarking or timing operations
- Always set the DateTimeKind when creating a new DateTime object
- Don’t hardcode the format provider when turning dates and times to string
- Always use DateTime.Parse overloads with an IFormatProvider parameter
- Use TimeZoneInfo.FindSystemTimeZoneById instead of TimezoneConverter
- Use UTC when recording DateTime instants
- Date and time should not be used as types for primary keys
These new rules are available now on SonarCloud and will be in SonarQube 10.2 and upcoming releases of SonarLint.
We’d love to hear your feedback on these new rules.
Tom