call-datetime-fromtimestamp (DTZ006)#
Derived from the flake8-datetimez linter.
What it does#
Checks for usage of datetime.datetime.fromtimestamp()
without a tz
argument.
Why is this bad?#
Python datetime objects can be naive or timezone-aware. While an aware object represents a specific moment in time, a naive object does not contain enough information to unambiguously locate itself relative to other datetime objects. Since this can lead to errors, it is recommended to always use timezone-aware objects.
datetime.datetime.fromtimestamp(ts)
returns a naive datetime object.
Instead, use datetime.datetime.fromtimestamp(ts, tz=)
to return a
timezone-aware object.
Example#
Use instead:
Or, for Python 3.11 and later: