Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#32366 closed Cleanup/optimization (fixed)

Update usage of now() & co

Reported by: Carlton Gibson Owned by: Nick Pope
Component: Utilities Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using datetime.now(), rather than utcnow() is the modern recommended approach.

Warning in docs:

... the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc).

https://docs.python.org/3.9/library/datetime.html#datetime.datetime.now
https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow

More depth here:
https://blog.ganssle.io/articles/2019/11/utcnow.html

datetime.utcfromtimestamp() and datetime.utctimetuple() carry similar warnings.

  • Update the code to modern usage.
  • Update docs examples (for backport to stable docs).

Change History (9)

comment:1 by Carlton Gibson, 4 years ago

Owner: changed from nobody to Carlton Gibson

comment:2 by Mariusz Felisiak, 4 years ago

Summary: Update usage of now() &coUpdate usage of now() & co
Triage Stage: UnreviewedAccepted

comment:3 by Jacob Walls, 4 years ago

Has patch: set

comment:4 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:5 by Nick Pope, 3 years ago

Owner: changed from Carlton Gibson to Nick Pope
Patch needs improvement: unset

I've created a new PR to try and get this completed.

comment:6 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Carlton Gibson <carlton.gibson@…>, 3 years ago

In 69ffb1ac:

Refs #32366 -- Avoided use of datetime.utcnow() in the documentation.

comment:8 by Carlton Gibson <carlton.gibson@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In d06c5b3:

Fixed #32366 -- Updated datetime module usage to recommended approach.

  • Replaced datetime.utcnow() with datetime.now().
  • Replaced datetime.utcfromtimestamp() with datetime.fromtimestamp().
  • Replaced datetime.utctimetuple() with datetime.timetuple().
  • Replaced calendar.timegm() and datetime.utctimetuple() with datetime.timestamp().

comment:9 by Carlton Gibson <carlton.gibson@…>, 3 years ago

In cd84f7ac:

[3.2.x] Refs #32366 -- Avoided use of datetime.utcnow() in the documentation.

Backport of 69ffb1acf38bd34f76707468bb592eb4b164e2da from main

Note: See TracTickets for help on using tickets.
Back to Top