redline
LowError handlingnaive-datetime

Time without a time zone

Time without a time zone. datetime.now() has no time zone, so it means the server's local time.

This is how the comment reads inline, under the flagged line.

Why it matters

Naive datetimes compare wrongly with aware ones and shift when the server's zone or DST changes.

What to do

datetime.now(UTC).

Examples

Flagged
ts = datetime.now()
Better
ts = datetime.now(UTC)

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

ts = datetime.now()

Left alone

ts = datetime.now(tz=UTC)

Research

`datetime.now()` with no timezone. Timestamps shift by hours across hosts.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "naive-datetime"
    ],
    "severity": {
      "naive-datetime": "medium"
    }
  }
}

Read the source or report a false positive.