flake8 plugin which bans the usage of datetime.datetime.utcnow. Forked to support 3.6.
Project description
flake8-ban-utcnow-36
Forked from https://github.com/jkittner/flake8-ban-utcnow to run under Python 3.6
flake8 plugin which checks that datetime.utcnow() is not used. It suggests using datetime.now(timezone.utc) instead.
note: timezone must be imported from datetime first:
from datetime import datetime
from datetime import timezone
datetime.now(timezone.utc)
installation
pip install flake8-ban-utcnow
flake8 code
| Code | Description |
|---|---|
| UTC001 | don't use datetime.utcnow(), use datetime.now(timezone.utc) instead |
as a pre-commit hook
See pre-commit for instructions
Sample .pre-commit-config.yaml:
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies: [flake8-ban-utcnow==0.1.0]
rationale
One could expect that when explicitly calling datetime.utcnow() the datetime
object would be timezone aware, but it's not! A common pitfall is, deriving a
timestamp from the datetime object created using datetime.utcnow().
example
-
the computer is in
CESTand we want to derive adatetimein UTC formatted as a timestamp hence callingutcnow().timestamp().>>> from datetime import datetime >>> datetime.utcnow() datetime.datetime(2022, 8, 7, 23, 40, 17, 7858) >>> datetime.utcnow().timestamp() 1659908656.048843
-
if we convert the timestamp, it says this, which is obviously incorrect.
GMT: Sunday, 7. August 2022 21:44:16 Your time zone: Sunday, 7. August 2022 23:44:16 GMT+02:00 DST Relative: 2 hours ago -
converting it using python and
datetime.fromtimestamp, we by accident get the correct datetime in UTC>>> datetime.fromtimestamp(1659908656.048843) datetime.datetime(2022, 8, 7, 23, 44, 16, 48843)
-
being aware that the timestamp should be in
UTCwe callutcfromtimestampinstead and get the result as above, since the timestamp actually is in local time, but unaware of this.>>> datetime.utcfromtimestamp(1659908656.048843) datetime.datetime(2022, 8, 7, 21, 44, 16, 48843)
the correct way
-
the computer is in
CESTand we want to actually derive adatetimein UTC formatted as a timestamp .>>> from datetime import timezone >>> from datetime import datetime >>> datetime.now(timezone.utc).timestamp() 1659916399.651218
-
we now get what we actually expect
GMT: Sunday, 7. August 2022 23:53:19 Your time zone: Monday, 8. August 2022 01:53:19 GMT+02:00 DST Relative: A few seconds ago -
the next thing to keep in mind is, that only timezone aware
datetimeobjects can be compared hence using this forces us to always make sure all objects are timezone aware.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flake8_ban_utcnow_36-0.2.0-py2.py3-none-any.whl.
File metadata
- Download URL: flake8_ban_utcnow_36-0.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/1.0.0 urllib3/1.26.15 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a04acc270a50cfe8bb02e4e9911aaa66086279ce7c9254cace8da134a50f95a0
|
|
| MD5 |
9192823be175ea94439e1035a0364183
|
|
| BLAKE2b-256 |
479bbc4653b3d8ebaafbd89939d434e7bf3b21be3a8e6b02ebaeeedd0a78ae5c
|