Skip to main content

ci pre-commit.ci status

flake8-ban-utcnow

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 CEST and we want to derive a datetime in UTC formatted as a timestamp hence calling utcnow().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 UTC we call utcfromtimestamp instead 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 CEST and we want to actually derive a datetime in 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 datetime objects can be compared hence using this forces us to always make sure all objects are timezone aware.

Metadata

Release files for flake8-ban-utcnow 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-ban-utcnow 0.1.0
File Size Uploaded
flake8_ban_utcnow-0.1.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-ban-utcnow 0.1.0
File Interpreter ABI Platform
flake8_ban_utcnow-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 8.1 kB

Release files / flake8_ban_utcnow-0.1.0.tar.gz

Download URL flake8_ban_utcnow-0.1.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
53d4c7ef638c8e248eed12f4e83d0862442c67e697a2bc0e27102a2c62841592
BLAKE2b-256 checksum
How to use checksums
f11ebfb175f29e1359f5e2b10f41153993204ba8b8731a8a61e68c03743fcb4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / flake8_ban_utcnow-0.1.0-py2.py3-none-any.whl

Download URL flake8_ban_utcnow-0.1.0-py2.py3-none-any.whl
Size 4.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
eaa27d7a0f552ed2501a36312a6bf03ebfec71dca9f39e0d823d154cccdb009f
BLAKE2b-256 checksum
How to use checksums
4328fe5678ee108a312bdc3c03c54070d0acf0ede50f3d361bdf6dd142a0297b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page