Skip to content

Commit

Permalink
Remove usage of utcnow (#8791)
Browse files Browse the repository at this point in the history
* Remove usage of utcnow

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
amweiss and pre-commit-ci[bot] committed Jan 14, 2024
1 parent dc49ec2 commit fa1d98c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 63 deletions.
4 changes: 2 additions & 2 deletions celery/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time
import warnings
from collections import namedtuple
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from functools import partial
from weakref import WeakValueDictionary

Expand Down Expand Up @@ -460,7 +460,7 @@ def _get_result_meta(self, result,
state, traceback, request, format_date=True,
encode=False):
if state in self.READY_STATES:
date_done = datetime.utcnow()
date_done = datetime.now(timezone.utc)
if format_date:
date_done = date_done.isoformat()
else:
Expand Down
8 changes: 4 additions & 4 deletions celery/backends/database/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Database models used by the SQLAlchemy result store backend."""
from datetime import datetime
from datetime import datetime, timezone

import sqlalchemy as sa
from sqlalchemy.types import PickleType
Expand All @@ -22,8 +22,8 @@ class Task(ResultModelBase):
task_id = sa.Column(sa.String(155), unique=True)
status = sa.Column(sa.String(50), default=states.PENDING)
result = sa.Column(PickleType, nullable=True)
date_done = sa.Column(sa.DateTime, default=datetime.utcnow,
onupdate=datetime.utcnow, nullable=True)
date_done = sa.Column(sa.DateTime, default=datetime.now(timezone.utc),
onupdate=datetime.now(timezone.utc), nullable=True)
traceback = sa.Column(sa.Text, nullable=True)

def __init__(self, task_id):
Expand Down Expand Up @@ -84,7 +84,7 @@ class TaskSet(ResultModelBase):
autoincrement=True, primary_key=True)
taskset_id = sa.Column(sa.String(155), unique=True)
result = sa.Column(PickleType, nullable=True)
date_done = sa.Column(sa.DateTime, default=datetime.utcnow,
date_done = sa.Column(sa.DateTime, default=datetime.now(timezone.utc),
nullable=True)

def __init__(self, taskset_id, result):
Expand Down
4 changes: 2 additions & 2 deletions celery/backends/elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Elasticsearch result store backend."""
from datetime import datetime
from datetime import datetime, timezone

from kombu.utils.encoding import bytes_to_str
from kombu.utils.url import _parse_url
Expand Down Expand Up @@ -129,7 +129,7 @@ def _set_with_state(self, key, value, state):
body = {
'result': value,
'@timestamp': '{}Z'.format(
datetime.utcnow().isoformat()[:-3]
datetime.now(timezone.utc).isoformat()[:-9]
),
}
try:
Expand Down
2 changes: 1 addition & 1 deletion celery/utils/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def remaining(
using :func:`delta_resolution` (i.e., rounded to the
resolution of `ends_in`).
now (Callable): Function returning the current time and date.
Defaults to :func:`datetime.utcnow`.
Defaults to :func:`datetime.now(timezone.utc)`.
Returns:
~datetime.timedelta: Remaining time.
Expand Down
7 changes: 5 additions & 2 deletions t/unit/app/test_beat.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ def is_due(self, *args, **kwargs):

class mocked_schedule(schedule):

def __init__(self, is_due, next_run_at, nowfun=datetime.utcnow):
def now_func():
return datetime.now(timezone.utc)

def __init__(self, is_due, next_run_at, nowfun=now_func):
self._is_due = is_due
self._next_run_at = next_run_at
self.run_every = timedelta(seconds=1)
Expand Down Expand Up @@ -872,7 +875,7 @@ def test_maybe_make_aware(self):
def test_to_local(self):
x = schedule(10, app=self.app)
x.utc_enabled = True
d = x.to_local(datetime.utcnow()) # datetime.utcnow() is deprecated in Python 3.12
d = x.to_local(datetime.now())
assert d.tzinfo is None
x.utc_enabled = False
d = x.to_local(datetime.now(timezone.utc))
Expand Down
98 changes: 49 additions & 49 deletions t/unit/backends/test_elasticsearch.py

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion t/unit/backends/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,10 @@ def test_cleanup(self, mock_get_database):
mock_database.__getitem__ = Mock(name='MD.__getitem__')
mock_database.__getitem__.return_value = mock_collection

self.backend.app.now = datetime.datetime.utcnow
def now_func():
return datetime.datetime.now(datetime.timezone.utc)

self.backend.app.now = now_func
self.backend.cleanup()

mock_get_database.assert_called_once_with()
Expand Down
4 changes: 2 additions & 2 deletions t/unit/utils/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ def test_tz_when_zoneinfo(self):
def test_maybe_make_aware(self):
aware = datetime.now(_timezone.utc).replace(tzinfo=timezone.utc)
assert maybe_make_aware(aware)
naive = datetime.utcnow() # datetime.utcnow() is deprecated in Python 3.12
naive = datetime.now()
assert maybe_make_aware(naive)
assert maybe_make_aware(naive).tzinfo is ZoneInfo("UTC")

tz = ZoneInfo('US/Eastern')
eastern = datetime.now(_timezone.utc).replace(tzinfo=tz)
assert maybe_make_aware(eastern).tzinfo is tz
utcnow = datetime.utcnow() # datetime.utcnow() is deprecated in Python 3.12
utcnow = datetime.now()
assert maybe_make_aware(utcnow, 'UTC').tzinfo is ZoneInfo("UTC")


Expand Down

0 comments on commit fa1d98c

Please sign in to comment.