no-datetime-today

Try in Playground
python-best-practicesBest PracticeInformational

0

No tags

No CWE or CVE

Avoid using datetime.today() and use instead datetime.now(). The two calls are equivalent (as mentioned in the official documentation) and the use of now() is more explicit than today().

Pattern Rule: datetime.today()


no-datetime-today

How to write a rule
function visit(pattern, filename, code) {
  const error = buildError(pattern.start.line, pattern.start.col, pattern.end.line, pattern.end.col, "use datetime.now() instead of datetime.today()", "INFO", "BEST_PRACTICES");
  const edit = buildEdit(pattern.start.line, pattern.start.col, pattern.end.line, pattern.end.col, "update", "datetime.now()");
  const fix = buildFix("use datetime.now()", [edit]);
  addError(error.addFix(fix));
}

pass

Expected test result: no error

Using datetime.now() does not trigger any issue

from datetime import datetime
print("foo")
bla = datetime.now()

error

Expected test result: has error

Using datetime.today()

from datetime import datetime
print("foo")
bla = datetime.today()
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.