-
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert datetime.now() to django.utils.timezone.now(). Closes #545
- datetime.now() isn't very safe for timezone enabled scenarios so make use of django.utils.timezone.now() which acts according to settings - add new linter to prevent & discover usage of datetime.now() - how to configure time zone has already been documented so close issue #545 with this commit
- Loading branch information
Showing
13 changed files
with
56 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Warns against usage of datetime.now() which may not be fully timezone | ||
safe. Instead we should use django.utils.timezone.now() which acts | ||
according to settings! | ||
""" | ||
|
||
from astroid import nodes | ||
|
||
from pylint import checkers | ||
from pylint import interfaces | ||
|
||
|
||
class DatetimeChecker(checkers.BaseChecker): | ||
__implements__ = (interfaces.IAstroidChecker, ) | ||
|
||
name = 'datetime-checker' | ||
|
||
msgs = { | ||
'R4711': ('Do not use datetime.now(), use django.utils.timezone.now()', | ||
'datetime-now-used', | ||
'Use django.utils.timezone.now()! See: ' | ||
'https://docs.djangoproject.com/en/2.2/topics/i18n/timezones/' | ||
'#interpretation-of-naive-datetime-objects') | ||
} | ||
|
||
def visit_name(self, node): | ||
parent = node.parent | ||
while node.name == 'datetime' and isinstance(parent, nodes.Attribute): | ||
if parent.attrname == 'now': | ||
self.add_message('datetime-now-used', node=node) | ||
parent = parent.parent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters