requests-cache icon indicating copy to clipboard operation
requests-cache copied to clipboard

datetime.utcnow() is not offset-aware and should be replaced with datetime.now(timezone.utc)

Open sebastianrasor opened this issue 3 years ago • 1 comments

The problem

For some reason datetime.utcnow() is not offset-aware. This can be verified with datetime.utcnow().tzinfo is None.

datetime.utcnow() should be replaced with datetime.now(timezone.utc), which is offset-aware. This can be verified with datetime.now(timezone.utc).tzinfo is None.

sebastianrasor avatar Sep 11 '22 21:09 sebastianrasor

Are you referring to CachedResponse.created_at and expires? Those are only created, modified, and compared internally, so they do not need to be timezone-aware; they are always assumed to be UTC. Any user- or header-provided datetimes are also converted to timezone-naive UTC. This is needed because not all storage backends have a native method to store a timezone offset.

Is there a specific problem you've encountered with this?

JWCook avatar Sep 12 '22 17:09 JWCook