Write Clean Code with Python Date & Time Libraries

Hello Pythonistas,

Working with date & time can be tricky, so we’ve added some new rules to help you avoid pitfalls when using Date & Time libraries in Python, including the Numpy & Pandas libraries:

  • S6882: Constructor attributes of date and time objects should be in the range of possible values
  • S6883: The 12-hour format should be used with the AM/PM marker, otherwise 24-hour format should be used
  • S6887: pytz.timezone should not be passed to the datetime.datetime constructor
  • S6890: zoneinfo should be preferred to pytz when using Python 3.9 and later
  • S6894: A date should be formatted correctly when using “pandas.to_datetime” with “dayfirst” or “yearfirst” arguments
  • S6900: Numpy weekmask should have a valid value
  • S6903: Using timezone aware "datetime"s should be preferred over using “datetime.datetime.utcnow” and “datetime.datetime.utcfromtimestamp”

These rules will be available in SonarCloud shortly and will be available in SonarQube 10.5 and the next SonarLint release.

Your feedback on these rules is important to us. Do have a look at what’s coming up for Python in SonarLint, SonarQube and SonarCloud .